//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template<class T> using PQ = priority_queue<T>;
template<class T> using PQG = priority_queue<T, vector<T>, greater<T> >;
const int INF = 0xccccccc;
const ll LINF = 0xcccccccccccccccLL;
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b && (a = b, true);}
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b && (a = b, true);}
template<typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) { return is >> p.first >> p.second;}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) { return os << p.first << ' ' << p.second;}

struct UnionFind {
	vector<int> par; // 親を指すvector,-par[親]は木のサイズ
	UnionFind(int n):par(n, -1) {} // uniteで親を埋め込んでいく必要あり
	int root(int x) { // 親をたどる&データの整理
		if(par[x] < 0) return x;
		return par[x] = root(par[x]);
	}
	bool unite(int x, int y) { // データの結合
		x = root(x);
		y = root(y);
		if(x == y) return false;
		if(par[x] > par[y]) swap(x, y);
		par[x] += par[y];
		par[y] = x;
		return true;
	}
	bool same(int x, int y) {return root(x) == root(y);} // 所属判定
	int size(int x) {return -par[root(x)];} // 木のサイズ
};

//head

#define N 400010

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	vi cnt(N);
	UnionFind uft(N);
	rep(i, n) {
		int a, b;
		cin >> a >> b;
		uft.unite(a, b);
		cnt[a]++;
	}
	rep(i, N) if(uft.root(i) != i) cnt[uft.root(i)] += cnt[i];
	int ans = 0;
	rep(i, N) if(uft.root(i) == i) {
		ans += min(uft.size(i), cnt[i]);
	}
	cout << ans << endl;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef pair<int, pair<int,int> > ppi;
const ll mod = 1e9+7;
const int MAXN=4e5+3;
void pvec(vi v){
    for(auto x:v){
        cout << x << " ";
    }cout << endl;
}
ll gcd(ll x, ll y){
    if(y==0){
        return x;
    }else{
        return gcd(y, x%y);
    }
}
 
ll binpow(ll a, ll b) {
    if(b==0){
        return 1;
    }
    ll res = 1;
    ll tmp = binpow(a,b/2);
    res = (tmp*tmp);
    if(b%2==1){
        res = (res*a);
    }
    return res;
}

vector<vi> ad(MAXN);
vector<bool> vis(MAXN,false), mk(MAXN,false);
vi sz(MAXN, 1);
void dfs(int u, int st, int par=0){
    vis[u] = true;
    for(int v : ad[u]){
        if(!vis[v]){
            sz[st]++;
            dfs(v, st, u);
        }else if(v != par){
            mk[st] = true;
        }
    }
}
void solve(){
    int n;
    cin>>n;
    vi gd(MAXN, 0);
    for(int i=0; i<n; i++){
        int x,y;
        cin>>x>>y;
        if(x==y){
            // mk[x] = true;
            // gd[x] = 1;
            ad[x].push_back(x);
        }else{
            ad[x].push_back(y);
            ad[y].push_back(x);
        }
    }
    int s=0;
    for(int i=1; i<MAXN; i++){
        if(!vis[i]){
            dfs(i,i);
            s += sz[i];
            if(!mk[i] && gd[i]==0) s--;
            // if(sz[i]>1) cout << i << " "  << sz[i] << "\n";
        }
    }
    cout << s << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t=1;
    // cin>>t;
    while(t--){
        solve();
    }
}