#include using namespace std; #define endl '\n' #define ALL(a) (a).begin(),(a).end() #define ALLR(a) (a).rbegin(),(a).rend() #define spa << " " << #define test cout<<"test"<= (ll)(n); i--) using ll = long long; using ld = long double; const ll MOD = 1e9+7; //const ll MOD = 998244353; const ll INF = 1e18; using P = pair; template void chmin(T &a,T b){if(a>b)a=b;} template void chmax(T &a,T b){if(a void ans(bool x,T1 y,T2 z){if(x)cout< void debug(vector>&v,ll h,ll w){for(ll i=0;i&v,ll h,ll w){for(ll i=0;i void debug(vector&v,ll n){if(n!=0)cout< vector>vec(ll x, ll y, T w){ vector>v(x,vector(y,w));return v;} ll gcd(ll x,ll y){ll r;while(y!=0&&(r=x%y)!=0){x=y;y=r;}return y==0?x:y;} vectordx={1,0,-1,0,1,1,-1,-1}; vectordy={0,1,0,-1,1,-1,1,-1}; template vector make_v(size_t a,T b){return vector(a,b);} template auto make_v(size_t a,Ts... ts){ return vector(a,make_v(ts...)); } struct UnionFind { vector data; UnionFind(ll size) : data(size, -1) { } bool unite(ll x, ll 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 find(ll x, ll y) { return root(x) == root(y); } ll root(ll x) { return data[x] < 0 ? x : data[x] = root(data[x]); } ll size(ll x) { return -data[root(x)]; } }; template map compress(vector v){ auto t=v; sort(ALL(t)); t.erase(unique(ALL(t)),t.end()); map ret; for(ll i=0;i<(ll)t.size();i++) ret[t[i]]=i; return ret; } int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); //cout<>n>>m; vectora(n),b(n); vectorp; rep(i,0,n){ cin>>a[i]>>b[i]; p.PB(a[i]); p.PB(b[i]); } auto mp=compress(p); UnionFind uf(mp.size()); rep(i,0,n){ uf.unite(mp[a[i]],mp[b[i]]); } vectornum(mp.size()); rep(i,0,n){ num[uf.root(mp[a[i]])]++; } for(auto z:mp){ res+=min(uf.size(z.se),num[z.se]); } cout<