#DP1061. 505
505
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.
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 .
Format
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
3 3
101
001
110
2
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011
-1