import numpy as np n,m=map(int,input().split()) z=np.zeros([m,n]) for i in range(n): b,c=map(int,input().split()) z[b-1][c-1]+=1 for i in range(n): nonzero=np.where(z[:,i]!=0)[0] if(len(nonzero)!=0): p=nonzero[0] nonzero_r=nonzero[::-1][:-1] for idx in nonzero_r: z[p]+=z[idx] z=np.delete(z,idx,0) print(m-z.shape[0])