#include<bits/stdc++.h>
#define Fail puts("NO"),exit(0);
#define eps 1e-10
#define maxn 100010
#define maxm 100010
#define inf 1000000007
#define mod 1000000007
#define pi acos(-1)
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int>pii;
typedef bitset<maxn> bit;
int read()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch-'0'<0||ch-'0'>9){if(ch=='-') f=-1;ch=getchar();}
    while(ch-'0'>=0&&ch-'0'<=9){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,N,M,s,t;
int P[maxn],Q[maxn];
int idp[maxn],idq[maxn];
void dfs1(int x)
{
    if(idp[x])  return;
    idp[x]=N;
    dfs1(P[x]);
}
void dfs2(int x)
{
    if(idq[x])  return;
    idq[x]=M;
    dfs2(Q[x]);
}
int res;
int head[maxn*2],nxt[maxn*2],to[maxn*2],c[maxn*2],tot=1;
void add(int u,int v,int x)
{
    tot++;
    nxt[tot]=head[u];
    head[u]=tot;
    to[tot]=v;
    c[tot]=x;
}
int dep[maxn*2];
queue<int>q;
void bfs()
{
    for(int i=1;i<=t;i++)  dep[i]=-1;
    q.push(s);dep[s]=0;
    while(q.size())
    {
        int now=q.front();q.pop();
        for(int i=head[now];i;i=nxt[i])
        {
            if(dep[to[i]]<0&&c[i])
            {
                dep[to[i]]=dep[now]+1;
                q.push(to[i]);
            }
        }
    }
}
int cur[maxn*2];
int dfs(int u,int t,int dist)
{
    if(u==t)  return dist;
    for(int &i=cur[u];i;i=nxt[i])
    {
        if(dep[to[i]]==dep[u]+1&&c[i])
        {
            int di=dfs(to[i],t,min(dist,c[i]));
            if(di>0)
            {
                c[i]-=di;
                c[i^1]+=di;
                return di;
            }
        }
    }
    return 0;
}
int Dinic()
{
    int res=0;
    while(true)
    {
        bfs();
        if(dep[t]<0)  return res;
        for(int i=1;i<=t;i++)  cur[i]=head[i];
        while(int d=dfs(s,t,inf))  res+=d;
    }
    return res;
}
int main()
{
    n=read();res=n;
    for(int i=1;i<=n;i++)  P[i]=read()+1;
    for(int i=1;i<=n;i++)  Q[i]=read()+1;
    for(int i=1;i<=n;i++)
      if(!idp[i])  ++N,dfs1(i);
    for(int i=1;i<=n;i++)
      if(!idq[i])  ++M,dfs2(i);
    s=N+M+1;t=N+M+2;
    for(int i=1;i<=n;i++)
    {
        if(P[i]==Q[i])
        {
            if(P[i]==i)  res--;
            else{
                add(idp[i],idq[i]+N,1);
                add(idq[i]+N,idp[i],1);
            }
        }
        else if(P[i]==i)
        {
            add(idq[i]+N,t,1);
            add(t,idq[i]+N,0);
        }
        else if(Q[i]==i)
        {
            add(s,idp[i],1);
            add(idp[i],s,0);
        }
        else{
            add(idq[i]+N,idp[i],1);
            add(idp[i],idq[i]+N,0);
        }
    }
    printf("%d\n",res-Dinic());
    return 0;
}