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>
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");
}