#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
const int N = 1e5+5;
const int INF = (1<<30)-1;

int hd[N],iter[N],dis[N];
struct E{int v,w,nxt;}e[N<<3];
queue<int>Q;
bool v[N];
int p[N],q[N],c[N],d[N];
int n,nc,nd,tot=1,S,T;

inline void add(int u,int v,int w,int w2){
	e[++tot]={v,w,hd[u]};hd[u]=tot;
	e[++tot]={u,w2,hd[v]};hd[v]=tot;
}
inline bool bfs(){
	memcpy(iter,hd,sizeof(hd));
	memset(dis,-1,sizeof(dis));
	Q.push(S);dis[S]=0;
	while(!Q.empty()){
		int x=Q.front();Q.pop();
		for(int i=hd[x],y;i;i=e[i].nxt)
			if(e[i].w&&dis[y=e[i].v]==-1){
				dis[y]=dis[x]+1, Q.push(y);
			}
	}
	return ~dis[T];
}
int dfs(int x=S,int f=INF){
	if(x==T||!f)return f;
	int sf=0,y;
	for(int &i=iter[x];i;i=e[i].nxt)
		if(e[i].w&&dis[y=e[i].v]==dis[x]+1){
			int w=dfs(y,min(f,e[i].w));
			e[i].w-=w, e[i^1].w+=w;
			f-=w, sf+=w;
			if(!f)break;
		}
	return sf;
}

void prep(int p[],int c[],int &m){
	m = 0;
	rep(i,1,n) if(!c[i]){
		++m;
		for(int x=i;!c[x];x=p[x]) c[x] = m;
	}
}

int main() {
	scanf("%d",&n);
	rep(i,1,n)scanf("%d",p+i),++p[i];
	rep(i,1,n)scanf("%d",q+i),++q[i];
	prep(p,c,nc);
	prep(q,d,nd);
	S = 0, T = nc+nd+1;
	int ans = n;
	rep(i,1,n){
		bool f1 = (p[i]==i), f2 = (q[i]==i);
		if(f1&&f2) --ans;
		else if(f1) add(nc+d[i],T,1,0);
		else if(f2) add(S,c[i],1,0);
		else add(nc+d[i],c[i],1,p[i]==q[i]);
	}
	int flow = 0;
	while(bfs()) flow += dfs();
	printf("%d\n",ans-flow);
	return 0;
}