/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author majk
 */

#ifndef MAJK_LIB
#define MAJK_LIB

#include <vector>
#include <stack>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <array>
#include <bitset>
using namespace std;

#define x first
#define y second
typedef std::pair<int,int> pii; typedef long long ll; typedef unsigned long long ull; typedef unsigned int ui; typedef pair<ui,ui> puu;

template <typename T, typename U> std::istream&operator>>(std::istream&i, pair<T,U>&p) {i >> p.x >> p.y; return i;}
template<typename T>std::istream&operator>>(std::istream&i,vector<T>&t) {for(auto&v:t){i>>v;}return i;}
template <typename T, typename U> std::ostream&operator<<(std::ostream&o, const pair<T,U>&p) {o << p.x << ' ' << p.y; return o;}
template<typename T>std::ostream&operator<<(std::ostream&o,const vector<T>&t) {if(t.empty())o<<'\n';for(size_t i=0;i<t.size();++i){o<<t[i]<<" \n"[i == t.size()-1];}return o;}
template<typename T> using minheap = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using maxheap = priority_queue<T, vector<T>, less<T>>;
ui logceil(ll x) { return x?8*sizeof(ll)-__builtin_clzll(x):0; }

namespace std { template<typename T,typename U>struct hash<pair<T,U>>{hash<T>t;hash<U>u;size_t operator()(const pair<T,U>&p)const{return t(p.x)^(u(p.y)<<7);}}; }
template<typename T,typename F>T bsh(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){l=m+1;r=m;}else{h=m-1;}}return r;}
template<typename F> double bshd(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){l=m;}else{h=m;}}return (l+h)/2;}
template<typename T,typename F>T bsl(T l,T h,const F&f){T r=-1,m;while(l<=h){m=(l+h)/2;if(f(m)){h=m-1;r=m;}else{l=m+1;}}return r;}
template<typename F> double bsld(double l,double h,const F&f,double p=1e-9){ui r=3+(ui)log2((h-l)/p);while(r--){double m=(l+h)/2;if(f(m)){h=m;}else{l=m;}}return (l+h)/2;}
template<typename T> T gcd(T a,T b) { if (a<b) swap(a,b); return b?gcd(b,a%b):a; }


template<typename T>class vector2:public vector<vector<T>>{public:vector2(){} vector2(size_t a,size_t b,T t=T()):vector<vector<T>>(a,vector<T>(b,t)){}};
template<typename T>class vector3:public vector<vector2<T>>{public:vector3(){} vector3(size_t a,size_t b,size_t c,T t=T()):vector<vector2<T>>(a,vector2<T>(b,c,t)){}};
template<typename T>class vector4:public vector<vector3<T>>{public:vector4(){} vector4(size_t a,size_t b,size_t c,size_t d,T t=T()):vector<vector3<T>>(a,vector3<T>(b,c,d,t)){}};
template<typename T>class vector5:public vector<vector4<T>>{public:vector5(){} vector5(size_t a,size_t b,size_t c,size_t d,size_t e,T t=T()):vector<vector4<T>>(a,vector4<T>(b,c,d,e,t)){}};


#endif
#ifndef UTIL_H
#define UTIL_H



template <typename T> struct bounded_priority_queue {
	inline bounded_priority_queue(ui X) : A(X), B(0), s(0) {}
	inline void push(ui L, T V) { B = max(B, L); A[L].push(V); ++s; }
	inline const T &top() const { return A[B].front(); }
	inline void pop() { --s; A[B].pop(); while (B > 0 && A[B].empty()) --B; }
	inline bool empty() const { return A[B].empty(); }
	inline void clear() { s = B = 0; for (auto &a: A) a = queue<T>(); }
	inline ui size() const { return s; }
private:
	vector<queue<T>> A; ui B; int s;
};

