#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=201000;
int n,m,s1,s2,f[N],p[N],pr[N],qr[N],q[N];


//const int MAXN = 5005;
template <int MAXN, class T = int>
struct HLPP {
    const T INF = numeric_limits<T>::max();
    struct edge {
        int to, rev;
        T f;
    };
    int s = MAXN - 1, t = MAXN - 2;
    vector<edge> adj[MAXN];
    deque<int> lst[MAXN];
    vector<int> gap[MAXN];
    int ptr[MAXN];
    T excess[MAXN];
    int highest, height[MAXN], cnt[MAXN], work;
    void addEdge(int from, int to, int f, bool isDirected = true) {
    	assert(f>=0);
    	if (f==0) return;
        adj[from].push_back({ to, (int)adj[to].size(), f });
        adj[to].push_back({ from, (int)adj[from].size() - 1, isDirected ? 0 : f });
    }
    void updHeight(int v, int nh) {
        work++;
        if (height[v] != MAXN)
            cnt[height[v]]--;
        height[v] = nh;
        if (nh == MAXN)
            return;
        cnt[nh]++, highest = nh;
        gap[nh].push_back(v);
        if (excess[v] > 0)
            lst[nh].push_back(v), ptr[nh]++;
    }
    void globalRelabel() {
        work = 0;
        fill(begin(height), end(height), MAXN);
        fill(begin(cnt), end(cnt), 0);
        for (int i = 0; i <= highest; i++) lst[i].clear(), gap[i].clear(), ptr[i] = 0;
        height[t] = 0;
        queue<int> q({ t });
        while (!q.empty()) {
            int v = q.front();
            q.pop();
            for (auto &e : adj[v])
                if (height[e.to] == MAXN && adj[e.to][e.rev].f > 0)
                    q.push(e.to), updHeight(e.to, height[v] + 1);
            highest = height[v];
        }
    }
    void push(int v, edge &e) {
        if (excess[e.to] == 0)
            lst[height[e.to]].push_back(e.to), ptr[height[e.to]]++;
        T df = min(excess[v], e.f);
        e.f -= df, adj[e.to][e.rev].f += df;
        excess[v] -= df, excess[e.to] += df;
    }
    void discharge(int v) {
        int nh = MAXN;
        for (auto &e : adj[v]) {
            if (e.f > 0) {
                if (height[v] == height[e.to] + 1) {
                    push(v, e);
                    if (excess[v] <= 0)
                        return;
                } else
                    nh = min(nh, height[e.to] + 1);
            }
        }
        if (cnt[height[v]] > 1)
            updHeight(v, nh);
        else {
            for (int i = height[v]; i < MAXN; i++) {
                for (auto j : gap[i]) updHeight(j, MAXN);
                gap[i].clear(), ptr[i] = 0;
            }
        }
    }
    T calc(int heur_n = MAXN) {
        fill(begin(excess), end(excess), 0);
        excess[s] = INF, excess[t] = -INF;
        globalRelabel();
        for (auto &e : adj[s]) push(s, e);
        for (; highest >= 0; highest--) {
            // while (ptr[highest] >= 0) {
            while (!lst[highest].empty()) {
                // int v = lst[highest][ptr[highest]];
                // cout << highest << ' ' << ptr[highest] << ' ' << lst[highest].size() << endl;
                int v = lst[highest].back();
                // ptr[highest]--;
                lst[highest].pop_back();
                discharge(v);
                if (work > 4 * heur_n)
                    globalRelabel();
            }
        }
        return excess[t] + INF;
    }
};

HLPP<N, long long> hlpp;


int find(int x) {
	return f[x]==x?x:f[x]=find(f[x]);
}

int main() {
	scanf("%d",&n);
	rep(i,0,n) f[i]=i;
	rep(i,0,n) {
		scanf("%d",p+i);
		f[find(i)]=find(p[i]);
	}
	rep(i,0,n) if (find(i)==i) pr[i]=++m;
	rep(i,0,n) if (find(i)!=i) pr[i]=pr[find(i)];
	int s1=m;
	rep(i,0,n) f[i]=i;
	rep(i,0,n) {
		scanf("%d",q+i);
		f[find(i)]=find(q[i]);
	}
	rep(i,0,n) if (find(i)==i) qr[i]=++m;
	rep(i,0,n) if (find(i)!=i) qr[i]=qr[find(i)];
	int s=m+1,t=m+2;
	hlpp.s=s; hlpp.t=t;
	rep(i,0,n) {
		int f01=1+(p[i]==q[i]);
		int f10=1+(i==i);
		int f00=1+(p[i]==i);
		int f11=1+(q[i]==i);
		assert(f00+f11<=f01+f10);
		//printf("%d %d %d %d\n",f00,f11,f10,f01);
		int c=f01+f10-f00-f11;
		hlpp.addEdge(pr[i],qr[i],c,0);
		int a=min(2*f11,2*f10-c);
		int e=2*f10-c-a;
		int b=2*f11-a;
		int d=2*f00-e;
		hlpp.addEdge(s,pr[i],a,1);
		hlpp.addEdge(s,qr[i],b,1);

		hlpp.addEdge(pr[i],t,d,1);
		hlpp.addEdge(qr[i],t,e,1);
	}
	printf("%lld\n",2*n-hlpp.calc()/2);
}


Measured memory_bytes: 184412