A binary matrix is called good if every even length square sub-matrix has an odd number of ones. 

Given a binary matrix $a$ consisting of $n$ rows and $m$ 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 $n$ and $m$ ($1 \leq n \leq m \leq 10^6$ and $n\cdot m \leq 10^6$)  — the number of rows and columns in $a$, respectively. 

The following $n$ lines each contain $m$ characters, each of which is one of 0 and 1. If the $j$-th character on the $i$-th line is 1, then $a_{i,j} = 1$. Similarly, if the $j$-th character on the $i$-th line is 0, then $a_{i,j} = 0$.


-----Output-----

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


-----Examples-----
Input
3 3
101
001
110

Output
2
Input
7 15
000100001010010
100111010110001
101101111100100
010000111111010
111010010100001
000011001111101
111111011010011

Output
-1


-----Note-----

In the first case, changing $a_{1,1}$ to $0$ and $a_{2,2}$ to $1$ 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 $1$ or $0$.  A sub-matrix is described by $4$ parameters — $r_1$, $r_2$, $c_1$, and $c_2$; here, $1 \leq r_1 \leq r_2 \leq n$ and $1 \leq c_1 \leq c_2 \leq m$.  This sub-matrix contains all elements $a_{i,j}$ that satisfy both $r_1 \leq i \leq r_2$ and $c_1 \leq j \leq c_2$.  A sub-matrix is, further, called an even length square if $r_2-r_1 = c_2-c_1$ and $r_2-r_1+1$ is divisible by $2$.

Difficulty: interview

Starter code:

Solutions:
import sys
input = sys.stdin.readline

n, m = list(map(int, input().split()))
l = []
for _ in range(n):
    l.append(input().strip())

def witch(s):
    out = 0
    if s[0] != s[1]:
        out += 2
    if s[1] != s[2]:
        out += 1
    return out

if n >= 4 and m >= 4:
    print(-1)
else:
    if n < m:
        n, m = m, n
        l = [''.join([l[j][i] for j in range(m)]) for i in range(n)]
    if m == 1:
        print(0)
    elif m == 2:
        even = 0
        odd = 0
        first = l.pop(0)
        if first == '00' or first == '11':
            odd += 1
        else:
            even += 1
        for nex in l:
            if nex == '00' or nex == '11':
                odd, even = even + 1, odd
            else:
                odd, even = even, odd + 1
        print(min(even, odd))
    elif m == 3:
        #ee, eo, oe, oo = [0, 0, 0 ,0]
        ll = [0, 0, 0, 0]
        for nex in l:
            ll.reverse()
            ll[witch(nex)] += 1
        print(n - max(ll))
        
        


from collections import *
from sys import stdin, stderr
def rl():
    return [int(w) for w in stdin.readline().split()]

n, m = rl()
if n > 3:
    print(-1)
    return
if n < 2:
    print(0)
    return
a = [[int(c) for c in stdin.readline().rstrip()] for _ in range(n)]
if n == 2:
    a.append(a[0])
r = n * m
for target in range(4):
    total = 0
    for a0, a1, a2 in zip(*a):
        if a0 ^ a1 != target & 1 or a1 ^ a2 != target >> 1:
            total += 1
        target ^= 3
    if total < r:
        r = total
print(r)


from sys import stdin
input = stdin.readline

n, m = map(int, input().split())
a = []

for x in range(n):
    a.append(input().rstrip())

if n == 1:
    print(0)
elif n >= 4:
    print(-1)
elif n == 2:
    cnt1, cnt2 = 0, 0
    
    for x in range(m):
        if x % 2 == 0:
            cnt1 += (a[0][x] == a[1][x])
            cnt2 += (a[0][x] != a[1][x])
        else:
            cnt1 += (a[0][x] != a[1][x])
            cnt2 += (a[0][x] == a[1][x])

    print(min(cnt1, cnt2))

