Problem: A remote island chain contains n islands, with some bidirectional bridges between them. The current bridge network forms a tree. In other words, a total of n - 1 bridges connect pairs of islands in a way that it's possible to reach any island from any other island using the bridge network. The center of each island contains an identical pedestal, and all but one of the islands has a fragile, uniquely colored statue currently held on the pedestal. The remaining island holds only an empty pedestal.

The islanders want to rearrange the statues in a new order. To do this, they repeat the following process: first, they choose an island directly adjacent to the island containing an empty pedestal. Then, they painstakingly carry the statue on this island across the adjoining bridge and place it on the empty pedestal.

It is often impossible to rearrange statues in the desired order using only the operation described above. The islanders would like to build one additional bridge in order to make this achievable in the fewest number of movements possible. Find the bridge to construct and the minimum number of statue movements necessary to arrange the statues in the desired position.

Input

The first line contains a single integer n (2 ≤ n ≤ 200 000) — the total number of islands.

The second line contains n space-separated integers ai (0 ≤ ai ≤ n - 1) — the statue currently located on the i-th island. If ai = 0, then the island has no statue. It is guaranteed that the ai are distinct.

The third line contains n space-separated integers bi (0 ≤ bi ≤ n - 1) — the desired statues of the i-th island. Once again, bi = 0 indicates the island desires no statue. It is guaranteed that the bi are distinct.

The next n - 1 lines each contain two distinct space-separated integers ui and vi (1 ≤ ui, vi ≤ n) — the endpoints of the i-th bridge. Bridges form a tree, and it is guaranteed that no bridge is listed twice in the input.

Output

Print a single line of integers:

If the rearrangement can be done in the existing network, output 0 t, where t is the number of moves necessary to perform the rearrangement.

Otherwise, print u, v, and t (1 ≤ u < v ≤ n) — the two endpoints of the new bridge, and the minimum number of statue movements needed to perform the rearrangement.

If the rearrangement cannot be done no matter how the new bridge is built, print a single line containing  - 1.

Examples

Input

3
1 0 2
2 0 1
1 2
2 3


Output

1 3 3


Input

2
1 0
0 1
1 2


Output

0 1


Input

4
0 1 2 3
0 2 3 1
1 2
1 3
1 4


Output

-1

Note

In the first sample, the islanders can build a bridge connecting islands 1 and 3 and then make the following sequence of moves: first move statue 1 from island 1 to island 2, then move statue 2 from island 3 to island 1, and finally move statue 1 from island 2 to island 3 for a total of 3 moves.

In the second sample, the islanders can simply move statue 1 from island 1 to island 2. No new bridges need to be built and only 1 move needs to be made.

In the third sample, no added bridge and subsequent movements result in the desired position.

Solution (in CPP):

Solution:
#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;
}