/** author: shobonvip created: 2025.02.14 21:31:06 **/ #include using namespace std; //* ATCODER #include using namespace atcoder; typedef modint998244353 mint; //*/ /* BOOST MULTIPRECISION #include using namespace boost::multiprecision; //*/ typedef long long ll; #define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++) #define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--) #define all(v) v.begin(), v.end() template bool chmin(T &a, const T &b) { if (a <= b) return false; a = b; return true; } template bool chmax(T &a, const T &b) { if (a >= b) return false; a = b; return true; } template T max(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]); return ret; } template T min(vector &a){ assert(!a.empty()); T ret = a[0]; for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]); return ret; } template T sum(vector &a){ T ret = 0; for (int i=0; i<(int)a.size(); i++) ret += a[i]; return ret; } // defcomp template vector compress(vector &X) { vector vals = X; sort(vals.begin(), vals.end()); vals.erase(unique(vals.begin(), vals.end()), vals.end()); return vals; } // ----- // importbisect template int bisect_left(vector &X, T v){ return lower_bound(X.begin(), X.end(), v) - X.begin(); } template int bisect_right(vector &X, T v){ return upper_bound(X.begin(), X.end(), v) - X.begin(); } // ----- int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int n; ll m; cin >> n >> m; vector x(n),y(n); vector v; rep(i,0,n){ cin>>x[i]>>y[i]; v.push_back(x[i]); v.push_back(y[i]); } v = compress(v); rep(i,0,n){ x[i]=bisect_left(v,x[i]); y[i]=bisect_left(v,y[i]); } int k=v.size(); dsu uf(k); vector order(k); rep(i,0,n){ uf.merge(x[i],y[i]); } rep(i,0,n){ order[uf.leader(x[i])]++; } int ans=0; rep(i,0,k){ if(uf.leader(i)==i){ int sz = uf.size(i); ans+=min(sz,order[i]); } } cout<