class UnionFind def initialize(n) @size = Array.new(n, 1) @rank = Array.new(n, 0) @parent = [] (0..n).each do |i| @parent[i] = i end end def find(x) if @parent[x] == x x else @parent[x] = find(@parent[x]) end end def unite(x, y) x = find(x) y = find(y) return if x == y if @rank[x] < @rank[y] @parent[x] = y @size[y] += @size[x] else @parent[y] = x @size[x] += @size[y] @rank[x] += 1 if @rank[x] == @rank[y] end end def same?(x, y) find(x) == find(y) end def size(x) @size[find(x)] end end N, M = gets.split.map(&:to_i) Q = N.times.map { gets.split.map(&:to_i) } C = Hash.new { |h, k| h[k] = [] } Q.each do |b, c| C[c] << b end ans = 0 uf = UnionFind.new(M + 1) C.each do |c, pos_list| pos_list.each_cons(2) do |a, b| next if uf.same?(a, b) ans += 1 uf.unite(a, b) end end puts ans