#P1391D. 505
505
No submission language available for this problem.
Description
A binary matrix is called good if every even length square sub-matrix has an odd number of ones.
Given a binary matrix consisting of rows and columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all.
All the terms above have their usual meanings — refer to the Notes section for their formal definitions.
The first line of input contains two integers and ( and ) — the number of rows and columns in , respectively.
The following lines each contain characters, each of which is one of 0 and 1. If the -th character on the -th line is 1, then . Similarly, if the -th character on the -th line is 0, then .
Output the minimum number of cells you need to change to make good, or output if it's not possible at all.
Input
The first line of input contains two integers and ( and ) — the number of rows and columns in , respectively.
The following lines each contain characters, each of which is one of 0 and 1. If the -th character on the -th line is 1, then . Similarly, if the -th character on the -th line is 0, then .
Output
Output the minimum number of cells you need to change to make good, or output if it's not possible at all.
Samples
Note
In the first case, changing to and to is enough.
You can verify that there is no way to make the matrix in the second case good.
Some definitions —
- A binary matrix is one in which every element is either or .
- A sub-matrix is described by parameters — , , , and ; here, and .
- This sub-matrix contains all elements that satisfy both and .
- A sub-matrix is, further, called an even length square if and is divisible by .