/*
ID:ACwisher
LANG:C++
TASK:
*/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=200010;
int n,x[N],y[N],fa[N<<1],e[N<<1],sum[N<<1],sumn[N<<1];
bool vis[N<<1];
int find(int x){ return (x==fa[x])?x:fa[x]=find(fa[x]); }
void merge(int x,int y){ x=find(x); y=find(y); if(x!=y) fa[x]=y; }
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdiout);
	scanf("%d",&n);
	for(int i=1;i<=400000;i++) fa[i]=i;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&x[i],&y[i]);
		merge(x[i],y[i]); vis[x[i]]=vis[y[i]]=1;
	}
	for(int i=1;i<=400000;i++)
		e[i]=find(i),sumn[e[i]]++;
	for(int i=1;i<=n;i++) sum[e[x[i]]]++;
	int ans=0;
	for(int i=1;i<=400000;i++)
		if(vis[i]&&e[i]==i)
			ans+=min(sum[i],sumn[i]);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>

#define IOS ios::sync_with_stdio(false);cin.tie(nullptr)
#define all(x) x.begin(),x.end()
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define lc(x) ((x)<<1)
#define rc(x) (((x)<<1)|1)

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double ld;

const ll LINF=1e18;
const ll MOD=1e9+7;
const int INF=0x3f3f3f3f;
const int MAXN=400050;

int a[MAXN],b[MAXN],fa[MAXN],cyc[MAXN];
int find(int x){
	return fa[x]==x?x:(fa[x]=find(fa[x]));
}
void merge(int x,int y){
	int fax=find(x),fay=find(y);
	fa[fay]=fax;
}
void solve(int T){
	int n;
	scanf("%d",&n);
	for(int i=1;i<MAXN;i++){
		fa[i]=i;
		cyc[i]=0;
	}
	int u,v,fau,fav;
	int ans=0;
	for(int i=1;i<=n;i++){
		scanf("%d%d",&u,&v);
		fau=find(u),fav=find(v);
		if(fau==fav){
			if(cyc[fau]==0){
				cyc[fau]=1;
				ans++;
			}
		}else if(cyc[fau]==0||cyc[fav]==0){
			ans++;
			fa[fav]=fau;
			cyc[fau]|=cyc[fav];
		}
	}
	printf("%d\n",ans);
}
signed main(){
	//IOS;
	int t=1;
	//scanf("%d",&t);
	for(int i=1;i<=t;i++){
		solve(i);
	}
}