Bad Apple!! - MarisaOJ: Marisa Online Judge

Bad Apple!!

Time limit: 1000 ms
Memory limit: 256 MB
**Rule 86: If it exists, it can run Bad Apple!** For the Gensokyo's light festival, Reimu prepared a n×n table of lanterns (a square of n rows and n columns) to play Bad Apple! As we know, a video consists of many frames. The current frame is A and the next frame is B. In one second, Reimu can flip the state of an entire row or an entire column of lanterns. Normally, transforming two frames shouldn't take longer than a split second, but we can wait. Calculate the smallest time, in seconds, needed for Reimu to transform frame A into frame B, or determine if it would take forever. ### Input - The first line contains an integer n. - The next n lines each contain a binary string of length n, representing frame A, where 1 means the lantern is on and 0 means it's off. - The following n lines each contain a binary string of length n, representing frame B, with the same meaning. ### Output - Print the minimum time, in seconds, required to transform A into B. If time would end before Reimu could do so, print -1. ### Constraints - 1≤n≤500. ### Example Input: 3010001110111100000 Output: -1