/*
Author: yww
Language: C++
Mail: weeerrr720@qq.com
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
void open(const char *s){
#ifndef ONLINE_JUDGE
    char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
void open2(const char *s){
#ifdef DEBUG
    char str[100];sprintf(str,"%s.in",s);freopen(str,"r",stdin);sprintf(str,"%s.out",s);freopen(str,"w",stdout);
#endif
}
template <class T>
int upmin(T &a, const T &b){return (b<a?a=b,1:0);}
template <class T>
int upmax(T &a, const T &b){return (b>a?a=b,1:0);}
namespace io
{
    const int SIZE=(1<<20)+1;
    char ibuf[SIZE],*iS,*iT;
    char obuf[SIZE],*oS=obuf,*oT=oS+SIZE-1;
    int getc()
    {
        (iS==iT?iS=ibuf,iT=ibuf+fread(ibuf,1,SIZE,stdin):0);
        return iS==iT?EOF:*(iS++);
    }
    int f;
    char c;
    template <class T>
    void get(T &x)
    {
        f=1;
        for(c=getc();(c<'0'||c>'9')&&c!='-';c=getc());
        (c=='-'?f=-1,c=getc():0);
        x=0;
        for(;c>='0'&&c<='9';c=getc())
            x=x*10+c-'0';
        x*=f;
    }
    void flush()
    {
        fwrite(obuf,1,oS-obuf,stdout);
        oS=obuf;
    }
    void putc(char x)
    {
        *(oS++)=x;
        if(oS==oT)
            flush();
    }
    int a[55],t;
    template <class T>
    void put(T x)
    {
        if(!x)
            putc('0');
        x<0?putc('-'),x=-x:0;
        while(x)
        {
            a[++t]=x%10;
            x/=10;
        }
        while(t)
            putc(a[t--]+'0');
    }
    void space()
    {
        putc(' ');
    }
    void enter()
    {
        putc('\n');
    }
    struct flusher
    {
        ~flusher()
        {
            flush();
        }
    }
    io_flusher;
}
const int infi=0x3fffffff;
const ll infll=0x3fffffffffffffffll;
namespace flow
{
	int cur[200010];
	int h[200010];
	int v[1000010];
	int c[1000010];
	int t[1000010];
	int cnt;
	void add(int x, int y, int c1, int c2)
	{
//		if(c1)
//			printf("%d %d\n",x,y);
//		if(c2)
//			printf("%d %d\n",y,x);
		cnt++;
		v[cnt]=y;
		c[cnt]=c1;
		t[cnt]=h[x];
		h[x]=cnt;
		cnt++;
		v[cnt]=x;
		c[cnt]=c2;
		t[cnt]=h[y];
		h[y]=cnt;
	}
	int d[200010];
	queue<int> q;
	int S, T;
	int bfs()
	{
	    memset(d,-1,sizeof d);
	    queue<int> q;
	    q.push(S);
	    d[S]=0;
	    int x,i;
	    while(!q.empty())
	    {
	        x=q.front();
	        q.pop();
	        for(i=h[x];i;i=t[i])
	            if(c[i]&&d[v[i]]==-1)
	            {
	                d[v[i]]=d[x]+1;
	                if(v[i]==T)
	                    return 1;
	                q.push(v[i]);
	            }
	    }
	    return 0;
	}
	int op(int x)
	{
	    return ((x-1)^1)+1;
	}
	int dfs(int x,int flow)
	{
	    if(x==T)
	        return flow;
	    int s=0;
	    for(int &i=cur[x];i;i=t[i])
	        if(c[i]&&d[v[i]]==d[x]+1)
	        {
	            int temp=dfs(v[i],min(flow,c[i]));
	            s+=temp;
	            flow-=temp;
	            c[i]-=temp;
	            c[op(i)]+=temp;
	            if(!flow)
	                break;
	        }
	    if(flow)
	        d[x]=-1;
	    return s;
	}
	int solve()
	{
		int res=0;
		while(bfs())
		{
			memcpy(cur,h,sizeof h);
			res+=dfs(S, infi);
		}
		return res;
	}
}
using flow::add;
const int N=100010;
int a[N],b[N],visa[N],visb[N],ca[N],cb[N];
int cnta,cntb;
int n;
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
		a[i]++;
	}
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&b[i]);
		b[i]++;
	}
	for(int i=1;i<=n;i++)
		if(!visa[i])
		{
			cnta++;
			for(int j=i;!visa[j];j=a[j])
			{
				visa[j]=1;
				ca[j]=cnta;
			}
		}
	for(int i=1;i<=n;i++)
		if(!visb[i])
		{
			cntb++;
			for(int j=i;!visb[j];j=b[j])
			{
				visb[j]=1;
				cb[j]=cntb;
			}
		}
	flow::S = cnta + cntb + 1;
	flow::T = cnta + cntb + 2;
	int ans=n;
	for(int i=1;i<=n;i++)
		if(a[i]==i&&b[i]==i)
			ans--;
		else if(a[i]==i)
		{
			flow::add(cb[i]+cnta, flow::T,1,0);
		}
		else if(b[i]==i)
		{
			flow::add(flow::S, ca[i],1,0);
		}
		else if(a[i]==b[i])
		{
			flow::add(ca[i], cb[i]+cnta,1,1);
		}
		else
		{
			flow::add(cb[i]+cnta, ca[i],1,0);
		}
	ans-=flow::solve();
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 14592