#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <bitset>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

const int MAXN = 200005;
const int MAXM = 300005;
const int INF = (1 << 30) - 1;

struct Edge
{
	int to;
	int cap;
	int nxt;
}edge[MAXM << 1];

int n,id = 1,ans,ca,cb,S,T;
int a[MAXN];
int b[MAXN];
int first[MAXN];
int dis[MAXN];
int cur[MAXN];
int bela[MAXN];
int belb[MAXN];
int vala[MAXN];
int valb[MAXN];

bool vis[MAXN];

queue<int> Q;
map<int,int> e[MAXN];

void addE(int u,int v,int c)
{
	edge[++id] = (Edge){v,c,first[u]};
	first[u] = id;
	edge[++id] = (Edge){u,0,first[v]};
	first[v] = id;
}

bool bfs()
{
	memset(dis,-1,sizeof(dis));
	dis[S] = 0;
	Q.push(S);
	while (!Q.empty())
	{
		int u = Q.front();
		Q.pop();
		for (int i = first[u];i;i = edge[i].nxt)
			if (edge[i].cap > 0 && dis[edge[i].to] == -1)
			{
				dis[edge[i].to] = dis[u] + 1;
				Q.push(edge[i].to);
			}
	}
	return ~dis[T];
}

int dfs(int u,int f)
{
	if (u == T)
		return f;
	int res = 0;
	for (int &i = cur[u];i;i = edge[i].nxt)
		if (edge[i].cap > 0 && dis[edge[i].to] == dis[u] + 1)
		{
			int ret = dfs(edge[i].to,min(edge[i].cap,f));
			edge[i].cap -= ret;
			edge[i ^ 1].cap += ret;
			f -= ret;
			res += ret;
			if (!f)
				break;
		}
	return res;
}

int Dinic()
{
	int res = 0;
	while (bfs())
	{
		memcpy(cur,first,sizeof(cur));
		while (int tot = dfs(S,INF))
		{
			res += tot;
			memcpy(cur,first,sizeof(cur));
		}
	}
	return res;
}

int main()
{
	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]++;
	}
	for (int i = 1;i <= n;i++)
	{
		if (vis[i])
			continue;
		ca++;
		int cur = i;
		while (!vis[cur])
		{
			vis[cur] = 1;
			bela[cur] = ca;
			cur = a[cur];
		}
	}
	memset(vis,0,sizeof(vis));
	for (int i = 1;i <= n;i++)
	{
		if (vis[i])
			continue;
		cb++;
		int cur = i;
		while (!vis[cur])
		{
			vis[cur] = 1;
			belb[cur] = cb;
			cur = b[cur];
		}
	}
	ans = n;
	S = 0,T = ca + cb + 1;
	for (int i = 1;i <= n;i++)
	{
		if (a[i] == i && b[i] == i)
			ans--;
		else if (a[i] == i)
			addE(S,belb[i] + ca,1);
		else if (b[i] == i)
			addE(bela[i],T,1);
		else
		{
			addE(bela[i],belb[i] + ca,1);
			if (a[i] == b[i])
				addE(belb[i] + ca,bela[i],1);
		}
	}
	printf("%d\n",ans - Dinic());
	return 0;
}