#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cassert>
#include<cstring>
using namespace std;

const int INF = 1e7;

namespace NetFlow
{
	const int N = 2e5+2;
	const int M = 4e5;
	struct Edge
	{
		int v,w,nxt,rev;
	} e[(M<<1)+3];
	int fe[N+3];
	int te[N+3];
	int dep[N+3];
	int que[N+3];
	int n,en,s,t;
	int dfscnt;
	void addedge(int u,int v,int w)
	{
		assert(u!=v);
//		printf("addedge %d %d %d\n",u,v,w);
		en++; e[en].v = v; e[en].w = w;
		e[en].nxt = fe[u]; fe[u] = en; e[en].rev = en+1;
		en++; e[en].v = u; e[en].w = 0;
		e[en].nxt = fe[v]; fe[v] = en; e[en].rev = en-1;
	}
	bool bfs()
	{
		for(int i=1; i<=n; i++) dep[i] = 0;
		int head = 1,tail = 1; que[1] = s; dep[s] = 1;
		while(head<=tail)
		{
			int u = que[head]; head++;
			for(int i=fe[u]; i; i=e[i].nxt)
			{
				int v = e[i].v;
				if(e[i].w>0 && dep[v]==0)
				{
					dep[v] = dep[u]+1;
					if(v==t)return true;
					tail++; que[tail] = v;
				}
			}
		}
		return false;
	}
	int dfs(int u,int cur)
	{
		if(u==t||cur==0) {return cur;}
		int rst = cur;
		for(int &i=te[u]; i; i=e[i].nxt)
		{
			dfscnt++; assert(dfscnt<=500000000);
			int v = e[i].v;
//			printf("dfs %d %d\n",u,v);
			if(e[i].w>0 && rst>0 && dep[v]==dep[u]+1)
			{
				int flow = dfs(v,min(rst,e[i].w));
				if(flow>0)
				{
					e[i].w -= flow;	
					rst -= flow;
					e[e[i].rev].w += flow;
					if(rst==0) {return cur;}
				}
				if(e[i].w>0) {te[u] = i;}
			}
		}
		if(rst==cur) {dep[u] = -2;}
		return cur-rst;
	}
	int dinic(int _n,int _s,int _t)
	{
		n = _n,s = _s,t = _t;
		assert(n<=200002&&en<=400000);
		int ret = 0;
		while(bfs())
		{
			for(int i=1; i<=n; i++) te[i] = fe[i];
			memcpy(te,fe,sizeof(int)*(n+1));
//			puts("I AK IOI");
//			dfscnt = 0;
			ret += dfs(s,INF);
//			return 0;
		}
		return ret;
	}
}
using NetFlow::addedge;
using NetFlow::dinic;

const int N = 1e5;
int a[N+3],b[N+3];
int ca[N+3],cb[N+3];
int n,cnta,cntb;

int main()
{
	scanf("%d",&n);
	for(int i=1; i<=n; i++) scanf("%d",&a[i]),a[i]++;
	for(int i=1; i<=n; i++) scanf("%d",&b[i]),b[i]++;
	for(int i=1; i<=n; i++)
	{
		if(!ca[i])
		{
			cnta++;
			int x = i;
			do
			{
				ca[x] = cnta;
				x = a[x];
			} while(x!=i);
		}
	}
	for(int i=1; i<=n; i++)
	{
		if(!cb[i])
		{
			cntb++;
			int x = i;
			do
			{
				cb[x] = cntb;
				x = b[x];
			} while(x!=i);
		}
	}
//	printf("ca: "); for(int i=1; i<=n; i++) printf("%d ",ca[i]); puts("");
//	printf("cb: "); for(int i=1; i<=n; i++) printf("%d ",cb[i]); puts("");
	int ans = n;
	for(int i=1; i<=n; i++)
	{
		if(a[i]==i && b[i]==i) {ans--;}
		else if(a[i]==i) {addedge(n+n+1,cb[i]+n,1);}
		else if(b[i]==i) {addedge(ca[i],n+n+2,1);}
		else
		{
			addedge(ca[i],cb[i]+n,1);
			if(a[i]==b[i]) {addedge(cb[i]+n,ca[i],1);}
		}
	}
	/*
	for(int i=1; i<=n; i++)
	{
		if(a[i]==i && b[i]==i) {ans--;}
		else if(a[i]==i) {addedge(1,cb[i]+cnta+2,1);}
		else if(b[i]==i) {addedge(ca[i]+2,2,1);}
		else
		{
			addedge(ca[i]+2,cb[i]+cnta+2,1);
			if(a[i]==b[i]) {addedge(cb[i]+cnta+2,ca[i]+2,1);}
		}
	}
	*/
	/*
	for(int i=1; i<=n; i++)
	{
		if(a[i]==i && b[i]==i) {ans--;}
		else if(a[i]==i) {addedge(cb[i]+cnta+2,2,1);}
		else if(b[i]==i) {addedge(1,ca[i]+2,1);}
		else
		{
			addedge(cb[i]+cnta+2,ca[i]+2,1);
			if(a[i]==b[i]) {addedge(ca[i]+2,cb[i]+cnta+2,1);}
		}
	}
	*/
	ans -= dinic(2+n+n,n+n+1,n+n+2);
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 12288