struct	UnionFind {
	UnionFind(size_t N) : P(N, -1), comp(N) {}
	int find(int x) { while (P[x]> 0){x = P[x]-1;}return x; }
	bool united(int x, int y) { return find(x) == find(y); }
	bool unite(int x, int y) { x=find(x);y=find(y);if(x==y){return false;}--comp;if(P[x]>P[y]){swap(x,y);}P[x]+=P[y];P[y]= x+1;return true;}
	inline ui size(int u) { return -P[find(u)]; }
	vector<int> P;
	size_t comp;
};



#endif
#ifndef MAJK_DINIC
#define MAJK_DINIC


// DINIC MAX-FLOW ALGORITHM. LOOK ELSEWHERE
template<typename T> struct Dinic {
	ui V, E;
	std::vector<ui> LastEdge, EdgeToProcess, BfsDistance, PrevEdge, EdgeTarget;
	std::vector<T> Capacity, Residual;

	Dinic(ui v, ui e = 0) : V(v), E(0), LastEdge(V, ui(-1)), BfsDistance(V), EdgeToProcess(V) {
		Capacity.reserve(2*e);
		Residual.reserve(2*e);
		EdgeTarget.reserve(2*e);
		PrevEdge.reserve(2*e);
	}

	void AddEdge(ui x, ui y, T c1 = 1, T c2 = 0) {
//		cerr << x << ' ' << y << endl;
		EdgeTarget.push_back(y);
		Capacity.push_back(c1);
		Residual.push_back(c1);
		PrevEdge.push_back(LastEdge[x]);
		LastEdge[x] = E++;
		EdgeTarget.push_back(x);
		Capacity.push_back(c2);
		Residual.push_back(c2);
		PrevEdge.push_back(LastEdge[y]);
		LastEdge[y] = E++;
	}

	T push(int x, int v, T flow) {
		if (x == v) return flow;
		for (ui &e = EdgeToProcess[x]; e != -1; e = PrevEdge[e]) {
			ui y = EdgeTarget[e];
			if (Residual[e] && BfsDistance[x] + 1 == BfsDistance[y]) {
				if (T f = push(y, v, min(flow, Residual[e]))) {
					Residual[e] -= f;
					Residual[e ^ 1] += f;
					return f;
				}
			}
		}
		return 0;
	}

	T GetMaxFlow(int u, int v) {
		T ret = 0;
		while (true) {
			fill(BfsDistance.begin(), BfsDistance.end(), (ui)-1);
			BfsDistance[u] = 0;
			std::queue<int> Q{{u}};
			while (!Q.empty()) {
				int x = Q.front(); Q.pop();
				for (int e = LastEdge[x]; e != ui(-1); e = PrevEdge[e]) {
					ui y = EdgeTarget[e];
					if (Residual[e] && BfsDistance[y] == ui(-1)) {
						Q.push(y);
						BfsDistance[y] = BfsDistance[x] + 1;
					}
				}

			}

			if (BfsDistance[v] == ui(-1)) break;
			copy(LastEdge.begin(), LastEdge.end(), EdgeToProcess.begin());
			while (T f = push(u, v, std::numeric_limits<T>::max())) ret += f;
		}

		return ret;
	}

	std::vector<bool> GetMinCut(int u) {
		std::queue<int> Q;
		Q.push(u);
		vector<bool> R(V, false);
		R[u] = true;
		while (!Q.empty()) {
			int x = Q.front();
			Q.pop();
			for (int e = LastEdge[x]; e != -1; e = PrevEdge[e]) {
				int y = EdgeTarget[e];
				if (!R[y] && Residual[e] > 0) {
					Q.push(y);
					R[y] = true;
				}
			}
		}
		return R;
	}

	vector<std::tuple<ui, ui, T>> GetEdgesWithFlow() {
		vector<std::tuple<ui, ui, T>> Ans;
		for (ui u = 0; u < V; ++u) {
			for (ui e = LastEdge[u]; e != ui(-1); e = PrevEdge[e]) {
				if (Residual[e] < Capacity[e]) {
					Ans.push_back({u, EdgeTarget[e], Capacity[e] - Residual[e]});
				}
			}
		}
		return Ans;
	};
};
// DINIC MAX-FLOW ALGORITHM
#endif

