#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ll long long
#define endl '\n'
using namespace std;
const int N = 1e5 + 5;

int n, a[4][N];

int inv[2], flip[2];
int label[N];

void add_col(vector <int> v, int id)
{
    if(abs(v[2] - v[0]) != 2 || abs(v[1] - v[0]) != 1 || v[1] % 3 < 2)
    {
        cout << "No" << endl;
        exit(0);
    }

    int parity = !(v[1] & 1);
    if(v[0] > v[2])
        flip[parity]++;

    label[(v[1] + 1) / 3] = id;
    if(id & 1 != parity)
    {
        cout << "No" << endl;
        exit(0);
    }
}

bool vis[N];
int comp_sz;
void DFS(int v)
{
    vis[v] = true;
    comp_sz++;
    if(!vis[label[v]])
        DFS(label[v]);
}


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);

    int n;
    cin >> n;
    for(int i = 1; i <= 3; i++)
        for(int j = 1; j <= n; j++)
            cin >> a[i][j];

    for(int j = 1; j <= n; j++)
    {
        vector <int> v = {a[1][j], a[2][j], a[3][j]};
        add_col(v, j);
    }

    for(int i = 1; i <= n; i++)
    {
        if(!vis[i])
        {
            comp_sz = 0;
            DFS(i);
            inv[i & 1] += (comp_sz - 1);
        }
    }

    inv[0] &= 1;
    inv[1] &= 1;
    flip[0] &= 1;
    flip[1] &= 1;
    if(inv[0] != flip[1] || inv[1] != flip[0])
        cout << "No" << endl;
    else
        cout << "Yes" << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define rp(i, k, n) for (int i = k; i < n; i++)
typedef long long ll;
typedef double ld;

template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7ll;
const double PI=3.14159265358979323846;



int main() {
    int n; cin >> n;
    vector<vector<int>> graph(n);
    rp(i, 0, n) {
        rp(j, 0, n-1) {
            int a; scanf("%d", &a); a--;
            graph.at(i).emplace_back(a);
        }
    }

    vector<int> prog(n);
    set<int> s;
    rp(i, 0, n) s.insert(i);
    int day = 0;
    while(!s.empty()) {
        set<int> next_s;
        for(auto x: s){
            int y = graph.at(x).at(prog.at(x));
            if(x == graph.at(y).at(prog.at(y))) {
                if(prog.at(x) < n-2) next_s.insert(x);
                if(prog.at(y) < n-2) next_s.insert(y);
            }
        }

        for(auto x: next_s) {
            // printf("%d ", x);
            prog.at(x) ++;
        }
        // printf("\n");
        day++;
        s = next_s;
    }
    rp(i, 0, n) {
        if(prog.at(i) < n-2) {
            cout << -1 << endl;
            return 0;
        }
    }
    cout << day << endl;
    return 0;
}