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: Time Limit Exceeded)

#include<bits/stdc++.h>
#define fo(i, x, y) for(int i = x, B = y; i <= B; i ++)
#define ff(i, x, y) for(int i = x, B = y; i <  B; i ++)
#define fd(i, x, y) for(int i = x, B = y; i >= B; i --)
#define ll long long
#define pp printf
using namespace std;

const int M = 1e6 + 5;
int fi[M], nt[M], to[M], r[M], tot = 1;
int S, T, co[M], d[M], cur[M];
void link(int x, int y, int z) {
	nt[++ tot] = fi[x], to[tot] = y, r[tot] = z, fi[x] = tot;
	nt[++ tot] = fi[y], to[tot] = x, r[tot] = 0, fi[y] = tot;
}
int dfs(int x, int flow) {
	if(x == T) return flow;
	int use = 0;
	for(int i = cur[x]; i; i = nt[i], cur[x] = i)
		if(d[x] == d[to[i]] + 1 && r[i]) {
			int t = dfs(to[i], min(flow - use, r[i]));
			r[i] -= t, r[i ^ 1] += t, use += t;
			if(use == flow) return use;
		}
	cur[x] = fi[x];
	if(!(-- co[d[x]])) d[S] = T;
	++ co[++ d[x]];
	return use;
}

const int N = 1e5 + 5;

int n, p[N], q[N], bp[N], bq[N];
int ans;

int main() {
	scanf("%d", &n);
	fo(i, 1, n) scanf("%d", &p[i]), p[i] ++;
	fo(i, 1, n) scanf("%d", &q[i]), q[i] ++;
	fo(i, 1, n) if(!bp[i]) {
		int x = i;
		do
			bp[x] = i, x = p[x];
		while(x != i);
	}
	fo(i, 1, n) if(!bq[i]) {
		int x = i;
		do
			bq[x] = i, x = q[x];
		while(x != i);
	}
	fo(i, 1, n) bq[i] += n;
	S = 2 * n + 1, T = S + 1;
	ans = n;
	fo(i, 1, n) {
		if(p[i] == i && q[i] == i) ans --; else
		if(p[i] == i) link(S, bq[i], 1); else
		if(q[i] == i) link(bp[i], T, 1); else {
			link(bp[i], bq[i], 1);
			if(p[i] == q[i]) link(bq[i], bp[i], 1);
		}
	}
	while(d[S] < T) ans -= dfs(S, 1 << 30);
	pp("%d\n", ans);
}