/*
 * Author       : YangDavid
 * Created Time : 2019年09月22日 星期日 21时16分17秒
 */

#include<bits/stdc++.h>
#define rep(i, n) for(int i = 1; i <= n; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x3f3f3f3f;

#define Type int
struct Edge {
    int from, to;
    Type cap, flow;
    Edge(int u = 0, int v = 0, Type c = 0):
        from(u), to(v), cap(c), flow(0) { }
};

struct Dinic {
    int N;
    vector<Edge> edges;
    vector< vector<int> > adj;
    vector<int> d, cur;

    Dinic(int Num): N(Num + 20), edges(0), adj(Num + 20), d(Num + 20), cur(Num + 20) { }

    void addEdge(int u, int v, Type cap) {
        if(u == v) return;
        edges.emplace_back(u, v, cap);
        adj[u].emplace_back(edges.size() - 1);
        edges.emplace_back(v, u, 0);
        adj[v].emplace_back(edges.size() - 1);
    }

    bool bfs(int S, int T) {
        queue<int> q({S});
        fill(d.begin(), d.end(), N + 1);
        d[S] = 0;
        while(!q.empty()) {
            int u = q.front(); q.pop();
            if(u == T) break;
            for(int k : adj[u]) {
                Edge &e = edges[k];
                if(e.flow < e.cap && d[e.to] > d[e.from] + 1) {
                    d[e.to] = d[e.from] + 1;
                    q.emplace(e.to);
                }
            }
        }
        return d[T] != N + 1;
    }

    Type dfs(int u, int T, Type flow = -1) {
        if(u == T || flow == 0) return flow;
        for(int &i = cur[u]; i < int(adj[u].size()); ++i) {
            Edge &e = edges[adj[u][i]];
            Edge &re = edges[adj[u][i] ^ 1];
            if(d[e.to] == d[e.from] + 1) {
                Type rem = e.cap - e.flow;
                if(flow != -1 && rem > flow) rem = flow;
                Type pushed = dfs(e.to, T, rem);
                if(pushed > 0) {
                    e.flow += pushed;
                    re.flow -= pushed;
                    return pushed;
                }
            }
        }
        return 0;
    }

    Type maxFlow(int S, int T) {
        Type total = 0;
        while(bfs(S, T)) {
            fill(cur.begin(), cur.end(), 0);
            while(Type flow = dfs(S, T))
                total += flow;
        }
        return total;
    }
};
#undef Type

int n;
pair< int, vector<int> > decomposite(vector<int> p) {
    int cc = 0;
    vector<int> vis(n, -1);
    for(int i = 0; i < n; ++i) if(vis[i] == -1) {
        int c = i;
        do { vis[c] = cc, c = p[c]; } while(c != i);
        cc++;
    }
    return {cc, vis};
}

int main() {
    scanf("%d", &n);
    vector<int> P(n), Q(n), a, b;
    for(int i = 0; i < n; ++i) scanf("%d", &P[i]);
    for(int i = 0; i < n; ++i) scanf("%d", &Q[i]);

    int lc, rc, ans = n;
    tie(lc, a) = decomposite(P);
    tie(rc, b) = decomposite(Q);
    
    Dinic mf(2 + lc + rc);
    int s = 0, t = 1 + lc + rc;
    for(int i = 0; i < n; ++i) {
        if(P[i] == Q[i] && P[i] == i) ans--;
        else if(P[i] == i) mf.addEdge(s, 1 + lc + b[i], 1);
        else if(Q[i] == i) mf.addEdge(1 + a[i], t, 1);
        else {
            mf.addEdge(1 + a[i], 1 + lc + b[i], 1);
            if(P[i] == Q[i])
                mf.addEdge(1 + lc + b[i], 1 + a[i], 1);
        }
    }
    printf("%d\n", ans - mf.maxFlow(s, t));

    return 0;
}


Measured memory_bytes: 16100