#include <bits/stdc++.h>
using namespace std;
int fx, vis[200002], c1[200002], c2[200002], c3[200002], c4[200002], t1, t2, t3,
    t4, xi, yi, pos[4], et = 1, e[200002], cd[200002 << 1], nt[200002 << 1],
                        a[200002], b[200002], fa[200002], d[200002], n, wt,
                        ss[19];
char fl[1 << 23], *A = fl;
bool fg;
bool u[200002];
void read(int& x) {
  char c;
  for (x = 0; '0' > (c = *A++) || c > '9';)
    ;
  while ('0' <= c && c <= '9') x = (x << 3) + (x << 1) + c - 48, (c = *A++);
}
void print(long long x) {
  if (!x)
    putchar(48);
  else {
    for (wt = 0; x; ss[++wt] = x % 10, x /= 10)
      ;
    for (; wt; putchar(ss[wt--] + 48))
      ;
  }
}
void dfs(int x, int fe) {
  for (int y = e[x]; y; y = nt[y])
    if (y != fe) fa[cd[y]] = x, d[cd[y]] = d[x] + 1, dfs(cd[y], y ^ 1);
}
void prin(int x, int y) {
  if (x < y)
    print(x), putchar(' '), print(y), putchar(' ');
  else
    print(y), putchar(' '), print(x), putchar(' ');
}
bool cmp(int x, int y) { return d[x] > d[y]; }
long long min(long long a, long long b) { return a > b ? b : a; }
int main() {
  int i, x, y, X, Y;
  long long tmp;
  for (*(fl + fread(fl, 1, 1 << 23, stdin)) = EOF, read(n), i = 1; i <= n; i++)
    if (read(a[i]), !a[i]) X = i;
  for (i = 1; i <= n; i++)
    if (read(b[i]), !b[i]) Y = i;
  for (i = 1; i < n; read(x), read(y), cd[++et] = y, nt[et] = e[x], e[x] = et,
      cd[++et] = x, nt[et] = e[y], e[y] = et, i++)
    ;
  for (dfs(X, 0), u[X] = true, y = Y; y != X;
       u[y] = true, swap(b[y], b[fa[y]]), y = fa[y])
    ;
  for (i = 1; i <= n; i++)
    if (a[i] != b[i]) {
      int ti = 0;
      fg = true;
      for (y = e[i]; y && ti <= 1; y = nt[y])
        if (a[cd[y]] != b[cd[y]]) ti++;
      if (ti <= 1) {
        for (x = 0; x < 4 && pos[x]; x++)
          ;
        if (x == 4)
          return puts("-1"), 0;
        else
          pos[x] = i;
      }
    }
  if (!fg)
    putchar('0'), putchar(' '), print(d[Y]);
  else if (pos[0] && pos[1]) {
    if (sort(pos, pos + 4, cmp), d[pos[1]] == d[pos[2]])
      for (y = pos[0]; y; y = fa[y])
        if (y == pos[2])
          break;
        else if (y == pos[1]) {
          swap(pos[1], pos[2]);
          break;
        }
    xi = pos[0], yi = pos[1];
    int X1 = xi, Y1 = yi, now, f1 = 0, f2 = 0, tp;
    if (d[xi] > d[yi])
      for (; d[xi] > d[yi];
           c1[++t1] = a[xi], c3[++t3] = b[xi], vis[xi] = 1, xi = fa[xi])
        ;
    else
      for (; d[yi] > d[xi];
           c2[++t2] = a[yi], c4[++t4] = b[yi], vis[yi] = -1, yi = fa[yi])
        ;
    for (; xi != yi; c1[++t1] = a[xi], c3[++t3] = b[xi], c2[++t2] = a[yi],
                     c4[++t4] = b[yi], vis[xi] = 1, vis[yi] = -1, xi = fa[xi],
                     yi = fa[yi])
      ;
    if ((pos[2] && !vis[pos[2]]) || (pos[3] && !vis[pos[3]]))
      return puts("-1"), 0;
    if (a[xi] != b[xi]) c1[++t1] = a[xi], c3[++t3] = b[xi];
    for (; t2; c1[++t1] = c2[t2--])
      ;
    for (; t4; c3[++t3] = c4[t4--])
      ;
    for (i = 1; i <= t3; i++)
      if (c3[i] == c1[1]) {
        for (f1 = i - 1, f2 = t1 - i + 1, now = i, i = 1;
             i <= t1 && c1[i] == c3[now]; now == t1 ? now = 1 : now++, i++)
          ;
        if (i <= t1) return puts("-1"), 0;
        break;
      }
    if (xi == X1)
      prin(fa[xi], Y1), xi = fa[xi];
    else if (xi == Y1)
      prin(fa[xi], X1), xi = fa[xi];
    else
      prin(X1, Y1);
    for (; Y != X && !vis[Y]; fx++, Y = fa[Y])
      ;
    switch (vis[Y]) {
      case 0:
        for (tp = 0; !u[xi]; xi = fa[xi], tp++)
          ;
        print(fx + (long long)(f1 > f2 ? f2 : f1) * (t1 + 1) + (tp << 1));
        break;
      case 1:
        tmp = min((long long)f1 * (t1 + 1) - d[xi] + d[Y],
                  (long long)f2 * (t1 + 1) - d[Y] + d[xi]),
        print(fx + d[xi] + tmp);
        break;
      case -1:
        tmp = min((long long)f1 * (t1 + 1) + d[xi] - d[Y],
                  (long long)f2 * (t1 + 1) + d[Y] - d[xi]),
        print(fx + d[xi] + tmp);
        break;
    }
  } else
    puts("-1");
}
