#include <bits/stdc++.h>
int fr[200010], ne[400010], v[400010], bs = 0;
void addb(int a, int b) {
  v[bs] = b;
  ne[bs] = fr[a];
  fr[a] = bs++;
}
int st[200010], en[200010], fa[200010], sw[200010], ew[200010], sd[200010];
void dfs(int u, int f) {
  fa[u] = f;
  if (f)
    sd[u] = sd[f] + 1;
  else
    sd[u] = 0;
  for (int i = fr[u]; i != -1; i = ne[i]) {
    if (v[i] != f) dfs(v[i], u);
  }
}
int sz[200010];
int check0(int n) {
  dfs(ew[0], 0);
  for (int i = 1; i <= n; i++) sz[i] = st[i];
  int u = sw[0], s = 0;
  while (u != ew[0]) {
    int f = fa[u];
    int t = sz[u];
    sz[u] = sz[f];
    sz[f] = t;
    u = f;
    s += 1;
  }
  for (int i = 1; i <= n; i++) {
    if (sz[i] != en[i]) return -1;
  }
  return s;
}
struct SJd {
  int u, z;
  SJd() {}
  SJd(int U, int Z) {
    u = U;
    z = Z;
  }
};
SJd px[200010];
int cmp(const void* a, const void* b) { return ((SJd*)a)->z - ((SJd*)b)->z; }
int wz[200010], sa[200010], sb[200010];
long long check(int n, int m, int ss[200010], int ux, int uy) {
  int wx, wy;
  for (int i = 0; i < n; i++) wz[i] = -1;
  for (int i = 0, x = 0; i < m; i++) {
    if (ss[i] != ux)
      wz[st[ss[i]]] = x;
    else
      wx = i;
    if (ss[i] == uy) wy = i;
    if (ss[i] != ux) x += 1;
  }
  int cc, cz = -1, bb;
  if (wy > wx)
    cc = wy - wx, bb = -1;
  else
    cc = wx - wy, bb = 1;
  for (int i = 0, x = 0; i < m; i++) {
    if (ss[i] == ux) continue;
    int j = wz[en[ss[i]]];
    if (j == -1) return -1;
    int z = (j - x + m - 1) % (m - 1);
    if (cz != -1 && z != cz) return -1;
    cz = z;
    x += 1;
  }
  long long rt1 = 1ll * cz * m - cc * bb;
  long long rt2 = 1ll * (m - 1 - cz) * m + cc * bb;
  if (rt2 < rt1) rt1 = rt2;
  return rt1 + sd[ux];
}
int caldis(int x, int y) {
  int rt = 0;
  while (sd[x] > sd[y]) x = fa[x], rt += 1;
  while (sd[x] < sd[y]) y = fa[y], rt += 1;
  while (x != y) {
    x = fa[x];
    y = fa[y];
    rt += 2;
  }
  return rt;
}
int xl[200010];
bool bk[200010];
int main() {
  int n, m = 0;
  long long jg;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    fr[i] = -1;
    scanf("%d", &st[i]);
    sw[st[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &en[i]);
    ew[en[i]] = i;
  }
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    addb(a, b);
    addb(b, a);
  }
  if ((jg = check0(n)) >= 0) {
    printf("0 %lld", jg);
    return 0;
  }
  dfs(sw[0], 0);
  int u = ew[0];
  while (u != sw[0]) {
    int f = fa[u], t = en[u];
    en[u] = en[f];
    en[f] = t;
    u = f;
  }
  for (int i = 1; i <= n; i++) {
    if (st[i] != en[i]) px[m++] = SJd(i, sd[i]);
  }
  qsort(px, m, sizeof(SJd), cmp);
  int sm = 0;
  for (int i = 0; i < m; i++) {
    if (px[i].z == px[0].z) sm += 1;
  }
  if (sm > 2) {
    printf("-1");
    return 0;
  }
  if (sm == 1) {
    int tp = px[0].u, la = tp, k = 0;
    for (int i = 0; i < n; i++) wz[i] = -1;
    for (int i = 0; i < m; i++) wz[st[px[i].u]] = i;
    for (int i = 1; i < m; i++) {
      int t = px[i].u;
      if (fa[t] != la) {
        printf("-1");
        return 0;
      }
      la = t;
    }
    xl[k++] = fa[tp];
    for (int i = 0; i < m; i++) xl[k++] = px[i].u;
    for (int i = 0; i < k; i++) bk[xl[i]] = true;
    int ss = 0, u = ew[0];
    while (u && !bk[u]) {
      u = fa[u];
      ss += 1;
    }
    if (u == 0) u = fa[tp], ss = caldis(ew[0], fa[tp]);
    jg = check(n, k, xl, fa[tp], u);
    if (jg == -1)
      printf("-1");
    else {
      if (fa[tp] < la)
        printf("%d %d %lld", fa[tp], la, jg + ss);
      else
        printf("%d %d %lld", la, fa[tp], jg + ss);
    }
  } else {
    int tx = px[0].u, ty = px[1].u, lx = tx, ly = ty;
    if (fa[tx] == 0 || fa[ty] == 0 || fa[tx] != fa[ty]) {
      printf("-1");
      return 0;
    }
    int a = 0, b = 0;
    sa[a++] = lx;
    sb[b++] = ly;
    for (int i = 2; i < m; i++) {
      int t = px[i].u;
      if (fa[t] == lx) {
        sa[a++] = t;
        lx = t;
      } else if (fa[t] == ly) {
        sb[b++] = t;
        ly = t;
      } else {
        printf("-1");
        return 0;
      }
    }
    int k = 0;
    for (int i = b - 1; i >= 0; i--) xl[k++] = sb[i];
    xl[k++] = fa[tx];
    for (int i = 0; i < a; i++) xl[k++] = sa[i];
    for (int i = 0; i < k; i++) bk[xl[i]] = true;
    int ss = 0, u = ew[0];
    while (u && !bk[u]) {
      u = fa[u];
      ss += 1;
    }
    if (u == 0) u = fa[tx], ss = caldis(ew[0], fa[tx]);
    jg = check(n, k, xl, fa[tx], u);
    if (jg == -1)
      printf("-1");
    else {
      if (lx < ly)
        printf("%d %d %lld", lx, ly, jg + ss);
      else
        printf("%d %d %lld", ly, lx, jg + ss);
    }
  }
  return 0;
}
