#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define PB push_back
#define ll long long
using namespace std;
template <class T>
inline void rd(T &x) {
	x=0; char c=getchar(); int f=1;
	while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
	while(isdigit(c)) x=x*10-'0'+c,c=getchar(); x*=f;
}
const int N=2e5+10;
int S,T,ncnt;
namespace Flow {
	int head[N],cur[N],dep[N],ecnt;
	struct ed { int to,next,f; };
	vector<ed> e;
	void init() { memset(head,-1,sizeof(head)); e.clear(); }
	void ad(int x,int y,int f) {
		e.PB((ed){y,head[x],f}); head[x]=e.size()-1;
		e.PB((ed){x,head[y],0}); head[y]=e.size()-1;
	}
	queue<int> que;
	bool bfs() {
		for(int i=1;i<=ncnt;++i) dep[i]=-1,cur[i]=head[i];
		while(!que.empty()) que.pop();
		que.push(S),dep[S]=0;
		while(!que.empty()) {
			int u=que.front(); que.pop();
			if(u==T) return 1;
			for(int k=head[u];~k;k=e[k].next) if(e[k].f) {
				int v=e[k].to;
				if(dep[v]==-1) {
					dep[v]=dep[u]+1;
					que.push(v);
				}
			}
		}
		return 0;
	}
	int dfs(int u,int f) {
		if(u==T||!f) return f; int tmp,ret=0;
		for(int &k=cur[u];~k;k=e[k].next) if(e[k].f) {
			int v=e[k].to;
			if(dep[v]==dep[u]+1&&(tmp=dfs(v,min(e[k].f,f)))) {
				e[k].f-=tmp,e[k^1].f+=tmp;
				f-=tmp,ret+=tmp;
				if(!f) break;
			}
		}
		return ret;
	}
	int work() { int ans=0; while(bfs()) ans+=dfs(S,1e9); return ans; }
}
int P[N],Q[N],n;
int bP[N],bQ[N],cntP,cntQ;
int id[N][2];
int a[N],b[N],ans;
void dfsP(int u) { if(bP[u]) return; bP[u]=cntP,dfsP(P[u]); }
void dfsQ(int u) { if(bQ[u]) return; bQ[u]=cntQ,dfsQ(Q[u]); }
int main() {
	Flow::init();
	rd(n);
	for(int i=0;i<n;++i) rd(P[i]);
	for(int i=0;i<n;++i) rd(Q[i]);
	
	for(int i=0;i<n;++i) if(!bP[i]) cntP++,dfsP(i);
	for(int i=0;i<n;++i) if(!bQ[i]) cntQ++,dfsQ(i);
	S=++ncnt,T=++ncnt;
	for(int i=1;i<=cntP;++i) id[i][0]=++ncnt;
	for(int i=1;i<=cntQ;++i) id[i][1]=++ncnt;
	
	for(int i=0;i<n;++i) {
		if(P[i]==i&&Q[i]==i) continue;
		if(P[i]==i) b[bQ[i]]++;
		else if(Q[i]==i) a[bP[i]]++;
		else if(Q[i]!=P[i]) ans++,Flow::ad(id[bP[i]][0],id[bQ[i]][1],1);
		else ans++,Flow::ad(id[bP[i]][0],id[bQ[i]][1],1),Flow::ad(id[bQ[i]][1],id[bP[i]][0],1);
	}
	for(int i=1;i<=cntP;++i) {
		ans+=a[i];
		Flow::ad(S,id[i][0],0);
		Flow::ad(id[i][0],T,a[i]);
	}
	for(int i=1;i<=cntQ;++i) {
		ans+=b[i];
		Flow::ad(S,id[i][1],b[i]);
		Flow::ad(id[i][1],T,0);
	}
	ans-=Flow::work();
	printf("%d",ans);
	return 0;
}