import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


Tuple!(int, int[]) scc(in int[][][] graph) {
  const n = cast(int)(graph[0].length);
  int compN;
  auto compIds = new int[n];
  int[] us;
  void dfs(int s, int u, int a, int b) {
    if (compIds[u] == a) {
      compIds[u] = b;
      foreach (v; graph[s][u]) {
        dfs(s, v, a, b);
      }
      if (s == 0) {
        us ~= u;
      }
    }
  }
  foreach (u; 0 .. n) {
    dfs(0, u, 0, -1);
  }
  foreach_reverse (u; us) {
    if (compIds[u] == -1) {
      dfs(1, u, -1, compN);
      ++compN;
    }
  }
  return tuple(compN, compIds);
}


enum INF = 10^^9;

int solve(const(int) N, const(int[]) A, const(int[]) B) {
  int ret = INF;
  auto possA = new int[][N];
  foreach (h; 0 .. 3 * N) {
    possA[A[h]] ~= h;
  }
  foreach (i; 0 .. 3 * N + 1) foreach (j; i .. 3 * N + 1) {
    bool check() {
      auto possL = new int[][N];
      auto possM = new int[][N];
      auto possR = new int[][N];
      foreach (k; 0 .. i) possL[B[k]] ~= k;
      foreach (k; i .. j) possM[B[k]] ~= k;
      foreach (k; j .. 3 * N) possR[B[k]] ~= k;
      
      // 0: left first
      auto tos = new int[][2][N];
      auto cons = new int[][N];
      auto forces = new int[N];
      forces[] = -1;
      foreach (u; 0 .. N) {
        foreach (s; 0 .. 2) {
          tos[u][s] = new int[possM[u].length];
        }
        switch (possM[u].length) {
          case 0: {
            switch (possL[u].length) {
              case 0: {
                // 0+3
                return false;
              } break;
              case 1: {
                // 1+2
                forces[u] = 1;
                cons[u] = [possL[u][0], possR[u][$ - 1]];
              } break;
              case 2: {
                // 2+1
                forces[u] = 0;
                cons[u] = [possL[u][0], possR[u][$ - 1]];
              } break;
              case 3: {
                // 3+0
                return false;
              } break;
              default: assert(false);
            }
          } break;
          case 1: {
            switch (possL[u].length) {
              case 0: {
                // 0+2
                foreach (s; 0 .. 2) {
                  tos[u][s][0] = possA[u][0];
                }
              } break;
              case 1: {
                // 1+1
                tos[u][0][0] = possA[u][0];
                tos[u][1][0] = possA[u][2];
                cons[u] = [possL[u][0], possR[u][$ - 1]];
              } break;
              case 2: {
                // 2+0
                foreach (s; 0 .. 2) {
                  tos[u][s][0] = possA[u][2];
                }
              } break;
              default: assert(false);
            }
          } break;
          case 2: {
            foreach (s; 0 .. 2) {
              tos[u][s][0] = possA[u][0];
              tos[u][s][1] = possA[u][2];
            }
          } break;
          case 3: {
            foreach (s; 0 .. 2) {
              foreach (x; 0 .. 3) {
                tos[u][s][x] = possA[u][x];
              }
            }
          } break;
          default: assert(false);
        }
      }
      
      auto graph = new int[][][](2, N << 1);
      foreach (u; 0 .. N) {
        if (forces[u] != -1) {
          graph[0][u << 1 | (forces[u] ^ 1)] ~= (u << 1 | forces[u]);
          graph[1][u << 1 | forces[u]] ~= (u << 1 | (forces[u] ^ 1));
        }
      }
      foreach (u; 0 .. N) foreach (v; u + 1 .. N) {
        bool[2][2] ngs;
        foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
          foreach (x; 0 .. cast(int)(possM[u].length)) foreach (y; 0 .. cast(int)(possM[v].length)) {
            if ((possM[u][x] < possM[v][y]) != (tos[u][s][x] < tos[v][t][y])) {
              ngs[s][t] = true;
            }
          }
        }
        if (cons[u] && cons[v]) {
          if (cons[u][0] < cons[v][0] && cons[u][1] < cons[v][1]) {
            ngs[1][0] = true;
          }
          if (cons[u][0] > cons[v][0] && cons[u][1] > cons[v][1]) {
            ngs[0][1] = true;
          }
        }
        foreach (s; 0 .. 2) foreach (t; 0 .. 2) {
          if (ngs[s][t]) {
            graph[0][u << 1 | s] ~= (v << 1 | (t ^ 1));
            graph[1][v << 1 | (t ^ 1)] ~= (u << 1 | s);
            graph[0][v << 1 | t] ~= (u << 1 | (s ^ 1));
            graph[1][u << 1 | (s ^ 1)] ~= (v << 1 | t);
          }
        }
      }
      const res = scc(graph);
      foreach (u; 0 .. N) {
        if (res[1][u << 1 | 0] == res[1][u << 1 | 1]) {
          return false;
        }
      }
      return true;
    }
    
    const res = check();
    if (res) {
      debug {
        writeln("OK ", i, " ", j);
      }
      chmin(ret, 3 * N - (j - i));
    }
  }
  return ret;
}

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto A = new int[3 * N];
      foreach (i; 0 .. 3 * N) {
        A[i] = readInt() - 1;
      }
      auto B = new int[3 * N];
      foreach (i; 0 .. 3 * N) {
        B[i] = readInt() - 1;
      }
      
      const ans = solve(N, A, B);
      writeln((ans >= INF) ? -1 : ans);
    }
  } catch (EOFException e) {
  }
}
