#include <bits/stdc++.h>

const int MAXN = 200010;
const int MAXE = MAXN * 20;
const int INF = 0x3f3f3f3f;
int head[MAXN], nxt[MAXE], to[MAXE], val[MAXE], tot = 1;
void addedge(int b, int e, int v, int v2 = 0) {
	nxt[++tot] = head[b]; to[head[b] = tot] = e; val[tot] = v;
	nxt[++tot] = head[e]; to[head[e] = tot] = b; val[tot] = v2;
}
int dis[MAXN], S, T, idx;
std::queue<int> q;
bool bfs() {
	memset(dis, 0, idx + 1 << 2);
	q.push(S); dis[S] = 1;
	while (!q.empty()) {
		int t = q.front(); q.pop();
		for (int i = head[t]; i; i = nxt[i])
			if (val[i] && !dis[to[i]])
				dis[to[i]] = dis[t] + 1, q.push(to[i]);
	}
	return dis[T] > 0;
}
int dinic(int u, int minv) {
	if (u == T || !minv) return minv;
	int res = 0, t;
	for (int & i = head[u]; i; i = nxt[i])
		if (val[i] && dis[to[i]] == dis[u] + 1 && (t = dinic(to[i], std::min(minv, val[i])))) {
			val[i] -= t;
			val[i ^ 1] += t;
			res += t;
			minv -= t;
			if (!minv) break;
		}
	if (!res) dis[u] = -1;
	return res;
}
int maxflow() {
	int res = 0;
	static int th[MAXN];
	memcpy(th, head, idx + 1 << 2);
	while (bfs()) {
		res += dinic(S, INF);
		memcpy(head, th, idx + 1 << 2);
	}
	return res;
}
int n, P[MAXN], Q[MAXN];
int vis[MAXN];
struct EX {
	int a, b, c, d, e, f, g;
} ds[1 << 4];
int A[MAXN], B[MAXN];
int main() {
	for (int i = 0; i != (1 << 4); ++i) {
		static int g[4];
		g[0] = i & 1, g[1] = i >> 1 & 1;
		g[2] = i >> 2 & 1, g[3] = i >> 3 & 1;
		int t = g[0] + g[1] + g[2] + g[3];
		g[0] = t - g[0];
		g[1] = t - g[1];
		g[2] = t - g[2];
		g[3] = t - g[3];
		bool can = false;
		for (int a = 0; a <= g[0]; ++a) {
			int c = g[0] - a;
			for (int b = 0; b <= g[3]; ++b) {
				int d = g[3] - b;
				int e = g[2] - a - d;
				int f = g[1] - b - c;
				if (e >= 0 && f >= 0) {
					can = true;
					ds[i] = (EX) {a, b, c, d, e, f, t};
					break;
				}
			}
		}
		if (!can) ds[i].a = -1;
	}
	std::ios_base::sync_with_stdio(false), std::cin.tie(0);
	std::cin >> n;
	for (int i = 0; i < n; ++i) std::cin >> P[i];
	for (int i = 0; i < n; ++i) std::cin >> Q[i];
	S = 0, T = idx = 1;
	for (int i = 0; i < n; ++i) if (!vis[i]) {
		int now = i; ++idx;
		while (!vis[i] || now != i) {
			A[now] = idx;
			vis[now] = true, now = P[now];
		}
	}
	memset(vis, 0, n + 1 << 2);
	for (int i = 0; i < n; ++i) if (!vis[i]) {
		int now = i; ++idx;
		while (!vis[i] || now != i) {
			B[now] = idx;
			vis[now] = true, now = Q[now];
		}
	}
	int ans = 0;
	for (int i = 0; i < n; ++i) {
		static int f[4];
		f[0] = i != Q[i];
		f[1] = P[i] != Q[i];
		f[2] = i != i;
		f[3] = P[i] != i;
		EX t = ds[f[0] | f[1] << 1 | f[2] << 2 | f[3] << 3];
		if (t.a) addedge(S, A[i], t.a);
		if (t.b) addedge(A[i], T, t.b);
		if (t.c) addedge(S, B[i], t.c);
		if (t.d) addedge(B[i], T, t.d);
		if (t.e || t.f) addedge(B[i], A[i], t.e, t.f);
		ans += t.g;
	}
	ans -= maxflow();
	std::cout << ans << std::endl;
	return 0;
}