elif n == 3:
    cnt1, cnt2, cnt3, cnt4 = 0, 0, 0, 0

    for x in range(m):
        if x % 2 == 0:
            if a[0][x] == a[1][x] == a[2][x]:
                cnt1 += 0
            else:
                cnt1 += 1
            if a[0][x] == a[2][x] and a[0][x] != a[1][x]:
                cnt2 += 0
            else:
                cnt2 += 1
        else:
            if a[0][x] == a[2][x] and a[0][x] != a[1][x]:
                cnt1 += 0
            else:
                cnt1 += 1
            if a[0][x] == a[1][x] == a[2][x]:
                cnt2 += 0
            else:
                cnt2 += 1

        if x % 2 == 0:
            if a[0][x] == a[1][x] and a[0][x] != a[2][x]:
                cnt3 += 0
            else:
                cnt3 += 1
            if a[1][x] == a[2][x] and a[0][x] != a[1][x]:
                cnt4 += 0
            else:
                cnt4 += 1
        else:
            if a[1][x] == a[2][x] and a[0][x] != a[1][x]:
                cnt3 += 0
            else:
                cnt3 += 1
            if a[0][x] == a[1][x] and a[0][x] != a[2][x]:
                cnt4 += 0
            else:
                cnt4 += 1
                
    print(min(cnt1, cnt2, cnt3, cnt4))

import sys
input = sys.stdin.readline
n,m=map(int,input().split())
matrix=[0]*n
for i in range(n):
    line=input()
    matrix[i]=[0]*m
    for j in range(m):
        matrix[i][j]=int(line[j])
if n>m:
    matrix2=[0]*m
    for i in range(m):
        matrix2[i]=[0]*n
        for j in range(n):
            matrix2[i][j]=matrix[i][j]
    n,m=m,n
    matrix=[0]*n
    for i in range(n):
        matrix[i]=[0]*m
        for j in range(m):
            matrix[i][j]=matrix2[i][j]
if n>3:
    print(-1)
else:
    if n==1:
        print(0)
    else:
        if n==2:
            poss=[[0,0],[1,0],[1,1],[0,1]]
            costs=[0]*4
            for i in range(m):
                newcosts=[0]*4
                for p in range(4):
                    bc=0
                    for foo in range(2):
                        if poss[p][foo]!=matrix[foo][i]:
                            bc+=1
                    newcosts[p]=min(costs[p-1],costs[(p+1)%4])
                    newcosts[p]+=bc
                for p in range(4):
                    costs[p]=newcosts[p]
            print(min(costs))
        else:
            poss1=[[0,0,0],[0,1,0],[1,1,1],[1,0,1]]
            poss2=[[1,0,0],[1,1,0],[0,1,1],[0,0,1]]
            costs1=[0]*4
            costs2=[0]*4
            for i in range(m):
                newcosts1=[0]*4
                newcosts2=[0]*4
                for p in range(4):
                    bc1=0
                    bc2=0
                    for foo in range(3):
                        if poss1[p][foo]!=matrix[foo][i]:
                            bc1+=1
                        if poss2[p][foo]!=matrix[foo][i]:
                            bc2+=1
                    newcosts1[p]=min(costs1[p-1],costs1[(p+1)%4])
                    newcosts1[p]+=bc1
                    newcosts2[p]=min(costs2[p-1],costs2[(p+1)%4])
                    newcosts2[p]+=bc2
                for p in range(4):
                    costs1[p]=newcosts1[p]
                    costs2[p]=newcosts2[p]
            print(min(min(costs1),min(costs2)))

import itertools

n, m = list(map(int, input().split()))

rows = []

for _ in range(n):
    rows.append(list(map(int, input())))

#if m < n:
#    rows = [list(rows[i][j] for i in range(n)) for j in range(m)]

# width m
# height n
# n <= m

if m >= 4 and n>=4:
    print(-1)
    return
    
