#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <random>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

template<class T> class Dinic {
	struct edge {
		int to;
		T cap;
		int rev;
	};
	vector<vector<edge>> graph;
	vector<int> level, iter;
public:
	Dinic(int n) {
		graph = vector<vector<edge>>(n, vector<edge>());
		level = iter = vector<int>(n);
	}
	void add_edge(int from, int to, T cap) {
		graph[from].push_back({ to,cap,(T)graph[to].size() });
		graph[to].push_back({ from,0,(T)graph[from].size() - 1 });
	}
	void bfs(int s) {
		for (int &i : level) i = -1;
		queue<int> que;
		level[s] = 0;
		que.push(s);
		while (!que.empty()) {
			int v = que.front(); que.pop();
			for (int i = 0; i < graph[v].size(); i++) {
				edge &e = graph[v][i];
				if (e.cap > 0 && level[e.to] < 0) {
					level[e.to] = level[v] + 1;
					que.push(e.to);
				}
			}
		}
	}
	T dfs(int v, int t, T f) {
		if (v == t) return f;
		for (int &i = iter[v]; i < graph[v].size(); i++) {
			edge &e = graph[v][i];
			if (e.cap > 0 && level[v] < level[e.to]) {
				T d = dfs(e.to, t, min(f, e.cap));
				if (d > 0) {
					e.cap -= d;
					graph[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}
	T max_flow(int s, int t) {
		T flow = 0;
		while (true) {
			bfs(s);
			if (level[t] < 0) return flow;
			for (int &i : iter) i = 0;
			T f = 0;
			while ((f = dfs(s, t, inf)) > 0) flow += f;
		}
	}
	void debug() {
		for (int i = 0; i < graph.size(); i++) {
			for (auto &e : graph[i]) {
				if (e.cap) {
					cerr << i << ' ' << e.to << ' ' << e.cap << '\n';
				}
			}
		}
	}
};

class unionfind {
	vector<int> par;
	vector<int> sz;
public:
	unionfind(int n) {
		par = vector<int>(n);
		for (int i = 0; i < n; i++) par[i] = i;
		sz = vector<int>(n, 1);
	}
	int find(int x) {
		if (par[x] == x) return x;
		else return par[x] = find(par[x]);
	}
	int si(int x) { return sz[find(x)]; }
	bool same(int x, int y) { return find(x) == find(y); }
	void unite(int x, int y) {
		x = find(x);
		y = find(y);
		if (x == y) return;
		if (sz[x] < sz[y]) {
			par[x] = y;
			sz[y] += sz[x];
		}
		else {
			par[y] = x;
			sz[x] += sz[y];
		}
	}
};

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n;
	cin >> n;
	vector<int> p(n), q(n);
	unionfind unip(n), uniq(n);
	rep(i, n) {
		cin >> p[i];
		unip.unite(p[i], i);
	}
	rep(i, n) {
		cin >> q[i];
		uniq.unite(q[i], i);
	}
	Dinic<int> d(2 * n + 2);
	int ans = n;
	rep(i, n) {
		if (p[i] == i && q[i] == i) { ans--; continue; }
		else if (p[i] == i) d.add_edge(2 * n, n + uniq.find(i), 1);
		else if (q[i] == i) d.add_edge(unip.find(i), 2 * n + 1, 1);
		d.add_edge(unip.find(i), n + uniq.find(i), 1);
		if (p[i] != i && p[i] == q[i]) d.add_edge(n + uniq.find(i), unip.find(i), 1);
	}
	cout << ans - d.max_flow(2 * n, 2 * n + 1) << '\n';
}

Measured memory_bytes: 18340