def read_data(): N, M = map(int, input().split()) Cs = [] for m in range(M): mat = [list(map(int, input().split())) for n in range(N)] Cs.append(mat) return N, M, Cs def solve(N, M, Cs): mymax = max mysum = sum maxC = mymax(mymax(mymax(c) for c in C) for C in Cs) Ds = C2D(N, M, Cs) record = 0 bits = [0] * (maxC + 1) for i, D in enumerate(Ds): for d in D: for di in d: bits[di] = 1 for E in Ds[i + 1:]: for e in E: # counts = len([1 for ei in e if bits[ei]]) counts = mysum([bits[ei] for ei in e]) if counts > record: record = counts for di in d: bits[di] = 0 if record == N: return N - 1 return 2 * N - 1 - record def C2D(N, M, Cs): return [c2d(N, C) for C in Cs] def c2d(N, C): d = [] for c in C: d.append(c[:]) Ct = list(zip(*C)) for ct in Ct: d.append(list(ct)) diagonal1 = [C[i][i] for i in range(N)] d.append(diagonal1) diagonal2 = [C[N - 1 - i][i] for i in range(N)] d.append(diagonal2) return d N, M, Cs = read_data() print(solve(N, M, Cs))