#include<bits/stdc++.h>
#define no {puts("No");return 0;}
#define yes {puts("Yes");return 0;}
#define nc() getchar()
using namespace std;
const int N=300010;

/*inline char nc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}*/
inline int read(){
    char ch=nc();int sum=0;
    while(!(ch>='0'&&ch<='9'))ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum;
}
 
int c[N],n;

void add(int x,int k){
	while(x<=n){
		c[x]+=k;
		x+=x&-x;
	}
}

int query(int x){
	int ans=0;
	while(x){
		ans+=c[x];
		x-=x&-x;
	}
	return ans;
}

int a[N],fu[N],s[N];
bool vis[N];

int main(){
	n=read();
	int s1=0,s2=0;
	for(int i=1;i<=3;i++){
		for(int j=1;j<=n;j++){
			int x;
			x=read();
			if(i==1){
				if(x%3==0){
					a[j]=x/3;
					
					fu[j]=1;
					if(j&1) s1++;
					
					else s2++;
				}else{
					if(x%3!=1) {no;}
					a[j]=x/3+1;
				}
				if(vis[a[j]]) {no;}
				if((j&1)^(a[j]&1)) {no;}
				vis[a[j]]=1;
			}
			else{
				if(fu[j]){
					if(x!=a[j]*3-i+1) {no;}
				}else{
			//		printf("a(%d)=%d\n",j,a[j]);
					if(x!=(a[j]-1)*3+i) {no;}
				}
			}
		}
	}
//	for(int i=1;i<=n;i++) printf("%d ",a[i]);cout<<endl;
	
	int i;
	
	if(n&1) i=n;else i=n-1;
	
	for(;i>0;i-=2){
		s2+=query(a[i]);
		add(a[i],1);
	}
	
	if(n&1) i=n-1;else i=n;
	
	memset(c,0,sizeof(c));
	
	for(;i>0;i-=2){
		s1+=query(a[i]);
		add(a[i],1);
	}
	
	
//	printf("%d %d\n",s1,s2);
	
	if(s1%2==0&&s2%2==0) {yes;}
	else {no;}
	
	return 1;
}
			
	
#define _USE_MATH_DEFINES
#define INF 100000000

#include <iostream>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair <int,int> P;


static const double EPS = 1e-8;

char stage[20][20];

int tx[] = {-1,0,1,0};
int ty[] = {0,1,0,-1};

class Data{
public:
	char dx;
	char dy;
	char stage[20][20];
	Data(char dx,char dy,char stage[20][20]){
		this->dx = dx;
		this->dy = dy;
		memcpy(this->stage,stage,sizeof(char)*(20*20));
	}
};


typedef pair <int,Data> PP;

int main(){

	int w,h;
	char sx,sy,gx,gy;
	while(~scanf("%d %d",&w,&h)){
		if(w==h && h==0) break;

		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				int n;
				scanf("%d",&n);
				stage[y][x] = n;

				if(stage[y][x]==2){
					sx=x;
					sy=y;
				}
				else if(stage[y][x]==3){
					gx=x;
					gy=y;
				}
			}
		}

		queue<PP> que;

		que.push(PP(0,Data(sx,sy,stage)));

		int res = 11;
		while(!que.empty()){
			int c = que.front().first;

			int qx = que.front().second.dx;
			int qy = que.front().second.dy;

			char tmpStage[20][20];
			memcpy(tmpStage,que.front().second.stage,sizeof(char)*(20*20));

			que.pop();

			for(int i=0;i<4;i++){
				for(int d=1;d<=max(w,h);d++){
					int dx = qx + tx[i]*d;
					int dy = qy + ty[i]*d;
					if(dx < 0 || dx >= w || dy < 0 || dy >= h) break;
					if(d==1 && tmpStage[dy][dx] == 1) break;

					if(tmpStage[dy][dx] == 1){
						
						char mx = dx - tx[i];
						char my = dy - ty[i];

						if(c+1 <= 10){
							//cost[my][mx] = c+1;
							tmpStage[dy][dx] = 0;
							que.push(PP(c+1,Data(mx,my,tmpStage)));	
							//cout << mx << " " << my << endl;
							tmpStage[dy][dx] = 1;
						}
						
						break;
					}
					else if(tmpStage[dy][dx] == 3){
						if(c+1 <= 10){
							res = min(res,c+1);
						}
						break;
					}
				}

			}
		}

		printf("%d\n", res > 10 ? -1 : res);
	}
}