Problem:
Score : 1600 points

Problem Statement

Snuke has permutations (P_0,P_1,\cdots,P_{N-1}) and (Q_0,Q_1,\cdots,Q_{N-1}) of (0,1,\cdots,N-1).

Now, he will make new permutations A and B of (0,1,\cdots,N-1), under the following conditions:

For each i (0 \leq i \leq N-1), A_i should be i or P_i.

For each i (0 \leq i \leq N-1), B_i should be i or Q_i.

Let us define the distance of permutations A and B as the number of indices i such that A_i \neq B_i.
Find the maximum possible distance of A and B.

Constraints

1 \leq N \leq 100000

0 \leq P_i \leq N-1

P_0,P_1,\cdots,P_{N-1} are all different.

0 \leq Q_i \leq N-1

Q_0,Q_1,\cdots,Q_{N-1} are all different.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
P_0 P_1 \cdots P_{N-1}
Q_0 Q_1 \cdots Q_{N-1}

Output

Print the maximum possible distance of A and B.

Sample Input 1

4
2 1 3 0
0 2 3 1

Sample Output 1

3

For example, if we make A=(0,1,2,3) and B=(0,2,3,1), the distance will be 3, which is the maximum result possible.

Sample Input 2

10
0 4 5 3 7 8 2 1 9 6
3 8 5 6 4 0 2 1 7 9

Sample Output 2

8

Sample Input 3

32
22 31 30 29 7 17 16 3 14 9 19 11 2 5 10 1 25 18 15 24 20 0 12 21 27 4 26 28 8 6 23 13
22 3 2 7 17 9 16 4 14 8 19 26 28 5 10 1 25 18 15 13 11 0 12 23 21 20 29 24 27 6 30 31

Sample Output 3

28

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<bits/stdc++.h>
using namespace std;
inline int gi()
{
	char c=getchar(); int x=0;
	for(;c<'0'||c>'9';c=getchar());
	for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
	return x;
}
const int N=1e5+5,M=5e5+5,inf=1<<30;
int head[N],cur[N],nxt[M],to[M],wei[M],lev[N],tot=1,s,t,n,p[N],q[N],cp[N],cq[N],tid,ans;
void addedge(int u, int v, int w=1)
{
	nxt[++tot]=head[u], head[u]=tot, to[tot]=v, wei[tot]=w;
	nxt[++tot]=head[v], head[v]=tot, to[tot]=u, wei[tot]=0;
}
bool bfs()
{
	memset(lev,-1,sizeof(lev));
	queue<int> q;
	q.push(s),lev[s]=1;
	memcpy(cur,head,sizeof(head));
	while(!q.empty())
	{
		int u=q.front(); q.pop();
		for(int e=head[u];e;e=nxt[e])
			if(lev[to[e]]==-1&&wei[e])
			{
				lev[to[e]]=lev[u]+1,q.push(to[e]);
				if(to[e]==t) return true;
			}
	}
	return false;
}
int dfs(int u, int mx)
{
	if(u==t) return mx;
	int l=mx;
	for(int& e=cur[u];l&&e;e=nxt[e])
		if(wei[e]&&lev[to[e]]==lev[u]+1)
		{
			int f=dfs(to[e],min(l,wei[e]));
			if(!f) --lev[to[e]];
			l-=f,wei[e]-=f,wei[e^1]+=f;
		}
	return mx-l;
}
int main()
{
	n=ans=gi();
	for(int i=1;i<=n;++i) p[i]=gi()+1;
	for(int i=1;i<=n;++i) q[i]=gi()+1;
	for(int i=1;i<=n;++i) if(!cp[i])
	{
		cp[i]=++tid;
		for(int j=p[i];!cp[j];j=p[j]) cp[j]=tid;
	}
	for(int i=1;i<=n;++i) if(!cq[i])
	{
		cq[i]=++tid;
		for(int j=q[i];!cq[j];j=q[j]) cq[j]=tid;
	}
	s=tid+1,t=s+1;
	for(int i=1;i<=n;++i)
	{
		if(p[i]==q[i]&&p[i]==i)--ans;
		else if(p[i]==i) addedge(cq[i],t);
		else if(q[i]==i) addedge(s,cp[i]);
		else if(p[i]!=q[i]) addedge(cq[i],cp[i]);
		else addedge(cp[i],cq[i]),addedge(cq[i],cp[i]);
	}
	while(bfs()) ans-=dfs(s,inf);
	printf("%d",ans);
}