#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 500000;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef pair<ll, ll> LP;
typedef vector<ll> vec;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-5;
const ld pi = acos(-1.0);

struct edge { int to; ll cap; int rev; };
struct Dinic {
private:
	int n;
	vector<vector<edge>> v;
	vector<int> dist, iter;
public:
	Dinic(int sz) :n(sz), v(sz), dist(sz), iter(sz) {}

	void addedge(int from, int to, ll cap) {
		int x = v[to].size(), y = v[from].size();
		v[from].push_back({ to,cap,x });
		v[to].push_back({ from,0,y });
	}

	void bfs(int s) {
		fill(dist.begin(), dist.end(), -1);
		queue<int> q;
		dist[s] = 0;
		q.push(s);
		while (q.size()) {
			int x = q.front(); q.pop();
			rep(i, v[x].size()) {
				edge &e = v[x][i];
				if (e.cap>0 && dist[e.to]<0) {
					dist[e.to] = dist[x] + 1;
					q.push(e.to);
				}
			}
		}
	}

	ll dfs(int x, int t, ll f) {
		if (x == t)return f;
		for (int& i = iter[x]; i<(int)v[x].size(); ++i) {
			edge& e = v[x][i];
			if (e.cap>0 && dist[x]<dist[e.to]) {
				ll d = dfs(e.to, t, min(f, e.cap));
				if (d>0) {
					e.cap -= d;
					v[e.to][e.rev].cap += d;
					return d;
				}
			}
		}
		return 0;
	}

	ll max_flow(int s, int t) {
		ll flow = 0;
		while (1) {
			bfs(s);
			if (dist[t]<0)return flow;
			fill(iter.begin(), iter.end(), 0);
			ll f;
			while ((f = dfs(s, t, 1LL << 62))>0)flow += f;
		}
	}
};

vector<vector<int>> cyclep, cycleq;
int locp[1 << 17], locq[1 << 17];
void solve() {
	int n; cin >> n;
	vector<int> p(n), q(n);
	rep(i, n) {
		cin >> p[i];
	}
	rep(i, n) {
		cin >> q[i];
	}
	int s = 2 * n; int t = s + 1;
	Dinic g(2 * n + 2);
	vector<bool> used(n, false);
	rep(i, n) {
		if (used[i])continue;
		vector<int> ids; ids.push_back(i);
		int cur = p[i]; used[i] = true; locp[i] = cyclep.size();
		while (cur != i) {
			ids.push_back(cur); used[cur] = true;
			locp[cur] = cyclep.size();
			cur = p[cur];
		}
		cyclep.push_back(ids);
	}
	fill(used.begin(), used.end(), false);
	rep(i, n) {
		if (used[i])continue;
		vector<int> ids; ids.push_back(i);
		int cur = q[i]; used[i] = true; locq[i] = cycleq.size();
		while (cur != i) {
			ids.push_back(cur); used[cur] = true;
			locq[cur] = cycleq.size();
			cur = q[cur];
		}
		cycleq.push_back(ids);
	}
	int lenp = cyclep.size();
	int lenq = cycleq.size();
	rep(i, lenp) {
		g.addedge(s, i, cyclep[i].size());
		if (cyclep[i].size() == 1) {
			g.addedge(i, t, mod);
		}
		else {
			g.addedge(i, t, cyclep[i].size());
		}
	}
	rep(i, lenq) {
		g.addedge(i+lenp,t, cycleq[i].size());
		if (cycleq[i].size() == 1) {
			g.addedge(s, i + lenp, mod);
		}
		else {
			g.addedge(s, i + lenp, cycleq[i].size());
		}
	}
	rep(i, lenp) {
		vector<int> id,idq;
		rep(j, cyclep[i].size()) {
			id.push_back(locq[cyclep[i][j]]);
			int cur = cyclep[i][j];
			if (p[cur] == q[cur])idq.push_back(locq[cur]);
		}
		sort(id.begin(), id.end());
		rep(j, id.size()) {
			int le = j;
			while (j + 1 < id.size() && id[j] == id[j + 1])j++;
			//g.addedge(i, lenp + id[j], j - le + 1);
			g.addedge(lenp + id[j], i, j - le + 1);
		}
		sort(idq.begin(), idq.end());
		rep(j, idq.size()) {
			int le = j;
			while (j + 1 < idq.size() && idq[j] == idq[j + 1])j++;
			g.addedge(i, lenp + idq[j], j - le + 1);
			//g.addedge(lenp + id[j], i, j - le + 1);
		}
	}
	ll f = g.max_flow(s, t);
	//cout << f << endl;
	f -= 2 * n;
	f = n - f;
	cout << f << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//init();
	solve();
	//stop
	return 0;
}


Measured memory_bytes: 33068