import sys read=sys.stdin.buffer.read readline=sys.stdin.buffer.readline readlines=sys.stdin.buffer.readlines n, m=map(int, readline().split()) c=[[[] for _ in range(n)] for _ in range(m)] for i in range(m): for j in range(n): c[i][j]=list(map(int, readline().split())) ans=2*n import itertools def myon(d): res=[set() for i in range(2*n+2)] for i, di in enumerate(d): res[i]=set(di) for j in range(n): for i in range(n): res[j+n].add(d[i][j]) for i in range(n): res[2*n].add(d[i][i]) res[2*n+1].add(d[i][n-1-i]) return res s=[myon(ci) for ci in c] for si, sj in itertools.combinations(s, 2): for di, dj in itertools.product(si, sj): ans=min(ans, len(di|dj)) print(ans-1)