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: Wrong Answer)

#include <bits/stdc++.h>
#define N 100005
#define ll long long
#define For(i,x,y) for(int i=(x);i<=(y);++i)
#define Rof(i,x,y) for(int i=(x);i>=(y);--i)
#define Edge(x) for(int i=head[x];i;i=e[i].nxt)
#define Cur(x) for(int &i=cur[x];i;i=e[i].nxt)
#define mset(x,y) memset(x,y,sizeof(x))
using namespace std;
const int S=0,T=200015;
int dep[N*2],cur[N*2],head[N*2],cnt=1,tot=0,p[N],q[N],_p[N],_q[N];
queue<int> Q;
struct ed{ int v,nxt,f; }e[N<<3];
void add(int u,int v,int f){
	e[++cnt]=(ed){v,head[u],f},head[u]=cnt;
	e[++cnt]=(ed){u,head[v],0},head[v]=cnt;
}
void dfs1(int x,int y){ if(x==y)return;_p[x]=tot;dfs1(p[x],y); }
void dfs2(int x,int y){ if(x==y)return;_q[x]=tot;dfs2(q[x],y); }
bool bfs(){
	For(i,0,T) dep[i]=0;
	dep[S]=1;Q.push(S);
	while(!Q.empty()){
		int x=Q.front();Q.pop();
		Edge(x){
			int to=e[i].v;
			if(!dep[to] && e[i].f){
				dep[to]=dep[x]+1;
				Q.push(to);
			}
		}
	}
	return dep[T];
}
int dfs(int x,int f){
	if(x==T) return f;
	int use=0,o,w;
	Cur(x){
		int to=e[i].v;
		if(e[i].f && dep[to]==dep[x]+1){
			o=min(f-use,e[i].f);
			w=dfs(to,o);
			use+=w;
			e[i].f-=w,e[i^1].f+=w;
			if(use==f) return f;
		}
	}
	return use;
}
int main(){
	int n,ans;
	scanf("%d",&n);tot=0,ans=n;
	For(i,1,n) scanf("%d",&p[i]),p[i]++;
	For(i,1,n) scanf("%d",&q[i]),q[i]++;
	For(i,1,n) if(!_p[i]) ++tot,dfs1(i,i);
	For(i,1,n) if(!_q[i]) ++tot,dfs2(i,i);
	For(i,1,n){
		if(p[i]==i && q[i]==i) ans--;
		else{
			if(p[i]==i && q[i]!=i) add(S,_q[i],1);
			else if(p[i]!=i && q[i]==i) add(_p[i],T,1);
			else{
				add(_p[i],_q[i],1);
				if(p[i]==q[i]) add(_q[i],_p[i],1);
			}
		}
	}
	while(bfs()){
		For(i,0,T) cur[i]=head[i];
		ans-=dfs(S,2001010);
	}
	printf("%d\n",ans);
}