import sys input = sys.stdin.readline N,M=map(int,input().split()) XY=[list(map(int,input().split())) for i in range(N)] LIST=[] for x,y in XY: LIST.append(x) LIST.append(y) LIST=sorted(set(LIST)) D={LIST[i]:i for i in range(len(LIST))} for i in range(N): XY[i][0]=D[XY[i][0]] XY[i][1]=D[XY[i][1]] # UnionFind Group = [i for i in range(len(LIST)+1)] # グループ分け Nodes = [1]*(len(LIST)+1) # 各グループのノードの数 def find(x): while Group[x] != x: x=Group[x] return x def Union(x,y): if find(x) != find(y): if Nodes[find(x)] < Nodes[find(y)]: Nodes[find(y)] += Nodes[find(x)] Nodes[find(x)] = 0 Group[find(x)] = find(y) else: Nodes[find(x)] += Nodes[find(y)] Nodes[find(y)] = 0 Group[find(y)] = find(x) for x,y in XY: Union(x,y) A=[0]*len(LIST) for x,y in XY: A[find(x)]+=1 ANS=0 for i in range(len(LIST)): if find(i)==i: ANS+=min(A[i],Nodes[i]) print(ANS)