#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;

class UnionFind{
public:
    vector<ll> par,num;

    int find(ll v){
        return (par[v]==v)? v: (par[v]=find(par[v]));
    }
    explicit UnionFind(ll N):par(N),num(N,1){
        iota(par.begin(),par.end(),0);
    }
    void unite(ll u,ll v){
        u=find(u),v=find(v);
        if(u==v)return ;
        if(num[u]<num[v])swap(u,v);
        num[u] += num[v];
        par[v] = u;
    }
    bool same(ll u,ll v){
        return find(u) == find(v);
    }
    bool ispar(ll v){
        return v=find(v);
    }
    ll size(ll v){
        return num[find(v)];
    }
};
//arc111
int main(){
    //オーバーフローは大丈夫ですか？？
    cin.tie(0);ios::sync_with_stdio(false);
    int n;
    cin>>n;
    V<int> a(n),b(n);
    UnionFind uf(400000);
    V<bool> used(400000,0);
    rep(i,n){
        cin>>a[i]>>b[i];
        a[i]--;b[i]--;
        used[a[i]]=1;
        used[b[i]]=1;
        uf.unite(a[i],b[i]);
    }
    V<bool> seen(400000,0);
    V<int> cnt(400000,0);
    rep(i,n){
        int x=uf.find(a[i]);
        cnt[x]++;
    }
    int ans=0;
    rep(i,400000){
        if(!used[i]) continue;
        int x=uf.find(i);
        if(seen[x]) continue;
        seen[x]=1;
        if(uf.size(i)-cnt[x]==1) ans+=uf.size(i)-1;
        else ans+=uf.size(i);
    }
    out(ans);
}
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<int,int> Pair;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}

void Main(){
    int N; cin >> N;
    VVI edge(400400);
    REP(i,N){
        int a,b; cin >> a >> b; a--; b--;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }

    int ans = 0;
    vector<bool> vis(400400,false);
    REP(i,400400){
        if(vis[i]) continue;
        stack<Pair> s({Pair(i,0)});
        int nv = 0, ne = 0;
        while(!s.empty()){
            auto [v,ptr] = s.top(); s.pop();
            if(!vis[v]){
                vis[v] = true;
                nv++;
            }
            for(int j=ptr; j<(int)edge[v].size(); j++){
                ne++;
                if(vis[edge[v][j]]) continue;
                s.emplace(v,j+1);
                s.emplace(edge[v][j],0);
                break;
            }
        }

        ne /= 2;
        if(nv == ne+1) ans += nv-1;
        else ans += nv;
    }

    cout << ans << en;
    return;
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}