#include <bits/stdc++.h>
#include <atcoder/all>

using namespace atcoder;

constexpr int N = 33;

int n;
int a[3 * N], b[3 * N];
int pos[N][3], c[N];
int posb[N][3];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    std::cin >> n;
    for (int i = 0; i < 3 * n; ++i) {
        std::cin >> a[i];
        --a[i];
        pos[a[i]][c[a[i]]++] = i;
    }
    for (int i = 0; i < n; ++i)
        c[i] = 0;
    for (int i = 0; i < 3 * n; ++i) {
        std::cin >> b[i];
        --b[i];
        posb[b[i]][c[b[i]]++] = i;
    }
    
    int ans = 3 * n + 1;
    
    for (int l = 0; l <= 3 * n; ++l) {
        for (int r = l; r <= 3 * n; ++r) {
            int cntl[N] = {}, cntr[N] = {};
            for (int i = 0; i < l; ++i)
                ++cntl[b[i]];
            for (int i = r; i < 3 * n; ++i)
                ++cntr[b[i]];
            bool ok = true;
            for (int i = 0; i < n; ++i)
                if (cntl[i] == 3 || cntr[i] == 3)
                    ok = false;
            if (!ok)
                continue;
            two_sat ts(n);
            int pa[3 * N];
            std::memset(pa, -1, sizeof(pa));
            for (int i = 0; i < n; ++i) {
                if (cntl[i] == 1 && cntr[i] == 0) {
                    pa[posb[i][1]] = pos[i][0];
                    pa[posb[i][2]] = pos[i][2];
                } else if (cntl[i] == 0 && cntr[i] == 1) {
                    pa[posb[i][0]] = pos[i][0];
                    pa[posb[i][1]] = pos[i][2];
                } else if (cntl[i] == 2 && cntr[i] == 0) {
                    pa[posb[i][2]] = pos[i][2];
                } else if (cntl[i] == 0 && cntr[i] == 2) {
                    pa[posb[i][0]] = pos[i][0];
                } else if (cntl[i] == 0 && cntr[i] == 0) {
                    pa[posb[i][0]] = pos[i][0];
                    pa[posb[i][1]] = pos[i][1];
                    pa[posb[i][2]] = pos[i][2];
                }
            }
            for (int i = l; i < r; ++i)
                for (int j = i + 1; j < r; ++j)
                    if (pa[i] != -1 && pa[j] != -1 && pa[i] > pa[j])
                        ok = false;
            if (!ok)
                continue;
            for (int i = 0; i < n; ++i) {
                if (cntl[i] != 2 || cntr[i] != 1)
                    continue;
                for (int j = 0; j < n; ++j) {
                    if (cntl[j] != 1 || cntr[j] != 2)
                        continue;
                    int il = posb[i][0], ir = posb[i][2];
                    int jl = posb[j][0], jr = posb[j][2];
                    if (il > jl && ir > jr)
                        ok = false;
                }
            }
            if (!ok)
                continue;
            for (int i = 0; i < n; ++i) {
                if (cntl[i] == 1 && cntr[i] == 1) {
                    bool ok0 = true, ok2 = true;
                    int p = pos[i][0];
                    for (int j = l; j < r; ++j)
                        if (pa[j] != -1 && ((j < posb[i][1] && pa[j] > p) || (j > posb[i][1] && pa[j] < p)))
                            ok0 = false;
                    p = pos[i][2];
                    for (int j = l; j < r; ++j)
                        if (pa[j] != -1 && ((j < posb[i][1] && pa[j] > p) || (j > posb[i][1] && pa[j] < p)))
                            ok2 = false;
                    for (int j = 0; j < n; ++j) {
                        if (i == j)
                            continue;
                        if (cntl[j] == 1 && cntr[j] == 1) {
                            bool ls = posb[i][1] < posb[j][1];
                            for (int x = 0; x < 2; ++x)
                                for (int y = 0; y < 2; ++y)
                                    if ((pos[i][2 * x] < pos[j][2 * y]) != ls)
                                        ts.add_clause(i, !x, j, !y);
                            if (posb[i][0] < posb[j][0] && posb[i][2] < posb[j][2])
                                ts.add_clause(i, 0, j, 1);
                        } else if (cntl[j] == 2 && cntr[j] == 1) {
                            if (posb[i][0] < posb[j][0] && posb[i][2] < posb[j][2])
                                ok2 = false;
                        } else if (cntl[j] == 1 && cntr[j] == 2) {
                            if (posb[i][0] > posb[j][0] && posb[i][2] > posb[j][2])
                                ok0 = false;
                        }
                    }
                    if (!ok0)
                        ts.add_clause(i, 1, i, 1);
                    if (!ok2)
                        ts.add_clause(i, 0, i, 0);
                }
            }
            if (ts.satisfiable())
                ans = std::min(ans, 3 * n - (r - l));
        }
    }
    
    if (ans > 3 * n)
        ans = -1;
    
    std::cout << ans << "\n";
    
    return 0;
}

Measured memory_bytes: 3820