#pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; #define REP(i, n) for (ll i = 0; i < n; i++) #define REPR(i, n) for (ll i = n; i >= 0; i--) #define FOR(i, m, n) for (ll i = m; i < n; i++) #define FORR(i, m, n) for (ll i = m; i >= n; i--) #define REPO(i, n) for (ll i = 1; i <= n; i++) #define ll long long #define INF (ll)1 << 60 #define MINF (-1 * INF) #define ALL(n) n.begin(), n.end() #define MOD 1000000007 #define P pair struct UnionFind { vector data; UnionFind(int size) : data(size, -1) { } bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; ll n, m, ans; bool used[210000]; UnionFind uf(210000); vector

s; int main(){ cin >> n >> m; REP(i, n){ ll a, b; cin >> a >> b; a--; b--; s.push_back(P(b, a)); } sort(ALL(s)); REP(i, (int)s.size() - 1){ if (s[i].first == s[i + 1].first) uf.unionSet(s[i].second, s[i + 1].second); } REP(i, m){ if(!used[uf.root(i)]){ used[uf.root(i)] = true; ans++; } } cout << m - ans << endl; }