import sys input = sys.stdin.readline N, M = map(int, input().split()) S_list = [[] for _ in range(M)] for i in range(M): C = [list(map(int, input().split())) for _ in range(N)] for j in range(N): s = set() for k in range(N): s.add(C[j][k]) S_list[i].append(s) for j in range(N): s = set() for k in range(N): s.add(C[k][j]) S_list[i].append(s) s = set() for j in range(N): s.add(C[j][j]) S_list[i].append(s) s = set() for j in range(N): s.add(C[N-1-j][j]) S_list[i].append(s) max_same = 0 for i in range(M): for j in range(i+1, M): for k in range(2*N+2): for l in range(2*N+2): max_same = max(max_same, len(S_list[i][k]&S_list[j][l])) print(2*N-max_same-1)