Magnus decided to play a classic chess game. Though what he saw in his locker shocked him! His favourite chessboard got broken into 4 pieces, each of size n by n, n is always odd. And what's even worse, some squares were of wrong color. j-th square of the i-th row of k-th piece of the board has color a_{k}, i, j; 1 being black and 0 being white. 

Now Magnus wants to change color of some squares in such a way that he recolors minimum number of squares and obtained pieces form a valid chessboard. Every square has its color different to each of the neightbouring by side squares in a valid board. Its size should be 2n by 2n. You are allowed to move pieces but not allowed to rotate or flip them.


-----Input-----

The first line contains odd integer n (1 ≤ n ≤ 100) — the size of all pieces of the board. 

Then 4 segments follow, each describes one piece of the board. Each consists of n lines of n characters; j-th one of i-th line is equal to 1 if the square is black initially and 0 otherwise. Segments are separated by an empty line.


-----Output-----

Print one number — minimum number of squares Magnus should recolor to be able to obtain a valid chessboard.


-----Examples-----
Input
1
0

0

1

0

Output
1

Input
3
101
010
101

101
000
101

010
101
011

010
101
010

Output
2

Difficulty: interview

Starter code:

Solutions:
from itertools import permutations

n = int(input())

p1, _, p2, _, p3, _, p4 = [input() for _ in range(n)], input(), [input() for _ in range(n)], input(), [input() for _ in range(n)], input(), [input() for _ in range(n)]

def count(a,b,c,d):
    board = [a[i] + b[i] for i in range(n)] + [c[i] + d[i] for i in range(n)]

    res = 0
    for i in range(2*n):
        for j in range(2*n):
            clr = '1' if (i+j)%2 == 0 else '0'
            res += board[i][j] != clr
    return res

print(min(count(*p) for p in permutations([p1, p2, p3, p4])))


n = int(input())

b1 = [[int(x) for x in input()] for _ in range(n)]
input()
b2 = [[int(x) for x in input()] for _ in range(n)]
input()
b3 = [[int(x) for x in input()] for _ in range(n)]
input()
b4 = [[int(x) for x in input()] for _ in range(n)]

def cost(b):
    z = o = 0
    for i in range(n):
        for j in range(n):
            if (i+j)%2 == b[i][j]:
                z += 1
            if (i+j+1)%2 == b[i][j]:
                o += 1
    return z,o


z1,o1 = cost(b1)
z2,o2 = cost(b2)
z3,o3 = cost(b3)
z4,o4 = cost(b4)

print((min(
    z1+z2+o3+o4,
    z1+o2+z3+o4,
    z1+o2+o3+z4,
    o1+z2+z3+o4,
    o1+z2+o3+z4,
    o1+o2+z3+z4,
        )))


from itertools import permutations

def solve(TL, TR, BL, BR, s, n):
	A = []

	for i in range(n):
		X = TL[i].copy()
		X.extend(TR[i])
		A.append(X)

	for i in range(n):
		X = BL[i].copy()
		X.extend(BR[i])
		A.append(X)

	c = 0
	ans = 0

	for i in range(2 * n):
		if i % 2 == 1:
			c = s ^ 1
		else:
			c = s

		for j in range(2 * n):
			if A[i][j] != c:
				ans += 1
				A[i][j] = c

			c ^= 1

	return ans


n = int(input())

P = []

for _ in range(4):
	A = []

	for i in range(n):
		A.append(list(map(int, list(input()))))

	if _ != 3:
		input()

	P.append(A)

ans = (4 * n * n) + 1

for S in permutations(range(4)):

	for s in range(2):

		cur = solve(P[S[0]],P[S[1]],P[S[2]],P[S[3]], s, n)

		ans = min(ans, cur)

print(ans)

n = int(input())
board = [[list(map(int, list(input()))) for i in range(n)]]

for _ in range(3):
    input()

    board.append([list(map(int, list(input()))) for i in range(n)])

