#include<bits/stdc++.h>
using namespace std;
 
#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         cout<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define pw(b,p)         pow(b,p) + 0.1
#define endl            "\n"
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());


void __print(int32_t x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 

 
 
void fastIO()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
}

int power(int a,int b,int md)
{
    int ans=1;

    while(b)
    {
        if(b&1) ans*=a;

        ans%=md;
        a*=a;
        a%=md;
        b>>=1;
    }

    return ans;
}

int find(map<int,int> &mp,int x)
{
    //debug("T",x);
    if(!mp.count(x)) return x;
    if(mp[x]==x) return x;
    return mp[x]=find(mp,mp[x]);
}

void solve()
{
    int n;
    cin>>n;

    map<int,int> mp;

    for(int i=0;i<n;i++)
    {
        int x,y;
        cin>>x>>y;
        //debug(mp);
        //debug(i);
        if(!mp.count(x)&&!mp.count(y)) mp[min(x,y)]=max(x,y);
        else if(!mp.count(x)) mp[x]=find(mp,y);
        else if(!mp.count(y)) mp[y]=find(mp,x);
        else
        {
           // debug("J",mp);
            int px=find(mp,x),py=find(mp,y);

            if(!mp.count(px)) 
            {
                mp[px]=py;
            }
            else if(!mp.count(py))
            {
                mp[py]=px;
            }

        }
       // debug(mp);
    }
    mp.erase(0);
    cout<<mp.size();
}
 
int32_t main()
{
    fastIO();
    //w(t)
    {
        solve();
        cout<<endl;
    }
    return 0;
}
/*  -*- coding: utf-8 -*-
 *
 * b.cc: B - Reversible Cards
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 200000;
const int MAX_M = 400000;

/* typedef */

typedef vector<int> vi;

/* global variables */

vi nbrs[MAX_M];
bool used[MAX_M];
int ps[MAX_M], cis[MAX_M];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);

  int m = 0;
  for (int i = 0; i < n; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    a--, b--;
    nbrs[a].push_back(b);
    nbrs[b].push_back(a);
    m = max(m, max(a, b));
  }
  m++;
  //printf("m=%d\n", m);

  int sum = 0;
  for (int st = 0; st < m; st++)
    if (! used[st]) {
      used[st] = true;
      ps[st] = -1;
      int gn = 1, en = 0;

      for (int u = st; u >= 0;) {
	vi &nbru = nbrs[u];
	if (cis[u] < nbru.size() && nbru[cis[u]] == ps[u]) cis[u]++;
	if (cis[u] < nbru.size()) {
	  int v = nbru[cis[u]++];
	  en++;
	  if (! used[v]) {
	    used[v] = true;
	    ps[v] = u;
	    gn++;
	    u = v;
	  }
	}
	else {
	  u = ps[u];
	}
      }
      //printf("gn=%d,en=%d\n", gn, en);
      sum += min(en, gn);
    }

  printf("%d\n", sum);
  return 0;
}