class FTwoPermutations {
public:
    void solve(istream& cin, ostream& cout) {
        // if P[i] = Q[i] = i, ignore i
        // if P[i] != Q[i] for rest, then done
        // if P[i] = Q[i] != i, we try to set A[i] = P[i] or B[i] = Q[i] and see where that gets us
        // setting A[i] = P[i] fixes the whole cycle containing "i" in A[i]


        /* i is ok if:
         *
         *  P[i] == Q[i] != i => C[i] && !D[i]
         *  P[i] == Q[i] != i => !C[i] && D[i]
         *  P[i] = i != Q[i] => D[i]
         *  Q[i] = i != P[i] => C[i]
         *  i != P[i] != Q[i] != i => always
         *
         */

        /**
         *    0                 C[i]+(C[i]^D[i])
         *    D[i]+(C[i]^D[i])  C[i]+D[i]
         *
         */

        /*
         * each C[j] has score
         * each D[j] has score
         * each C[j]&D[j] has penalty 2*(P[i]==Q[i]) */


        int N; cin >> N;
        vector<int> P(N), Q(N); cin >> P >> Q;
        UnionFind UP(N), UQ(N);
        for (int i = 0; i < N; ++i) {
            UP.unite(i,P[i]);
            UQ.unite(i,Q[i]);
        }

        int ans = 0;
        map<pii, int> R;
        for (int i = 0; i < N; ++i) {
            if (P[i] == Q[i]) {
                if (P[i] != i) {
                    R[{UP.find(i), UQ.find(i)}] += 2;
                    R[{-1, UQ.find(i)}]--;
                    R[{UP.find(i), -1}]--;
                    ans++;
                } else {
                    // this is never a point
                }
            } else if (P[i] == i) {
                R[{-1, UQ.find(i)}]--;
                ++ans;
            } else if (Q[i] == i) {
                R[{UP.find(i), -1}]++;
            } else {
                R[{-1, UQ.find(i)}]--;
                R[{UP.find(i), UQ.find(i)}]++;

                // this is always a point, except if A[i]=i && B[i]=i
                ++ans;
            }
        }

        /* By Default, all A[i] = i && B[i] = Q[i]
         * This means that we get:
         *    - all bonuses for case Q[i] != P[i] == i;
         *        - picking {-1, UQ.find(i)} has penalty -x
         *    - all bonuses for case Q[i] == P[i] != i
         *        - picking {-1, UQ.find(i)} has penalty -x
         *        - picking {UP.find(i), -1} has penalty -x
         *        - there is a project {UP.find(i),UQ.find(i)} that has bonus 2*x
         *    - no bonus for case P[i] != Q[i] == i
         *        - picking {UP.find(i), -1} has bonus +x */

        map<pii, int> I;
        for (auto r: R) {
            int i = I.size();
            I[r.x] = i+2;
        }

        int left = 0, right = 0;
        Dinic<int> X(2 + R.size());
        for (auto r: R) {
//            cout << "edge " << r << endl;
            if (r.y < 0) {
                left += -r.y;
                X.AddEdge(I[r.x], 1, -r.y);
            } else if (r.y > 0) {
                right += r.y;
                X.AddEdge(0, I[r.x], r.y);
            }

            if (r.x.x != -1 && r.x.y != -1) {
                X.AddEdge(I[r.x], I[{-1,r.x.y}], 1000000);
                X.AddEdge(I[r.x], I[{r.x.x,-1}], 1000000);
            }
        }

        int flow = X.GetMaxFlow(0, 1);

        cout << ans + right - flow << endl;

//        cout << ans << ' ' << left << ' ' << right << ' ' << flow << endl;
    }
};


int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	FTwoPermutations solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
    return 0;
}


Measured memory_bytes: 38072