import sys sys.setrecursionlimit(10**7) def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) class FordFulkerson(): def __init__(self,N): self.N = N self.G = [[] for _ in range(N)] def add(self,a,b,cap): # 頂点aから頂点bへのコストcの辺(0-indexed) forward = [b,cap,None] backward = [a,0,forward] forward[2] = backward self.G[a].append(forward) self.G[b].append(backward) def dfs(self,start,end,f): if start == end: return f used = self.used used[start] = 1 for e in self.G[start]: b,cap,backward = e if cap > 0 and used[b] == 0: d = self.dfs(b,end,min(f,cap)) if d > 0: e[1] -= d backward[1] += d return d return 0 def flow(self,start,end): flow = 0 inf = 10**9+7 f = inf while f: self.used = [0]*self.N f = self.dfs(start,end,inf) flow += f return flow H,W = MI() A = [LI() for _ in range(H)] X = [[] for _ in range(5*10**5+1)] for i in range(H): for j in range(W): a = A[i][j] X[a].append((i,j)) ans = 0 for i in range(1,5*10**5+1): if not X[i]: continue F = FordFulkerson(H+W+2) S = H+W T = H+W+1 for j in range(H): F.add(S,j,1) for k in range(H,H+W): F.add(k,T,1) for h,w in X[i]: F.add(h,H+w,1) ans += F.flow(S,T) print(ans)