#include #include #include #include #include #include #include using namespace std; using ll = long long; struct UnionFind { UnionFind(int n) : p(n, -1) {} int root(int u) { return p[u] < 0 ? u : p[u] = root(p[u]); } int size(int u) { return -p[root(u)]; } bool same(int u, int v) { return root(u) == root(v); } void unite(int u, int v) { u = root(u); v = root(v); if (u == v) return; if (p[u] > p[v]) swap(u, v); p[u] += p[v]; p[v] = u; } vector p; }; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; UnionFind uf(m); vector d(n, -1); int r = 0; for (int i = 0; i < n; i++) { int b, c; cin >> b >> c; b--; c--; int t = d[c]; d[c] = b; if (t < 0) continue; if (uf.same(t, b)) continue; uf.unite(t, b); r++; } cout << r << endl; return 0; }