#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

struct FlowEdge {
    int v, u;
    long long cap, flow = 0;
    FlowEdge(int v_, int u_, long long cap_) : v(v_), u(u_), cap(cap_) {}
};

struct Dinic {
    const long long flow_inf = 1e18;
    vector<FlowEdge> edges;
    vector<vector<int>> adj;
    int n, m = 0;
    int s, t;
    vector<int> level, ptr;
    queue<int> q;

    Dinic(int n_, int s_, int t_) : n(n_), s(s_), t(t_) {
        adj.resize(n_);
        level.resize(n_);
        ptr.resize(n_);
    }

    void add_edge(int v, int u, long long cap) {
        edges.emplace_back(v, u, cap);
        edges.emplace_back(u, v, 0);
        adj[v].push_back(m);
        adj[u].push_back(m + 1);
        m += 2;
    }

    bool bfs() {
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (int id : adj[v]) {
                if (edges[id].cap - edges[id].flow < 1)
                    continue;
                if (level[edges[id].u] != -1)
                    continue;
                level[edges[id].u] = level[v] + 1;
                q.push(edges[id].u);
            }
        }
        return level[t] != -1;
    }

    long long dfs(int v, long long pushed) {
        if (pushed == 0)
            return 0;
        if (v == t)
            return pushed;
        for (int& cid = ptr[v]; cid < (int)adj[v].size(); cid++) {
            int id = adj[v][cid];
            int u = edges[id].u;
            if (level[v] + 1 != level[u] || edges[id].cap - edges[id].flow < 1)
                continue;
            long long tr = dfs(u, min(pushed, edges[id].cap - edges[id].flow));
            if (tr == 0)
                continue;
            edges[id].flow += tr;
            edges[id ^ 1].flow -= tr;
            return tr;
        }
        return 0;
    }

    long long flow() {
        long long f = 0;
        while (true) {
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            q.push(s);
            if (!bfs())
                break;
            fill(ptr.begin(), ptr.end(), 0);
            while (long long pushed = dfs(s, flow_inf)) {
                f += pushed;
            }
        }
        return f;
    }
};

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int N;
	cin >> N;
	vector<int> P(N), Q(N);
	for(int i = 0; i < N; i++) cin >> P[i];
	for(int i = 0; i < N; i++) cin >> Q[i];
	vector<int> CP(N, -1), CQ(N, -1);
	int N_CP = 0, N_CQ = 0;
	for(int i = 0; i < N; i++) if(CP[i] == -1 && P[i] != i) {
		int cur = P[i];
		CP[i] = N_CP;
		while(cur != i) {
			CP[cur] = N_CP;
			cur = P[cur];
		}
		N_CP++;
	}
	for(int i = 0; i < N; i++) if(CQ[i] == -1 && Q[i] != i) {
		int cur = Q[i];
		CQ[i] = N_CQ;
		while(cur != i) {
			CQ[cur] = N_CQ;
			cur = Q[cur];
		}
		N_CQ++;
	}
	vector< map<int, int> > E(N_CP);
	vector<int> cost(N_CP + N_CQ, 0);
	for(int i = 0; i < N; i++) {
		if(P[i] == i && Q[i] == i) continue;
		if(P[i] == i) cost[CQ[i]+N_CP]--;
		else if(Q[i] == i) cost[CP[i]]--;
		else if(P[i] == Q[i]) {
			cost[CP[i]]++, cost[CQ[i]+N_CP]++;
			E[CP[i]][CQ[i]+N_CP] += 2;
		}
		else E[CP[i]][CQ[i]+N_CP] += 1;
	}
	Dinic solver(N_CP+N_CQ+2, 0, N_CP+N_CQ+1);
	cat cost_sum = 0;
	for(int i = 0; i < N_CP+N_CQ; i++) if(cost[i] > 0) {
		cost_sum += cost[i];
		if(i < N_CP) solver.add_edge(0, i+1, cost[i]);
		else solver.add_edge(i+1, N_CP+N_CQ+1, cost[i]);
	}
	for(int i = 0; i < N_CP; i++) for(auto & e : E[i]) {
		if(cost[i] <= 0) break;
		if(cost[e.ff] <= 0) continue;
		solver.add_edge(i+1, e.ff+1, e.ss);
	}
	cat ans = cost_sum - solver.flow();
	for(int i = 0; i < N; i++) if(P[i] != Q[i]) ans++;
	cout << ans << "\n";
	return 0;
}

// look at my code
// my code is amazing


Measured memory_bytes: 24492