/* cerberus97 - Hanit Banga */

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

#define pb push_back
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL)

typedef long long ll;
typedef long long LL;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;

struct Edge {
  int u, v;
  LL cap, flow;
  Edge() {}
  Edge(int u, int v, LL cap): u(u), v(v), cap(cap), flow(0) {}
};

struct Dinic {
  int N;
  vector<Edge> E;
  vector<vector<int>> g;
  vector<int> d, pt;
  
  Dinic(int N): N(N), E(0), g(N), d(N), pt(N) {}

  void AddEdge(int u, int v, LL cap) {
    if (u != v) {
      E.emplace_back(Edge(u, v, cap));
      g[u].emplace_back(E.size() - 1);
      E.emplace_back(Edge(v, u, 0));
      g[v].emplace_back(E.size() - 1);
    }
  }

  bool BFS(int S, int T) {
    queue<int> q({S});
    fill(d.begin(), d.end(), N + 1);
    d[S] = 0;
    while(!q.empty()) {
      int u = q.front(); q.pop();
      if (u == T) break;
      for (int k: g[u]) {
        Edge &e = E[k];
        if (e.flow < e.cap && d[e.v] > d[e.u] + 1) {
          d[e.v] = d[e.u] + 1;
          q.emplace(e.v);
        }
      }
    }
    return d[T] != N + 1;
  }

  LL DFS(int u, int T, LL flow = -1) {
    if (u == T || flow == 0) return flow;
    for (int &i = pt[u]; i < g[u].size(); ++i) {
      Edge &e = E[g[u][i]];
      Edge &oe = E[g[u][i]^1];
      if (d[e.v] == d[e.u] + 1) {
        LL amt = e.cap - e.flow;
        if (flow != -1 && amt > flow) amt = flow;
        if (LL pushed = DFS(e.v, T, amt)) {
          e.flow += pushed;
          oe.flow -= pushed;
          return pushed;
        }
      }
    }
    return 0;
  }

  LL MaxFlow(int S, int T) {
    LL total = 0;
    while (BFS(S, T)) {
      fill(pt.begin(), pt.end(), 0);
      while (LL flow = DFS(S, T))
        total += flow;
    }
    return total;
  }
};

const int N = 1e5 + 10;

int get_root(int u, vector<int> &par);
void merge(int u, int v, vector<int> &par);

int main() {
	fast_cin();
	int n; cin >> n;
	vector<int> p(n), q(n), x(n), y(n);
	iota(x.begin(), x.end(), 0);
	iota(y.begin(), y.end(), 0);
	for (int i = 0; i < n; ++i) {
		cin >> p[i];
		merge(i, p[i], x);
	}
	for (int i = 0; i < n; ++i) {
		cin >> q[i];
		merge(i, q[i], y);
	}
	for (int i = 0; i < n; ++i) {
		get_root(i, x);
		get_root(i, y);
	}
	for (int i = 0; i < n; ++i) {
		x[i] = 2 * x[i];
		y[i] = 2 * y[i] + 1;
	}
	int nodes = 2 * n + 2, source = 2 * n, sink = 2 * n + 1;
	Dinic dinic(nodes);
	int ans = n;
	for (int i = 0; i < n; ++i) {
		if (p[i] == i and q[i] == i) {
			--ans;
		} else if (p[i] == i) {
			dinic.AddEdge(y[i], sink, 1);
		} else if (q[i] == i) {
			dinic.AddEdge(source, x[i], 1);
		} else if (p[i] == q[i]) {
			dinic.AddEdge(x[i], y[i], 1);
			dinic.AddEdge(y[i], x[i], 1);
		} else {
			dinic.AddEdge(y[i], x[i], 1);
		}
	}
	cout << ans - dinic.MaxFlow(source, sink) << endl;
}

int get_root(int u, vector<int> &par) {
	if (u != par[u]) {
		par[u] = get_root(par[u], par);
	}
	return par[u];
}

void merge(int u, int v, vector<int> &par) {
	u = get_root(u, par);
	v = get_root(v, par);
	if (u != v) {
		par[u] = v;
	}
}

Measured memory_bytes: 24492