#include <bits/stdc++.h>
using namespace std;

#ifdef Dhiraj
    #include "D:/dhiraj/Programming/debug.h"
#else
    #define d(...) 11
    #define cerr if(0) cerr
#endif

#define ll long long int
#define endl '\n'

template <typename T, typename U>
inline istream& operator >> (istream& in, pair<T, U>& p) {
    in >> p.first >> p.second;
    return in;
}
template <typename T>
inline istream& operator >> (istream& in, vector<T>& v) {
    for(T& x : v) in >> x;
    return in;
}

const ll MX = 4e5 + 5;

ll n, m;
vector<vector<ll>> g;
vector<ll> vis;

void clearAll()
{
    vis.assign(MX, 0);
    g.assign(MX, vector<ll>());
}

ll cc, edges;
void dfs(ll u)
{
    if(vis[u] || g[u].empty()) return;
    cc++;
    vis[u] = 1;
    for(ll v : g[u]) {
        edges++;
        dfs(v);
    }
}

void solve(int &tc)
{
    cin >> n;
    clearAll();
    
    for(ll i = 0; i < n; i++)
    {
        ll x, y;
        cin >> x >> y;
        g[x].emplace_back(y);
        g[y].emplace_back(x);
    }

    ll ans = 0;
    for(ll i = 0; i < MX; i++)
    {
        cc = edges = 0;
        dfs(i);
        edges /= 2;
        ans += min(cc, edges);
    }
    cout << ans << endl;
}

int main()
{
    #ifdef Dhiraj
        freopen("D:/dhiraj/Programming/i1.txt", "r", stdin);
        freopen("D:/dhiraj/Programming/o1.txt", "w", stdout);
        // freopen("D:/dhiraj/Programming/e1.txt", "w", stderr);
    #endif

    ios::sync_with_stdio(false);
    cin.tie(0);

    int tc = 1;
    for(int i = 1; i <= tc; i++) {
        cerr << "Case #" << i << "\n";
        solve(i);
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
int sum=0;
int dd=0;
int vg=0;
int d[400000];
vector<int> ed[400000];
int vis[400000];
void dfs(int v,int p)
{
	//cout<<v<<endl;
	sum+=ed[v].size();
	vis[v]=1;
	vg++;
	if(d[v]!=0) dd++;
	if(ed[v].size()==0) return;
	for(int i=0;i<ed[v].size();i++)
		if(vis[ed[v][i]]==0) dfs(ed[v][i],v);
}
int main() {
	int n;
	cin>>n;
		for(int i=0;i<400000;i++) {vis[i]=0;d[i]=0;}
	while(n--)
	{
		int x,y;
		cin>>x>>y;
		x--;y--;
		if(x!=y)
		{
			ed[x].push_back(y);
		    ed[y].push_back(x);
	    }
	    else d[x]++;
	}
	int ans=0;//cout<<d[110]<<endl;

	for(int i=0;i<400000;i++)
	 {
	   if(vis[i]==0)
	   { 
	   	 dfs(i,-1);
	   	 //if(i==110) cout<<dd<<endl;
	   	 if(sum/2==vg-1 && dd==0) ans+=vg-1;
	   	 else ans+=vg;
	   	 sum=0;
	   	 dd=0;
	   	 vg=0;
	   }
	 }
	 cout<<ans<<endl;
	return 0;
}