#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int N=1e6+10;
int w[N],ne[N],la[N],len[N],t=1,ans,S,T;
void alink(int x,int y,int z){
	w[++t]=y;
	ne[t]=la[x];
	la[x]=t;
	len[t]=z;
}
void link(int x,int y,int z){
	alink(x,y,z);
	alink(y,x,0);
}
int n,p[N],q[N],ip[N],iq[N],dep[N],cur[N];
int bfs(){
	static int d[N],vis[N],l=0,r=1,ti=0;
	l=0,r=1;
	vis[S]=++ti;
	d[1]=S;
	while (l<r){
		int x=d[++l];
		cur[x]=la[x];
		for (int y=la[x];y;y=ne[y]){
			int z=w[y];
			if (len[y]&&vis[z]!=ti){
				vis[z]=ti;
				d[++r]=z;
				dep[z]=dep[x]+1;
			}
		}
	}
	return vis[T]==ti;
}
int flow(int x,int val){
	if (x==T)return val;
	int v1=val;
	for (int y=cur[x];y;y=ne[y],cur[x]=y){
		int z=w[y];
		if (dep[z]!=dep[x]+1||!len[y])continue;
		int use=flow(z,min(val,len[y]));
		val-=use;
		len[y]-=use;
		len[y^1]+=use;
		if (!val)return v1;
	}return v1-val;
}
int main(){
	cin>>n;
	for (int i=1;i<=n;i++)scanf("%d",&p[i]),p[i]++;
	for (int i=1;i<=n;i++)scanf("%d",&q[i]),q[i]++;
	int t1=0;
	for (int i=1;i<=n;i++){
		if (!ip[i]){
			if (p[i]==i)continue;
			ip[i]=++t1;
			for (int x=p[i];x-i;x=p[x])ip[x]=ip[i];
		}
	}
	for (int i=1;i<=n;i++){
		if (!iq[i]){
			if (q[i]==i)continue;
			iq[i]=++t1;
			for (int x=q[i];x-i;x=q[x])iq[x]=iq[i];
		}
	}S=t1+1,T=S+1;
	ans=t1+n;
	for (int i=1;i<=t1;i++){
		link(S,i,1);
		link(i,T,1);
	}
	for (int i=1;i<=n;i++){
		if (p[i]==q[i]){
			if (p[i]==i){
				ans--;
				continue;
			}
			link(ip[i],iq[i],1);
		}
		if (p[i]==i){
			link(iq[i],T,1);
			continue;
		}
		if (q[i]==i){
			link(S,ip[i],1);
			continue;
		}
		link(iq[i],ip[i],1);
	}
	while (bfs())
		ans-=flow(S,1<<30);
	cout<<ans<<endl;
}