//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define trav(x,a) for (const auto& x: a)
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), x.end()
#define mem(a,v) memset((a), (v), sizeof (a))
#define enl printf("\n")
#define case(t) printf("Case #%d: ", (t))
#define ni(n) scanf("%d", &(n))
#define nl(n) scanf("%lld", &(n))
#define nai(a, n) for (int _i = 0; _i < (n); _i++) ni(a[_i])
#define nal(a, n) for (int _i = 0; _i < (n); _i++) nl(a[_i])
#define pri(n) printf("%d\n", (n))
#define prl(n) printf("%lld\n", (n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vll vector<pll>
#define vi vector<int>
#define vl vector<long long>
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef cc_hash_table<int,int,hash<int>> ht;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> oset;
const double pi = acos(-1);
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int N = 1e6 + 5;
const double eps = 1e-9;
int P[N], Q[N];
int iP[N], iQ[N];
map<pii, int> cnt[2][2];

struct edge {
  int v, c, f;
};

vector<edge> e;
int m = 0;
vector<int> adj[N];

void add(int u, int v, int c = 1) {
  e.push_back({v, c, 0});
  e.push_back({u, c, c});
  adj[u].push_back(m++);
  adj[v].push_back(m++);
}

int cid[N], lvl[N];

bool bfs(int s, int t) {
  fill(lvl, lvl+N, N);
  deque<int> pq;
  pq.push_back(s);
  lvl[s] = 0;
  while (sz(pq)) {
    int u = pq.front(); pq.pop_front();
    trav(i, adj[u]) if (e[i].f != e[i].c && lvl[e[i].v] == N) {
      lvl[e[i].v] = lvl[u] + 1;
      pq.push_back(e[i].v);
    }
  }
  return lvl[t] != N;
}

int dfs(int u, int t, int psh) {
  if (psh == 0 || u == t)
    return psh;
  for (; cid[u] < sz(adj[u]); cid[u]++) {
    int i = adj[u][cid[u]];
    if (e[i].c != e[i].f && lvl[u]+1 == lvl[e[i].v]) {
      int ret = dfs(e[i].v, t, min(psh, e[i].c - e[i].f));
      if (ret != 0) {
        e[i].f += ret;
        e[i^1].f -= ret;
        return ret;
      }
    }
  }
  return 0;
}

int flow(int s, int t) {
  int ret = 0, v = 0;
  while (bfs(s, t)) {
    fill(cid, cid+N, 0);
    while ((v = dfs(s, t, N)) != 0)
      ret += v;
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int n; cin >> n;
  for (int i = 0; i < n; i++)
    cin >> P[i], iP[i] = -1;
  for (int i = 0; i < n; i++)
    cin >> Q[i], iQ[i] = -1;
  int cP = 0;
  for (int i = 0; i < n; i++) if (iP[i] < 0) {
    int j = i;
    do {
      iP[j] = cP;
      j = P[j];
    } while (j != i);
    cP++;
  }
  int cQ = 0;
  for (int i = 0; i < n; i++) if (iQ[i] < 0) {
    int j = i;
    do {
      iQ[j] = cQ;
      j = Q[j];
    } while (j != i);
    cQ++;
  }
  int ans = n;
  for (int i = 0; i < n; i++) {
    if (P[i] == i && Q[i] == i)
      ans--;
    else if (P[i] == i)
      add(1 + cP + iQ[i], 1 + cP + cQ);
    else if (Q[i] == i)
      add(0, 1 + iP[i]);
    else if (P[i] != Q[i])
      add(1 + cP + iQ[i], 1 + iP[i]);
    else
      add(1 + cP + iQ[i], 1 + iP[i]), add(1 + iP[i], 1 + cP + iQ[i]);
  }
  cout << ans-flow(0, 1 + cP + cQ) << endl;
  return 0;
}

Measured memory_bytes: 46300