#include <bits/stdc++.h>
    using namespace std;
     
    using ll=long long;
    //#define int ll
     
    #define rng(i,a,b) for(int i=int(a);i<int(b);i++)
    #define rep(i,b) rng(i,0,b)
    #define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
    #define per(i,b) gnr(i,0,b)
    #define pb push_back
    #define eb emplace_back
    #define a first
    #define b second
    #define bg begin()
    #define ed end()
    #define all(x) x.bg,x.ed
    #ifdef LOCAL
    #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
    #else
    #define dmp(x) void(0)
    #endif
     
    template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
    template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}
     
    template<class t> using vc=vector<t>;
    template<class t> using vvc=vc<vc<t>>;
     
    using pi=pair<int,int>;
    using vi=vc<int>;
     
    template<class t,class u>
    ostream& operator<<(ostream& os,const pair<t,u>& p){
    	return os<<"{"<<p.a<<","<<p.b<<"}";
    }
     
    template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
    	os<<"{";
    	for(auto e:v)os<<e<<",";
    	return os<<"}";
    }
     
    template<class d>
    struct maxflow{
    	struct E{
    		int to,rev;
    		d cap;
    	};
    	vvc<E> g;
    	vi itr,lv;
    	maxflow(int n):g(n),itr(n),lv(n){}
    	void ae(int s,int t,d c){
    		g[s].pb({t,(int)g[t].size(),c});
    		g[t].pb({s,(int)g[s].size()-1,0});
    	}
    	void bfs(int s){
    		fill(all(lv),-1);
    		lv[s]=0;
    		queue<int> q;
    		q.push(s);
    		while(q.size()){
    			int v=q.front();q.pop();
    			for(auto e:g[v])if(e.cap>0&&lv[e.to]==-1){
    				lv[e.to]=lv[v]+1;
    				q.push(e.to);
    			}
    		}
    	}
    	d dfs(int v,int t,d f){
    		if(v==t)return f;
    		d res=0;
    		for(int&i=itr[v];i<(int)g[v].size();i++){
    			E& e=g[v][i];
    			if(e.cap>0&&lv[e.to]==lv[v]+1){
    				d a=dfs(e.to,t,min(f,e.cap));
    				if(a>0){
    					e.cap-=a;
    					g[e.to][e.rev].cap+=a;
    					res+=a;
    					f-=a;
    					if(f<=0)break;
    				}
    			}
    		}
    		return res;
    	}
    	d calc(int s,int t){
    		d f=0;
    		while(1){
    			bfs(s);
    			if(lv[t]==-1)
    				return f;
    			fill(all(itr),0);
    			f+=dfs(s,t,1e9);
    		}
    	}
    };
     
    pair<int,vi> cmp(vi p){
    	int n=p.size(),s=0;
    	vi res(n,-1);
    	rep(i,n)if(res[i]==-1){
    		int x=i;
    		do{
    			res[x]=s;
    			x=p[x];
    		}while(x!=i);
    		s++;
    	}
    	return make_pair(s,res);
    }
     
    signed main(){
    	cin.tie(0);
    	ios::sync_with_stdio(0);
    	cout<<fixed<<setprecision(20);
    	
    	int n;cin>>n;
    	vi p(n);rep(i,n)cin>>p[i];
    	vi q(n);rep(i,n)cin>>q[i];
    	
    	int l=0,r=0;
    	vi a,b;
    	tie(l,a)=cmp(p);
    	tie(r,b)=cmp(q);
    	
    	maxflow<int> mcf(1+l+r+1);
    	int ans=n;
    	rep(i,n){
    		if(i==p[i]&&i==q[i])
    			ans--;
    		else if(i==p[i]){
    			mcf.ae(1+l+b[i],1+l+r,1);
    		}else if(i==q[i]){
    			mcf.ae(0,1+a[i],1);
    		}else{
    			mcf.ae(1+l+b[i],1+a[i],1);
    			if(p[i]==q[i])
    				mcf.ae(1+a[i],1+l+b[i],1);
    		}
    	}
    	int f=mcf.calc(0,1+l+r);
    	ans-=f;
    	cout<<ans<<endl;
    }

Measured memory_bytes: 11248