#P1391D. 505

    ID: 5496 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>bitmasksbrute forceconstructive algorithmsdpgreedyimplementation*2000

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 aa consisting of nn rows and mm 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 nn and mm (1nm1061 \leq n \leq m \leq 10^6 and nm106n\cdot m \leq 10^6)  — the number of rows and columns in aa, respectively.

The following nn lines each contain mm characters, each of which is one of 0 and 1. If the jj-th character on the ii-th line is 1, then ai,j=1a_{i,j} = 1. Similarly, if the jj-th character on the ii-th line is 0, then ai,j=0a_{i,j} = 0.

Output the minimum number of cells you need to change to make aa good, or output 1-1 if it's not possible at all.

Input

The first line of input contains two integers nn and mm (1nm1061 \leq n \leq m \leq 10^6 and nm106n\cdot m \leq 10^6)  — the number of rows and columns in aa, respectively.

The following nn lines each contain mm characters, each of which is one of 0 and 1. If the jj-th character on the ii-th line is 1, then ai,j=1a_{i,j} = 1. Similarly, if the jj-th character on the ii-th line is 0, then ai,j=0a_{i,j} = 0.

Output

Output the minimum number of cells you need to change to make aa good, or output 1-1 if it's not possible at all.

Samples

Sample Input 1

3 3
101
001
110

Sample Output 1

2

Sample Input 2

7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011

Sample Output 2

-1

Note

In the first case, changing a1,1a_{1,1} to 00 and a2,2a_{2,2} to 11 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 11 or 00.
  • A sub-matrix is described by 44 parameters — r1r_1, r2r_2, c1c_1, and c2c_2; here, 1r1r2n1 \leq r_1 \leq r_2 \leq n and 1c1c2m1 \leq c_1 \leq c_2 \leq m.
  • This sub-matrix contains all elements ai,ja_{i,j} that satisfy both r1ir2r_1 \leq i \leq r_2 and c1jc2c_1 \leq j \leq c_2.
  • A sub-matrix is, further, called an even length square if r2r1=c2c1r_2-r_1 = c_2-c_1 and r2r1+1r_2-r_1+1 is divisible by 22.