if m==1 or n==1:
    print(0)
    return
    
if n==2 or m==2:
    if n==2:    
        pars = [(rows[0][i]+rows[1][i])%2 for i in range(m)]
    else:
        pars = [(rows[i][0]+rows[i][1])%2 for i in range(n)]
    best = 10**8
    for to_match in [itertools.cycle([0, 1]), itertools.cycle([1, 0])]:
        cost = 0
        for x, y in zip(pars, to_match):
            cost += abs(x-y)
        best = min(best, cost)
    print(best)
    return
    
# n = 3

best = 10**8
if n==3:
    vals = [((rows[0][i]+rows[1][i])%2, (rows[1][i]+rows[2][i])%2) for i in range(m)]
else:
    vals = [((rows[i][0]+rows[i][1])%2, (rows[i][1]+rows[i][2])%2) for i in range(n)]
for up, down in itertools.product([0, 1], repeat=2):
    cost = 0

    for cur_up, cur_down in vals:
        up = 1-up
        down = 1-down
        diff = abs(cur_up-up)+abs(cur_down-down)
        if diff == 2:
            diff = 1
        cost += diff
        
    best = min(best, cost)
print(best)
        


import sys
input = sys.stdin.readline

n,m = list(map(int,input().split()))
a = []

def f(x,y):
    res = 0
    if x[0] != y[0]:
        res += 1
    if x[1] != y[1]:
        res += 1
    if x[2] != y[2]:
        res += 1
    return res

if n >= 4 and m >= 4:
    for i in range(n):
        a.append(list(input()))
    print(-1)
elif n == 1 or m == 1:
    for i in range(n):
        a.append(list(input()))
    print(0)
elif n == 2 or m == 2:
    c = []
    if m == 2:
        for i in range(n):
            s = list(input())
            c.append(s.count('1')%2)
    elif n == 2:
        for i in range(n):
            a.append(list(input()))
        for i in range(m):
            tmp = 0
            if a[0][i] == '1':
                tmp += 1
            if a[1][i] == '1':
                tmp += 1
            c.append(tmp%2)
    res1 = 0
    res2 = 0
    for i in range(len(c)):
        if i%2 == c[i]:
            res1 += 1
        else:
            res2 += 1
    print(min(res1,res2))
else:
    a = []

    if n == 3:    
        b = list(input())
        c = list(input())
        d = list(input())
        for i in range(m):
            a.append(b[i]+c[i]+d[i])
    elif m == 3:
        s = input()
        a.append(s)
    r = max(n,m)
    res1 = 0
    res2 = 0
    res3 = 0
    res4 = 0
    for i in range(r):
        if i%2 == 0:
            res1 += min(f(a[i],'111'),f(a[i],'000'))
            res2 += min(f(a[i],'010'),f(a[i],'101'))
            res3 += min(f(a[i],'110'),f(a[i],'001'))
            res4 += min(f(a[i],'011'),f(a[i],'100'))
        else:
            res2 += min(f(a[i],'111'),f(a[i],'000'))
            res1 += min(f(a[i],'010'),f(a[i],'101'))
            res4 += min(f(a[i],'110'),f(a[i],'001'))
            res3 += min(f(a[i],'011'),f(a[i],'100'))
    print(min(res1,res2,res3,res4))




import math
import sys
input = sys.stdin.readline


# def solve_rec(R, moves):
#     all_good = True
#     # print('solve_rec', R, moves)
#     for i in range(1, len(R)):
#         for j in range(1, len(R[0])):
#             if not R[i][j]:
#                 all_good = False
#                 break

#     if all_good:
#         return moves

#     if moves >= 3:
#         return math.inf

#     answer = math.inf
#     for i in range(len(R)-1):
#         for j in range(len(R)-1):
#             R2 = copy.deepcopy(R)
#             R2[i][j] = not R2[i][j]
#             R2[i+1][j] = not R2[i+1][j]
#             R2[i][j+1] = not R2[i][j+1]
#             R2[i+1][j+1] = not R2[i+1][j+1]
#             answer = min(answer, solve_rec(R2, moves+1))
#     return answer


