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;
inline int read() {
  int x = 0, c = getchar(), f = 0;
  for (; c > '9' || c < '0'; f = c == '-', c = getchar())
    ;
  for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + c - '0', c = getchar())
    ;
  return f ? -x : x;
}
inline void write(int x) {
  if (x < 0) putchar('-'), x = -x;
  if (x >= 10) write(x / 10);
  putchar(x % 10 + 48);
}
const int N = 200000 + 10;
struct edge {
  int to, nxt;
} e[N * 2];
int n, ecnt, head[N];
void addedge(int u, int v) {
  e[++ecnt] = (edge){v, head[u]};
  head[u] = ecnt;
  e[++ecnt] = (edge){u, head[v]};
  head[v] = ecnt;
}
void init(int *a) {
  for (int i = (int)1, _y = n; i <= _y; i++) {
    a[i] = read();
    if (!a[i]) a[0] = i;
  }
}
int dep[N], fa[N];
void dfs(int u) {
  for (int i = head[u], v; i; i = e[i].nxt)
    if ((v = e[i].to) != fa[u]) {
      dep[v] = dep[u] + 1;
      fa[v] = u;
      dfs(v);
    }
}
int a[N], b[N], lca;
bool vis[N];
pair<int, int> lx[N], ly[N];
void find(int &x) {
  for (int i = (int)1, _y = n; i <= _y; i++)
    if (a[i] != b[i] && !vis[i] && (!x || dep[i] > dep[x])) x = i;
  if (!x) {
    x = lca;
    return;
  }
  int i = x;
  for (vis[i] = 1; a[fa[i]] != b[fa[i]]; vis[i] = 1) i = fa[i];
  if (lca && lca != fa[i]) puts("-1"), exit(0);
  lca = fa[i];
}
int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (; dep[x] != dep[y]; x = fa[x])
    ;
  for (; x != y; x = fa[x], y = fa[y])
    ;
  return x;
}
int main() {
  n = read();
  init(a);
  init(b);
  for (int i = (int)1, _y = n - 1; i <= _y; i++) addedge(read(), read());
  dfs(*b);
  for (int i = *a; i != *b; i = fa[i]) swap(a[i], a[fa[i]]);
  bool flag = 1;
  for (int i = (int)1, _y = n; i <= _y; i++)
    if (a[i] != b[i]) {
      flag = 0;
      break;
    }
  if (flag) return printf("0 %d\n", dep[*a]), 0;
  int x = 0, y = 0;
  find(x);
  find(y);
  for (int i = (int)1, _y = n; i <= _y; i++)
    if (a[i] != b[i] && !vis[i]) puts("-1"), exit(0);
  if (x > y) swap(x, y);
  int lenx = 0, leny = 0;
  for (int i = x; i != lca; i = fa[i]) lx[++lenx] = make_pair(a[i], b[i]);
  for (int i = y; i != lca; i = fa[i]) ly[++leny] = make_pair(a[i], b[i]);
  for (; leny;) lx[++lenx] = ly[leny--];
  int loop = lenx + 1, pos = 0;
  for (int i = (int)1, _y = lenx; i <= _y; i++)
    if (lx[i].second == lx[1].first) {
      pos = i;
      break;
    }
  if (!pos) puts("-1"), exit(0);
  for (int i = (int)1, _y = lenx; i <= _y; i++)
    if (lx[i].first != lx[(pos + i - 2) % lenx + 1].second) puts("-1"), exit(0);
  int qs = min(pos - 1, lenx - pos + 1);
  printf("%d %d ", x, y);
  int LX = getlca(*a, x), RX = getlca(*a, y);
  if (dep[LX] < dep[lca] || (LX == lca && RX == lca))
    printf("%lld\n",
           dep[*a] + dep[lca] - 2 * dep[LX] + (long long)qs * loop + dep[lca]);
  else {
    long long ans1, ans2;
    if (LX == lca)
      ans1 = dep[*a] + (long long)(pos - 1) * loop,
      ans2 =
          dep[*a] + (long long)(loop - pos) * loop - (dep[RX] - dep[lca]) * 2;
    else
      ans1 = dep[*a] + (long long)(loop - pos) * loop,
      ans2 = dep[*a] + (long long)(pos - 1) * loop - (dep[LX] - dep[lca]) * 2;
    printf("%lld\n", min(ans1, ans2));
  }
}