Problem: Problem Statement

One day (call it day 0), you find a permutation $P$ of $N$ integers written on the blackboard in a single row. Fortunately you have another permutation $Q$ of $N$ integers, so you decide to play with these permutations.

Every morning of the day 1,2,3,... , you rewrite every number on the blackboard in such a way that erases the number $x$ and write the number $Q_x$ at the same position. Please find the minimum non-negative integer $d$ such that in the evening of the day $d$ the sequence on the blackboard is sorted in increasing order.

* * *

Input

The input consists of a single test case in the format below.

> $N$ $P_1$ $\ldots$ $P_N$ $Q_1$ $\ldots$ $Q_N$

The first line contains an integer $N$ ($1 \leq N \leq 200$). The second line contains $N$ integers $P_1,\ldots,P_N$ ($1 \leq P_i \leq N$) which represent the permutation $P$. The third line contains $N$ integers $Q_1,\ldots,Q_N$ ($1 \leq Q_i \leq N$) which represent the permutation $Q$.

Output

Print the minimum non-negative integer $d$ such that in the evening of the day $d$ the sequence on the blackboard is sorted in increasing order. If such $d$ does not exist, print -1 instead.

It is guaranteed that the answer does not exceed $10^{18}$.

Examples

Input| Output
---|---


6
2 3 1 4 5 6
3 1 2 5 4 6


|


4



6
1 2 3 4 5 6
3 1 2 5 4 6


|


0



6
2 3 1 4 5 6
3 4 5 6 1 2


|


-1






Example

Input




Output

Solution (in CPP):

Solution:
#include<iostream>
using namespace std;
#include<vector>
long long invmod(long long a,long long m)
{
	long long s=a%m,t=m,sx=1,sy=0,tx=0,ty=1;
	while(s%t!=0)
	{
		long long f=s/t;
		long long u=s-t*f,ux=sx-tx*f,uy=sy-ty*f;
		s=t,sx=tx,sy=ty;
		t=u,tx=ux,ty=uy;
	}
	if(tx<0)tx+=m;
	return tx;
}
long long garner(const vector<long long>&x,const vector<long long>&m,const int mod=0)//*=x mod m
{
	if(x.empty())return 0LL;
	vector<long long>v(x.size());
	v[0]=x[0];
	for(int i=1;i<x.size();i++)
	{
		long long X=(x[i]-x[0])%m[i];
		long long M=1;
		for(int j=0;j<i-1;j++)
		{
			(M*=m[j])%=m[i];
			(X-=v[j+1]*M)%=m[i];
		}
		(M*=m[i-1])%=m[i];
		if(X<0)X+=m[i];
		v[i]=X*invmod(M,m[i])%m[i];
	}
	long long ret=v[0],p=1;
	if(mod==0)
	{
		for(int i=1;i<x.size();i++)
		{
			p*=m[i-1];
			ret+=p*v[i];
		}
	}
	else
	{
		ret%=mod;
		for(int i=1;i<x.size();i++)
		{
			(p*=m[i-1])%=mod;
			(ret+=p*v[i])%=mod;
		}
	}
	return ret;
}
#include<algorithm>
long long garner_helper(vector<int>A,vector<int>B,const int mod=0,bool zero=true)//allow zero?
//make B disjoint. NA => -1
{
	vector<int>primes;
	for(int i=0;i<A.size();i++)
	{
		int b=B[i];
		for(int j=2;j*j<=b;j++)
		{
			if(b%j==0)
			{
				primes.push_back(j);
				while(b%j==0)b/=j;
			}
		}
		if(b>1)primes.push_back(b);
	}
	sort(primes.begin(),primes.end());
	primes.erase(unique(primes.begin(),primes.end()),primes.end());
	vector<long long>a,b;
	bool flag=!zero;
	long long ret=1;
	for(int p:primes)
	{
		int ma=1,res;
		vector<pair<int,int> >x;
		for(int i=0;i<A.size();i++)
		{
			if(B[i]%p==0)
			{
				int t=1;
				while(B[i]%p==0)
				{
					B[i]/=p;
					t*=p;
				}
				x.push_back(make_pair(t,A[i]%t));
				if(ma<t)ma=t,res=x.back().second;
				A[i]%=B[i];
			}
		}
		for(pair<int,int>q:x)
		{
			if(res%q.first!=q.second)return-1;
		}
		a.push_back(res);
		b.push_back(ma);
		flag&=res==0;
		if(flag)
		{
			ret*=ma;
			if(mod!=0)ret%=mod;
		}
	}
	if(!flag)ret=garner(a,b,mod);
	return ret;
}
int ap[200][200];
int main()
{
	int N;
	cin>>N;
	vector<int>P(N),Q(N);
	for(int i=0;i<N;i++)cin>>P[i],P[i]--;
	for(int i=0;i<N;i++)cin>>Q[i],Q[i]--;
	vector<int>A(N),B(N),tmp(N,1);
	for(int i=1;i<=N+1;i++)
	{
		for(int j=0;j<N;j++)
		{
			if(ap[j][P[j]]==0)
			{
				ap[j][P[j]]=i;
			}
			else
			{
				if(B[j]==0)B[j]=i-ap[j][P[j]];
			}
			P[j]=Q[P[j]];
		}
	}
	for(int i=0;i<N;i++)
	{
		A[i]=ap[i][i]-1;
		if(A[i]<0)
		{
			cout<<-1<<endl;
			return 0;
		}
	}
	cout<<garner_helper(A,B)<<endl;
}