class Union: def __init__(s, num): s.T = [-1] * num def root(s, x): if s.T[x] < 0: return x t = s.root(s.T[x]) s.T[x] = t return t def merge(s, x, y): an, bn = s.root(x), s.root(y) if an == bn: return False am, bm = -s.T[an], -s.T[bn] if am < bm: an, bn = bn, an s.T[an] += s.T[bn] s.T[bn] = an return True def size(s, x): n = s.root(x) return -s.T[n] def same(s, x, y): return s.root(x) == s.root(y) def print(s): #デバッグ用 print(*s.T) ans = [] for i in range(len(s.T)): ans.append(s.root(i)) print(*ans) N, M = list(map(int, input().split())) XY = [list(map(int, input().split())) for _ in range(N)] cnt = 0 R = {} for x, y in XY: if x not in R: R[x] = cnt cnt += 1 if y not in R: R[y] = cnt cnt += 1 D = [0] * cnt uni = Union(cnt) ans = 0 for x, y in XY: x = R[x] y = R[y] v = max(D[uni.root(x)], D[uni.root(y)]) u = min(D[uni.root(x)], D[uni.root(y)]) if uni.merge(x, y): if u != 1: ans += 1 D[uni.root(x)] = v else: t = uni.root(x) if D[t] == 0: D[t] = 1 ans += 1 print(ans)