var_ = ['1100', '1010', '1001', '0110', '0101', '0011']

ans = 4 * n ** 2
for pat in var_:
    now = 0

    for ind, ch in enumerate(list(map(int, list(pat)))):
        b = board[ind]

        for i in range(n):
            for j in range(n):
                if (i + j + ch) % 2 != b[i][j]:
                    now += 1

    ans = min(ans, now)

print(ans)


n = int(input())

A = []
for _ in range(n):
    A.append(input())
blank = input()

B = []
for _ in range(n):
    B.append(input())
blank = input()

C = []
for _ in range(n):
    C.append(input())
blank = input()

D = []
for _ in range(n):
    D.append(input())

ret = 999999
cnt = 0
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if A[i][j] == "0":
                cnt += 1
        else:
            if A[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if B[i][j] == "0":
                cnt += 1
        else:
            if B[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if C[i][j] == "1":
                cnt += 1
        else:
            if C[i][j] == "0":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if D[i][j] == "1":
                cnt += 1
        else:
            if D[i][j] == "0":
                cnt += 1
ret = min(ret, cnt)
cnt = 0
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if A[i][j] == "0":
                cnt += 1
        else:
            if A[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if C[i][j] == "0":
                cnt += 1
        else:
            if C[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if B[i][j] == "1":
                cnt += 1
        else:
            if B[i][j] == "0":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if D[i][j] == "1":
                cnt += 1
        else:
            if D[i][j] == "0":
                cnt += 1
ret = min(ret, cnt)
cnt = 0
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if A[i][j] == "0":
                cnt += 1
        else:
            if A[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if D[i][j] == "0":
                cnt += 1
        else:
            if D[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if C[i][j] == "1":
                cnt += 1
        else:
            if C[i][j] == "0":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if B[i][j] == "1":
                cnt += 1
        else:
            if B[i][j] == "0":
                cnt += 1
ret = min(ret, cnt)
cnt = 0
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if B[i][j] == "0":
                cnt += 1
        else:
            if B[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if C[i][j] == "0":
                cnt += 1
        else:
            if C[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if A[i][j] == "1":
                cnt += 1
        else:
            if A[i][j] == "0":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if D[i][j] == "1":
                cnt += 1
        else:
            if D[i][j] == "0":
                cnt += 1
ret = min(ret, cnt)
cnt = 0
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if D[i][j] == "0":
                cnt += 1
        else:
            if D[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if B[i][j] == "0":
                cnt += 1
        else:
            if B[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if C[i][j] == "1":
                cnt += 1
        else:
            if C[i][j] == "0":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if A[i][j] == "1":
                cnt += 1
        else:
            if A[i][j] == "0":
                cnt += 1
ret = min(ret, cnt)
cnt = 0
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if C[i][j] == "0":
                cnt += 1
        else:
            if C[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if D[i][j] == "0":
                cnt += 1
        else:
            if D[i][j] == "1":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if A[i][j] == "1":
                cnt += 1
        else:
            if A[i][j] == "0":
                cnt += 1
for i in range(n):
    for j in range(n):
        if (i+j) % 2 == 0:
            if B[i][j] == "1":
                cnt += 1
        else:
            if B[i][j] == "0":
                cnt += 1
ret = min(ret, cnt)
cnt = 0
print(ret)

n = int(input())
p = [[[] for _ in range(n)] for _ in range(4)]

for i in range(4):
    for j in range(n):
        r = list(map(int, list(input())))
        p[i][j] = r
    if i < 3: 
        input()

dif = [0] * 4

for k in range(4):
    o = 0
    for i in range(n):
        for j in range(n):
            if p[k][i][j] == (n * i + j) % 2: 
                o += 1        

    dif[k] = (o, n*n - o)

res = 10 ** 19
z = sum(dif[_][1] for _ in range(4))
for k1 in range(4):
    for k2 in range(k1 + 1, 4):
        res = min(res, dif[k1][0]+dif[k2][0]+z-dif[k1][1]-dif[k2][1])

print(res)

n=int(input())
L=[]
for k in range(4):
    x=0
    for i in range(n):
        A=list(map(int,input()))
        for j in range(n):
            x+=A[j]^((i-j)%2)
    L.append([x,n**2-x])
    if k!=3:
        input()

out=4*n**2
for i in range(4):
    for j in range(i+1,4):
        p,q=list({0,1,2,3}-{i,j})
        out=min(out,L[i][0]+L[j][0]+L[p][1]+L[q][1])
print(out)


from itertools import permutations

def main():
    n = int(input())
    cb0 = [list(map(int, input())) for _ in range(n)]
    _ = input()
    cb1 = [list(map(int, input())) for _ in range(n)]
    _ = input()
    cb2 = [list(map(int, input())) for _ in range(n)]
    _ = input()
    cb3 = [list(map(int, input())) for _ in range(n)]
    print(solve(n, [cb0, cb1, cb2, cb3]))

def solve(n, cbs):
    return min(cost(n, p) for p in permutations(cbs))

def cost(n, p):
    # print(p)
    n2 = 2 * n
    p2 = [p[:2], p[2:]]
    cost1 = sum(1 for i in range(n2) for j in range(n2) if p2[i//n][j//n][i%n][j%n] == (i+j) % 2)
    cost2 = sum(1 for i in range(n2) for j in range(n2) if p2[i//n][j//n][i%n][j%n] == (i+j+1) % 2)
    return min(cost1, cost2)

main()


n = int(input())
N = [0, 0, 0, 0]
for i in range(4):
    for j in range(n):
        t = input()
        for k in range(n):
            if int(t[k]) == (j+k)%2:
                N[i] += 1
    if i != 3:
        s = input()
M = [0, 0, 0, 0]
for i in range(4):
    M[i] = (n * n) - N[i]
L = [0] * 6
L[0] = N[0] + N[1] + M[2] + M[3]
L[1] = N[0] + N[2] + M[1] + M[3]
L[2] = N[0] + N[3] + M[1] + M[2]
L[3] = N[1] + N[2] + M[0] + M[3]
L[4] = N[1] + N[3] + M[0] + M[2]
L[5] = N[2] + N[3] + M[0] + M[1]
mn = L[0]
for i in range(1, 6):
    if L[i] < mn:
        mn = L[i]
print(mn)


n = int(input())
s1 = []
for i in range(n):
    s1.append(list(map(int,input())))
input()
s2 = []
for i in range(n):
    s2.append(list(map(int,input())))
input()
s3 = []
for i in range(n):
    s3.append(list(map(int,input())))
input()
s4 = []
for i in range(n):
    s4.append(list(map(int,input())))
a = [s1,s2,s3,s4]
b = [0,1,2,3]
m = float('inf')
for i in range(4):
    b = [0,1,2,3]
    f = []
    f.append(b[i])
    b.pop(i)
    
    for j in range(3):
        d= b[:]
        f.append(d[j])
        d.pop(j)
        for k in range(2):
            e= d[:]
            #print(e)
            f.append(e[k])
            e.pop(k)
            f.append(e[0])


            se = []
            for v in f:se.append(a[v])
            fa = 0
            if n%2==0:
                for seg in se:
                    for ii in range(n):
                        for jj in range(n):
                            if (ii+jj)%2 != seg[ii][jj]:
                                fa += 1
                m = min(m,fa,4*n*n-fa)
                #print(fa)
            else:
                for seg in se[:2]:
                    for ii in range(n):
                        for jj in range(n):
                            if (ii+jj)%2 != seg[ii][jj]:
                                fa += 1
                for seg in se[2:]:
                    for ii in range(n):
                        for jj in range(n):
                            if (ii+jj)%2 == seg[ii][jj]:
                                fa += 1
             #   print(fa)
                m = min(m,fa,4*n*n-fa)
            #print(m,f)
            f.pop()
            f.pop()
        f.pop()
    f.pop()
print(m)


def read_data():
    n = int(input().strip())
    pieces = []
    for j in range(4):
        a = []
        for i in range(n):
            a.append(input().strip())
        if j<3:
            input().strip()
        pieces.append(a)
    return n, pieces

def solve():
    counts = []
    for piece in pieces:
        cur = 0
        sum = 0
        for i in range(n):
            for j in range(n):
                if ord(piece[i][j])-48 == cur:
                    sum += 1
                cur += 1
                cur %= 2
        counts.append(sum)
    max= n*n
    counts.sort()
    sum = 0
    for i in range(4):
        if i < 2:
            sum += counts[i]
        else:
            sum += max - counts[i]
    return sum

n, pieces = read_data()
print(solve())

import itertools
n = int(input())
t1 = [[] for i in range(n)]
t2 = [[] for i in range(n)]
t3 = [[] for i in range(n)]
t4 = [[] for i in range(n)]

for i in range(n):
    a = list(input())
    t1[i] = a
w = input()

for i in range(n):
    a = list(input())
    t2[i] = a
w = input()

for i in range(n):
    a = list(input())
    t3[i] = a
w = input()

for i in range(n):
    a = list(input())
    t4[i] = a

rec1, rec2 = [0, 0, 0, 0], [0, 0, 0, 0]
for i in range(n):
    if i % 2 == 0:
        p = 1
    else:
        p = 0
    for j in range(n):
        if int(t1[i][j]) != p:
            rec1[0] += 1
        else:
            rec2[0] += 1
        p ^= 1

for i in range(n):
    if i % 2 == 0:
        p = 1
    else:
        p = 0
    for j in range(n):
        if int(t2[i][j]) != p:
            rec1[1] += 1
        else:
            rec2[1] += 1
        p ^= 1

for i in range(n):
    if i % 2 == 0:
        p = 1
    else:
        p = 0
    for j in range(n):
        if int(t3[i][j]) != p:
            rec1[2] += 1
        else:
            rec2[2] += 1
        p ^= 1

for i in range(n):
    if i % 2 == 0:
        p = 1
    else:
        p = 0
    for j in range(n):
        if int(t4[i][j]) != p:
            rec1[3] += 1
        else:
            rec2[3] += 1
        p ^= 1

R = [0, 1, 2, 3]
num = float("inf")
for a, b, c, d in itertools.permutations(R, 4):
    num = min(num, rec1[a] + rec1[b] + rec2[c] + rec2[d])

print(num)


#in in[i][0], cost for if 1st sqaure is balck is stored.
n=int(input())
a=[[[0 for j in range(n)] for i in range(n)] for k in range(4)]
in1=[[0,0] for i in range(4)]

for k in range(4):
    for i in range(n):
        s=input()
        #print('ae')
        for j in range(n):
            a[k][i][j]=int(s[j])
            if(int(s[j])==(i+j)%2):
                in1[k][1]+=1
    in1[k][0]=n*n - in1[k][1]
    #k,in1[k][0])
    if(k!=3):
        s=input()
min1=10000000000

min1=min(min1,in1[0][0]+in1[1][0]+ in1[2][1] + in1[3][1])
min1=min(min1,in1[0][0]+in1[1][1]+ in1[2][0] + in1[3][1])
min1=min(min1,in1[0][0]+in1[1][1]+ in1[2][1] + in1[3][0])
min1=min(min1,in1[0][1]+in1[1][1]+ in1[2][0] + in1[3][0])
min1=min(min1,in1[0][1]+in1[1][0]+ in1[2][1] + in1[3][0])
min1=min(min1,in1[0][1]+in1[1][0]+ in1[2][0] + in1[3][1])
print(min1)

n = int(input())
brd = []
for i in range(4):
    brd.append([list(map(int, list(input()))) for _ in range(n)])
    if i < 3:
        _ = input()

ans = 4 * n**2
for i in range(16):
    if sum([(i >> j) & 1 for j in range(4)]) != 2:
        continue
    ans = min(ans, sum([abs((r + c + ((i >> j) & 1)) % 2 - brd[j][r][c]) \
                    for j in range(4) for r in range(n) for c in range(n)]))

print(ans)


n=int(input().split()[0])
#p=[[[None for i in range(n)]for i in range(n)]for i in range(4)]

def cnt(p):
    # print('Hre:')
    # print(len(p))
    # print(len(p[0]))
    # print(p)
    ans=0
    for i in range(n):
        for j in range(n):
            s=p[i]
            #print(s[j])
            if (i+j)%2==int(s[j]):
                ans+=1
    return ans
ans=[]
for i in range(4):
    p=[]
    while len(p)<n:
        s=input()
        if len(s)==0:
            continue
        p.append(s)
    ans.append(cnt(p))
    #s=input()
ans.sort()
print(ans[0]+ans[1]+n*n-ans[2]+n*n-ans[3])

n = int(input())
block = []

def flip(ch):
	return "1" if ch == "0" else "0"

for i in range(4):
	if i:
		input()
	left = "1"
	c0 = c1 = 0	
	for j in range(n):
		s = input()
		expect = left
		for c in s:
			if c == expect:
				c0 += 1
			else:
				c1 += 1
			expect = flip(expect)
		left = flip(left)
	block.append( [c0, c1] )		
	
ans = 4 * n * n
import itertools
for a, b, c, d in itertools.permutations(range(4), 4):
	v = block[a][0] + block[b][0] + block[c][1] + block[d][1]
	ans = min(ans, v)
print(ans)

#!/usr/bin/env python3

import sys

n = int(sys.stdin.readline().strip())
blks = []
for i in range(4):
	blks.append([])
	for j in range(n):
		blks[-1].append(sys.stdin.readline().strip())
	if i != 3:
		sys.stdin.readline()

# start from B
def to_BW(blk):
	n = len(blk)
	BW = [0, 0]
	for i in range(n):
		for j in range(n):
			BW[(i + j + int(blk[i][j])) % 2] += 1
	return BW

BWs = [to_BW(blk) for blk in blks]

res = 4 * n**2

for i in range(4):
	for j in range(i):
		kl = list(set(range(4)) - {i, j})
		res = min(res, BWs[i][0] + BWs[j][0] + BWs[kl[0]][1] + BWs[kl[1]][1])

print (res)


n=int(input())

l=[]
for i in range(4):
	l.append(0)
for q in range(4):
	for i in range(1,n+1):
		s=input()
		for j in range(1,n+1):
			if i%2==j%2 and s[j-1]=='1':
				l[q]+=1
			if i%2!=j%2 and s[j-1]=='0':
				l[q]+=1
	if q<3:
		z=input()

#print(l)

l.sort()
n2=n**2
count=0
count+=l[0]+l[1]
count+=2*n2-l[2]-l[3]

print(count)


n = int(input())

answer  = []

for k in range(4):
    white_problems = 0
    black_problems = 0
    for j in range(n):
        a = [int(i) for i in list(input())]
        for x in range(n):
            if ((a[x] == 1 and x % 2 == 0) or (a[x] == 0 and x % 2 == 1)) and j % 2 == 0:
                white_problems += 1
            if ((a[x] == 1 and x % 2 == 1) or (a[x] == 0 and x % 2 == 0)) and j % 2 == 0:
                black_problems += 1
            if ((a[x] == 1 and x % 2 == 0) or (a[x] == 0 and x % 2 == 1)) and j % 2 == 1:
                black_problems += 1
            if ((a[x] == 1 and x % 2 == 1) or (a[x] == 0 and x % 2 == 0)) and j % 2 == 1:
                white_problems += 1

    answer.append([white_problems, 0])
    answer.append([black_problems, 1])
    if k != 3:
        trash = input()



answer.sort()
black = 0
white = 0
ans = 0
for i in answer:
    if black < 2 and i[1] == 1:
        ans += i[0]
        black += 1
        continue
    if white < 2 and i[1] == 0:
        white += 1
        ans += i[0]
        continue
    if white == 2 and black == 2:
        break
print(ans)


def generate_board(size, color):
    board = []
    for _ in range(size):
        row = []
        for _ in range(size):
            row.append(color)
            color = 0 if color == 1 else 1
        board.append(row)
    return board

def distance_from(have, need):
    paint = 0
    for have_row, need_row in zip(have, need):
        for h, n in zip(have_row, need_row):
            if h != n:
                paint += 1
    return paint

def solution(n, pieces):
    white = generate_board(n, 0)
    black = generate_board(n, 1)
    grid = [
        (distance_from(p, white), distance_from(p, black))
        for p in pieces
    ]
    combinations = [
        [0,0,1,1],
        [0,1,0,1],
        [1,0,0,1],
        [0,1,1,0],
        [1,0,1,0],
        [1,1,0,0]
    ]

    return min(sum(
            grid[i][c]
            for i, c in enumerate(combo)
        )
        for combo in combinations
    )

def __starting_point():
    n = int(input())
    pieces = []
    for _ in range(4):
        pieces.append(list(
            list(map(int, input()))
            for _ in range(n)
        ))
        if _ < 3:
            input()
    print(solution(n, pieces))

__starting_point()

N = int(input())
a = [0,1] * N
a2 = [1,0] * N
pt1 = []
pt2 = []

cnt = 0
for j in range(N):
	arr = input()	
	for k in range(N):
		if j % 2 == 1 and int(arr[k]) == a2[k]:
			cnt += 1

		elif j % 2 == 0 and int(arr[k]) == a[k]:
			cnt += 1

	
pt1.append(cnt)
pt2.append(N*N-cnt)
	



cnt = 0
s = input()
for j in range(N):
	arr = input()
	for k in range(N):
		if j % 2 == 1 and int(arr[k]) == a2[k]:
			cnt += 1
			
		elif j % 2 == 0 and int(arr[k]) == a[k]:
			cnt += 1

pt1.append(cnt)
pt2.append(N*N-cnt)
s = input()
cnt = 0
for j in range(N):
	arr = input()
	for k in range(N):
		if j % 2 == 1 and int(arr[k]) == a2[k]:
			cnt += 1
			
		elif j % 2 == 0 and int(arr[k]) == a[k]:
			cnt += 1

pt1.append(cnt)
pt2.append(N*N-cnt)

s = input()

cnt = 0
for j in range(N):
	arr = input()
	for k in range(N):
		if j % 2 == 1 and int(arr[k]) == a2[k]:
			cnt += 1
			
		elif j % 2 == 0 and int(arr[k]) == a[k]:
			cnt += 1

pt1.append(cnt)
pt2.append(N*N-cnt)

pt1.sort()
pt2.sort()
print(pt1[0]+pt1[1]+pt2[0]+pt2[1])



from itertools import permutations

n = int(input())

boards = []
for i in range(4):
    board = [input() for j in range(n)]
    boards.append(board)
    if i != 3:
        input()

perms = list(permutations(list(range(4))))


def cost(board):

    base1 = [1,0]*(len(board)//2)
    base2 = [0,1]*(len(board)//2)

    board1 = []
    board2 = []
    for i in range(len(board)//2):
        board1.append(base1)
        board1.append(base2)

        board2.append(base2)
        board2.append(base1)

    board = [list(map(int, list(a))) for a in board]

    curr_min_cost1 = 0
    curr_min_cost2 = 0
    for i in range(len(board)):
        for j in range(len(board)):
            curr_min_cost1 += board[i][j] ^ board1[i][j]
            curr_min_cost2 += board[i][j] ^ board2[i][j]

    return min(curr_min_cost1, curr_min_cost2)


min_cost = float('inf')
for p in perms:
    b1 = boards[p[0]]+boards[p[1]]
    b2 = boards[p[2]]+boards[p[3]]
    b = [a+b for a,b in zip(b1, b2)]
    min_cost = min(min_cost, cost(b))

print(min_cost)


n = int(input())
row1 = [0, 1] * (n // 2) + [0]
row2 = [1, 0] * (n // 2) + [1]

d = []
for i in range(4):
    d.append([0, 0])
    for j in range(n):
        row = [int(x) for x in input()]
        for k in range(n):
            # print(i, j, k)
            if j % 2 == 0:
                d[i][0] += abs(row1[k] - row[k])
                d[i][1] += abs(row2[k] - row[k])
            else:
                d[i][1] += abs(row1[k] - row[k])
                d[i][0] += abs(row2[k] - row[k])
    if i < 3:
        input()

# min_ = min([(d[0][0] + d[1][0] + d[2])])
m = [0] * 6
m[0] = d[0][0] + d[1][0] + d[2][1] + d[3][1]
m[1] = d[0][0] + d[1][1] + d[2][0] + d[3][1]
m[2] = d[0][0] + d[1][1] + d[2][1] + d[3][0]
m[3] = d[0][1] + d[1][0] + d[2][0] + d[3][1]
m[4] = d[0][1] + d[1][0] + d[2][1] + d[3][0]
m[5] = d[0][1] + d[1][1] + d[2][0] + d[3][0]


print(min(m))


"""
1
0

0

1

0
"""

import sys

n = int(input())


def read_board(n, numb):
    board = []
    for i in range(n):
        line = input()
        board.append([int(c) for c in line])
    # board = [[int(c) for c in input().strip()] for i in range(n)]
    if numb != 3:
        input()
    return board

boards = [read_board(n, i) for i in range(4)]
# a = [[1, 0, 1], [0, 0, 0], [0, 0, 1]]
# print(boards)
def _calc_line(l, start=1):
    cur = start
    errors = 0
    for cell in l:
        errors += cell != cur
        cur = not cur
    return errors

def _calc_board(b, start=1):
    errors = 0
    cur = start
    for line in b:
        errors += _calc_line(line, cur)
        cur = not cur
    return errors

checks = [
    (0, 0, 1, 1),
    (0, 1, 0, 1),
    (1, 0, 0, 1),
    (0, 1, 1, 0),
    (1, 0, 1, 0),
    (1, 1, 0, 0)
]

def check_all(boards):
    for check in checks:
        errs = 0
        for board, start in zip(boards, check):
            errs += _calc_board(board, start)
        yield errs

print(min(check_all(boards)))




def bubble_sort(a_list):
    changed = True
    while changed:
        changed = False
        for i in range(len(a_list)-1):
            if a_list[i] > a_list[i+1]:
                 changed = True
                 a_list[i],a_list[i+1] = a_list[i+1],a_list[i]
    return a_list

n = int(input())

str1 = ''
for i in range(n):

	str1 = str1+input()
j = input()
str2 = ''
for i in range(n):

	str2 = str2+input()
j = input()
str3 = ''
for i in range(n):

	str3 = str3+input()
str4 = ''
j = input()
for i in range(n):

	str4 = str4+input()

str1comp = "01"*(n**2)
#print()
#print(str2)
#print(str1comp)

a = 0
b = 0
c = 0
d = 0


for i in range(n**2):
	if str1[i] == str1comp[i]:
		a +=1
for i in range(n**2):
	if str2[i] == str1comp[i]:
		b +=1
for i in range(n**2):
	if str3[i] == str1comp[i]:
		c +=1
for i in range(n**2):
	if str4[i] == str1comp[i]:
		d +=1

lst2 = []
lst2.append(a)
lst2.append(b)
lst2.append(c)
lst2.append(d)



bubble_sort(lst2)

#print(lst2)

sum3 = 0

for i in range(2):
	sum3+= lst2[i]
for i in range(2,4):
	sum3+= (n**2 - lst2[i])

print(sum3)