//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
//#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=mul(ans,a,c);a=mul(a,a,c),b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ld pi = acos(-1);
const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=100000+10,maxn=2000000+10,inf=0x3f3f3f3f;

struct edge{int to,Next,c;}e[maxn];
int s,t,cnt,head[N],cur[N];
void init(){cnt=0;memset(head,-1,sizeof head);}
void add(int u,int v,ll c)
{
    e[cnt].to=v;e[cnt].c=c;e[cnt].Next=head[u];head[u]=cnt++;
    e[cnt].to=u;e[cnt].c=0;e[cnt].Next=head[v];head[v]=cnt++;
}
int dis[N];
bool bfs()
{
    queue<int>q;
    memset(dis,-1,sizeof dis);
    dis[s]=1;q.push(s);
    while(!q.empty())
    {
        int x=q.front();q.pop();
        for(int i=head[x];~i;i=e[i].Next)
        {
            int y=e[i].to;
            if(dis[y]==-1&&e[i].c>0)
            {
                dis[y]=dis[x]+1;
                q.push(y);
            }
        }
    }
    return dis[t]!=-1;
}
int dfs(int u,int mx)
{
    if(u==t)return mx;
    int flow=0,f;
    for(int &i=cur[u];~i;i=e[i].Next)
    {
        int x=e[i].to;
        if(dis[x]==dis[u]+1&&e[i].c>0&&(f=dfs(x,min(mx-flow,e[i].c))))
        {
            e[i].c-=f;
            e[i^1].c+=f;
            flow+=f;
            if(flow==mx)break;
        }
    }
    if(flow==0)dis[u]=-2;
    return flow;
}
int maxflow()
{
    int ans=0,f;
    while(bfs())
    {
        for(int i=0;i<=t;i++)cur[i]=head[i];
        while((f=dfs(s,inf)))ans+=f;
    }
    return ans;
}
int d1[N],d2[N],a[N],b[N];
int main()
{
    int n;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]++;
    int c1=0,c2=0;
    for(int i=1;i<=n;i++)if(!d1[i])
    {
        c1++;
        int x=i;
        while(!d1[x])d1[x]=c1,x=a[x];
    }
    for(int i=1;i<=n;i++)if(!d2[i])
    {
        c2++;
        int x=i;
        while(!d2[x])d2[x]=c2,x=b[x];
    }
    s=c1+c2+1;t=s+1;
    init();
    int ans=n;
    for(int i=1;i<=n;i++)
    {
        if(a[i]==i&&b[i]==i)ans--;
        else if(a[i]==i)add(s,d2[i]+c1,1);
        else if(b[i]==i)add(d1[i],t,1);
        else
        {
            add(d1[i],d2[i]+c1,1);
            if(a[i]==b[i])add(d2[i]+c1,d1[i],1);
        }
    }
    ans-=maxflow();
    printf("%d\n",ans);
    return 0;
}
/********************

********************/


Measured memory_bytes: 8448