#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int S=0,T=1;
struct bian{int nxt,to,f;}bi[400040];
int n,p[100010],q[100010],blp[100010],blq[100010],cnt=1,head[200020],num=1,cur[200020],dep[200020],ans;
bool vis[100010];
inline void add(int from,int to,int f){bi[++num]=bian{head[from],to,f};head[from]=num;}
inline void ADD(int from,int to,int f){add(from,to,f),add(to,from,0);}
bool bfs()
{
	queue<int>q;
	memset(dep,0,cnt+1<<2);
	memcpy(cur,head,cnt+1<<2);
	dep[S]=1,q.push(S);
	int x,y;
	while(!q.empty())
	{
		x=q.front();q.pop();
		for(int i=head[x];i;i=bi[i].nxt)
			if(bi[i].f)
			{
				y=bi[i].to;
				if(!dep[y])dep[y]=dep[x]+1,q.push(y);
			}
	}
	return dep[T]!=0;
}
int dfs(int v,int last)
{
	if(v==T)
	{
		ans-=last;
		return last;
	}
	int now=0;
	for(int &i=cur[v],u;i;i=bi[i].nxt)
	{
		u=bi[i].to;
		if(!bi[i].f||dep[u]!=dep[v]+1)continue;
		int dmin=dfs(u,min(last,bi[i].f));
		now+=dmin,last-=dmin;
		bi[i].f-=dmin,bi[i^1].f+=dmin;
		if(!last)break;
	}
	return now;
}
int main()
{
	scanf("%d",&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];
	for(int i=1,x;i<=n;++i)
		if(!vis[i])
		{
			++cnt;x=i;
			while(!vis[x])vis[x]=1,blp[x]=cnt,x=p[x];
		}
	memset(vis+1,0,n);
	for(int i=1,x;i<=n;++i)
		if(!vis[i])
		{
			++cnt;x=i;
			while(!vis[x])vis[x]=1,blq[x]=cnt,x=q[x];
		}
	for(int i=1;i<=n;++i)
	{
		if(p[i]==i&&q[i]==i)continue;
		++ans;
		if(p[i]==i)ADD(blq[i],T,1);
		else if(q[i]==i)ADD(S,blp[i],1);
		else if(p[i]==q[i])ADD(blp[i],blq[i],1),ADD(blq[i],blp[i],1);
		else ADD(blq[i],blp[i],1);
	}
	while(bfs())while(dfs(S,0x3f3f3f3f));
	printf("%d",ans);
	return 0;
}
