#include using namespace std; struct Unionfind { // tree number vector par; // constructor Unionfind(int n = 1) : par(n, -1) {} // search root int root(int x) { if (par[x] < 0) return x; return par[x] = root(par[x]); } // is same? bool issame(int x, int y) { return root(x) == root(y); } // add // already added, return 0 bool uni(int x, int y) { x = root(x); y = root(y); if (x == y) return 0; if (par[x] > par[y]) swap(x, y); par[x] += par[y]; par[y] = x; return 1; } int size(int x) { return -par[root(x)]; } }; int n, m; Unionfind uf; vector v; int main() { cin >> n >> m; uf = Unionfind(m); v.assign(n, -1); int res = 0; for (int i = 0; i < n; ++i) { int b, c; cin >> b >> c; --b, --c; if (v[c] >= 0) { if (!uf.issame(b, v[c])) ++res, uf.uni(b, v[c]); } else v[c] = b; } cout << res << endl; return 0; }