#include<bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
 
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, 1, -1};

vector<int> chidx;
int n;

int check(){
    REP(i, 0, n){
        if(chidx[i] != n-1){
            return 1;
        }
    }
    return 0;
}

int main(){
    cin >> n;
    chidx = vector<int>(n, 0);
    vector<vector<int> > lis(n, vector<int>(n-1));
    REP(i, 0, n){
        REP(l, 0, n-1){
            cin >> lis[i][l];
            lis[i][l]--;
        }
    }
    int ans = 0;
    while(check() == 1){
        ans++;
        vector<int> used(n, 0);
        int f = 0;
        REP(i, 0, n){
            if(chidx[i] == n-1){
                continue;
            }
            int to = lis[i][chidx[i]];
            if(used[i] == 1 || used[to] == 1){
                continue;
            }
            if(chidx[to] == n-1){
                cout << -1 << endl;
                return 0;
            }
            if(lis[to][chidx[to]] == i){
                chidx[i]++;
                chidx[to]++;
                used[i] = 1;
                used[to] = 1;
                f = 1;
            }
        }
        if(f == 0){
            cout << -1 << endl;
            return 0;
        }
    }
    cout << ans << endl;
}
#include<bits/stdc++.h>
using namespace std;
int flip[2],mv[2];
vector <int> vec[2];
bool vis[1000100];
int a[1000100],b[1000100],c[1000100],go[1000100];
int work(vector <int> &vec)
{
	//for (int i=0;i<vec.size();i++) cout<<vec[i]<<' ';cout<<"\n\n";
	int ans=0;
	memset(vis,0,sizeof(vis));
	for (int i=0;i<vec.size();i++) if (!vis[i])
	{
		int now=i;
		while (!vis[now])
		{
			vis[now]=true;
			ans++;
			now=vec[now];
		}
		ans--;
	}
	return ans&1;
}
int main()
{
	int n;scanf("%d",&n);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=1;i<=n;i++) scanf("%d",&b[i]);
	for (int i=1;i<=n;i++) scanf("%d",&c[i]);
	for (int i=1;i<=n;i++)
	{
		if ((a[i]+1==b[i])&&(b[i]+1==c[i])) {go[i]=c[i];}
		else if ((c[i]+1==b[i])&&(b[i]+1==a[i])) {flip[i&1]^=1;go[i]=a[i];}
		else {printf("No\n");return 0;}
		
		if (((go[i]/3)&1)!=(i&1)) {printf("No\n");return 0;}
		vec[i&1].push_back((go[i]/3-1)/2);
	}
	mv[0]=work(vec[0]);mv[1]=work(vec[1]);
	puts(((mv[0]==flip[1])&&(mv[1]==flip[0]))?"Yes":"No");
}