// Clockwork is still ticking.

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef unsigned int UI;
typedef pair<int, int> pii;

#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define fst first
#define snd second
#define SZ(u) ((int) (u).size())
#define ALL(u) (u).begin(), (u).end()

inline void proc_status()
{
	ifstream t("/proc/self/status");
	cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl;
}

namespace fastIO
{
	const UI BUFFER_SIZE = (UI) 1 << 15;
	char buffer[BUFFER_SIZE], *cur, *ed;

	inline char getc() { return *(cur == ed ? ed = buffer + fread(buffer, 1, BUFFER_SIZE, stdin), cur = buffer : cur)++; }
}
using fastIO::getc;

template<typename T> inline T read()
{
	register T sum(0), fg(1);
	register char ch(getc());
	for(; !isdigit(ch); ch = getc()) if(ch == '-') fg = -1;
	for(;  isdigit(ch); ch = getc()) sum = sum * 10 - '0' + ch;
	return sum * fg;
}

template<typename T> inline T sqr(const T &val) { return val * val; }
template<typename T> inline int chkmin(T &a, const T &b) { return b < a ? a = b, 1 : 0; }
template<typename T> inline int chkmax(T &a, const T &b) { return b > a ? a = b, 1 : 0; }

// END tpl.

const int INF = (int) 1e9;

const int MAXN = (int) 1e5;

namespace FLOW
{
	const int MAXV = MAXN * 2 + 5, MAXE = MAXN * 4 + 5;

	struct edge
	{
		int v, c, nxt;

		edge() { }
		edge(int _v, int _c, int _nxt): v(_v), c(_c), nxt(_nxt) { }
	};

	edge e[MAXE * 2 + 5];
	int st[MAXV + 5], edge_cnt = 0;

	inline void init() { memset(st, -1, sizeof st), edge_cnt = 0; }

	inline void add_edge(int u, int v, int c, int type = 1)
	{
		e[edge_cnt] = edge(v, c, st[u]), st[u] = edge_cnt++;
		if(type) add_edge(v, u, 0, 0);
	}

	int d[MAXV + 5], cur[MAXV + 5];

	inline int BFS(int S, int T)
	{
		static queue<int> q;
		memset(d, -1, sizeof d);

		d[S] = 0, q.push(S);
		while(!q.empty())
		{
			int u = q.front();
			cur[u] = st[u], q.pop();

			for(int i = st[u]; ~i; i = e[i].nxt)
			{
				int v = e[i].v;
				if(d[v] == -1 && e[i].c > 0) d[v] = d[u] + 1, q.push(v);
			}
		}

		return d[T] != -1;
	}

	inline int dfs(int u, int low, int T)
	{
		if(u == T) return low;

		int sum = 0;
		for(int &i = cur[u]; ~i; i = e[i].nxt)
		{
			int v = e[i].v, k;
			if(d[v] == d[u] + 1 && e[i].c > 0 && (k = dfs(v, min(low, e[i].c), T)) > 0)
			{
				e[i].c -= k, e[i ^ 1].c += k;
				low -= k, sum += k;
				if(low == 0) break;
			}
		}
		return sum;
	}

	inline int solve(int S, int T)
	{
		int ans = 0;
		while(BFS(S, T)) for(int k = dfs(S, INF, T); k > 0; k = dfs(S, INF, T)) ans += k;
		return ans;
	}
}

int n;

int P[MAXN + 5], Q[MAXN + 5];

inline void input()
{
	n = read<int>();
	for(int i = 1; i <= n; ++i) P[i] = read<int>() + 1;
	for(int i = 1; i <= n; ++i) Q[i] = read<int>() + 1;
}

inline void solve()
{
	FLOW::init();
	int cnt = 0, S = ++cnt, T = ++cnt;

	auto get_cir = [&] (int *p, int *id)
	{
		for(int i = 1; i <= n; ++i) id[i] = 0;

		for(int i = 1; i <= n; ++i) if(id[i] == 0)
		{
			++cnt;
			for(int j = i; !id[j]; j = p[j]) id[j] = cnt;
		}
	};

	static int p[MAXN + 5], q[MAXN + 5];

	get_cir(P, p);
	get_cir(Q, q);

	int ans = n;
	for(int i = 1; i <= n; ++i)
		if(P[i] == i && Q[i] == i) --ans;
		else if(P[i] == i) FLOW::add_edge(S, q[i], 1);
		else if(Q[i] == i) FLOW::add_edge(p[i], T, 1);
		else if(P[i] == Q[i])
		{
			FLOW::add_edge(p[i], q[i], 1);
			FLOW::add_edge(q[i], p[i], 1);
		}
		else FLOW::add_edge(p[i], q[i], 1);

	printf("%d\n", ans - FLOW::solve(S, T));
}

int main()
{
#ifdef K_ON // K-ON!
	freopen("F.in", "r", stdin);
	freopen("F.out", "w", stdout);
#endif

	input();
	solve();

	return 0;
}



Measured memory_bytes: 10112