#define _USE_MATH_DEFINES #include using namespace std; //template #define rep(i,a,b) for(int i=(a);i<(b);i++) #define ALL(v) (v).begin(),(v).end() typedef long long int ll; const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12; templateinline bool chmax(T& a,T b){if(ainline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;} templateinline T get(){ char c=getchar(); bool neg=(c=='-'); T res=neg?0:c-'0'; while(isdigit(c=getchar()))res=res*10+(c-'0'); return neg?-res:res; } templateinline void put(T x,char c='\n'){ if(x<0)putchar('-'),x*=-1; int d[20],i=0; do{d[i++]=x%10;}while(x/=10); while(i--)putchar('0'+d[i]); putchar(c); } //end struct SCC{ vector> g, gr; int n, k; vector cmp, vs; vector used; void dfs(int x){ used[x]=1; for(auto y:g[x]){ if(!used[y]) dfs(y); } vs.push_back(x); } void rdfs(int v, int k){ used[v]=1; cmp[v]=k; for(auto y:gr[v]){ if(!used[y]) rdfs(y, k); } } SCC(const vector> &g):g(g), n(g.size()), cmp(n), used(n){ gr.resize(n); for(int x=0; x=0; i--){ if(!used[vs[i]]) rdfs(vs[i], k++); } } }; int n,S[510],T[510],U[510]; bool a[250100]; int pos(int i,int j){return i*n+j;} int main(){ n=get(); rep(i,0,n)S[i]=get(),S[i]--; rep(i,0,n)T[i]=get(),T[i]--; rep(i,0,n)U[i]=get(); int m=n*n; vector> g(m*2+1); rep(i,0,n)rep(j,0,n){ int x=pos(S[i],j)+1,y=pos(j,T[i])+1; if(U[i]&1)x*=-1; if(U[i]&2)y*=-1; g[m-x].push_back(m+y); g[m-y].push_back(m+x); } SCC scc(g); rep(i,1,m+1){ if(scc.cmp[m-i]==scc.cmp[m+i]){ put(-1); return 0; } a[i]=(scc.cmp[m-i]