n, m = [int(_) for _ in input().split()]
M = []
for i in range(n):
    row = input()[:-1]
    M.append(row)

if n >= 4 and m >= 4:
    print(-1)
elif n == 1 or m == 1:
    print(0)
else:
    if n <= m:
        R = [[True] * (m-1) for _ in range(2)]
        for i in range(n-1):
            for j in range(m-1):
                count = 0
                if M[i][j] == '1':
                    count += 1
                if M[i+1][j+1] == '1':
                    count += 1
                if M[i+1][j] == '1':
                    count += 1
                if M[i][j+1] == '1':
                    count += 1
                if not (count % 2):
                    R[i][j] = False
    else:
        R = [[True] * (n-1) for _ in range(2)]
        for i in range(m-1):
            for j in range(n-1):
                count = 0
                if M[j][i] == '1':
                    count += 1
                if M[j+1][i+1] == '1':
                    count += 1
                if M[j+1][i] == '1':
                    count += 1
                if M[j][i+1] == '1':
                    count += 1
                if not (count % 2):
                    R[i][j] = False

    mnm = max(n, m)
    dp_all_good = [math.inf] * (mnm)
    dp_all_bad = [math.inf] * (mnm)
    dp_top_bad = [math.inf] * (mnm)
    dp_bot_bad = [math.inf] * (mnm)

    dp_all_good[0] = 0
    dp_all_bad[0] = 0
    dp_top_bad[0] = 0
    dp_bot_bad[0] = 0

    for i in range(1, len(R[0])+1):
        if R[0][i-1] and R[1][i-1]:
            dp_all_good[i] = min(dp_all_good[i], dp_all_good[i-1])
            dp_all_bad[i] = min(dp_all_bad[i], dp_all_bad[i-1]+1)
            dp_top_bad[i] = min(dp_top_bad[i], dp_top_bad[i-1]+1)
            dp_bot_bad[i] = min(dp_bot_bad[i], dp_bot_bad[i-1]+1)
        if (not R[0][i-1]) and (not R[1][i-1]):
            dp_all_good[i] = min(dp_all_good[i], dp_all_bad[i-1]+1)
            dp_all_bad[i] = min(dp_all_bad[i], dp_all_good[i-1])
            dp_top_bad[i] = min(dp_top_bad[i], dp_bot_bad[i-1]+1)
            dp_bot_bad[i] = min(dp_bot_bad[i], dp_top_bad[i-1]+1)
        if R[0][i-1] and (not R[1][i-1]):
            dp_all_good[i] = min(dp_all_good[i], dp_bot_bad[i-1]+1)
            dp_all_bad[i] = min(dp_all_bad[i], dp_top_bad[i-1]+1)
            dp_top_bad[i] = min(dp_top_bad[i], dp_all_bad[i-1]+1)
            dp_bot_bad[i] = min(dp_bot_bad[i], dp_all_good[i-1])
        if (not R[0][i-1]) and R[1][i-1]:
            dp_all_good[i] = min(dp_all_good[i], dp_top_bad[i-1]+1)
            dp_all_bad[i] = min(dp_all_bad[i], dp_bot_bad[i-1]+1)
            dp_top_bad[i] = min(dp_top_bad[i], dp_all_good[i-1])
            dp_bot_bad[i] = min(dp_bot_bad[i], dp_all_bad[i-1]+1)

    # print(R)
    # print(dp_all_good)
    # print(dp_all_bad)
    # print(dp_top_bad)
    # print(dp_bot_bad)

    print(min(dp_all_good[mnm-1], dp_all_bad[mnm-1]+1, dp_bot_bad[mnm-1]+1, dp_top_bad[mnm-1]+1))