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 n, A[200001], B[200001], head[200001], nxt[400001], b[400001], k, S, T,
    pre[200001], next[200001];
int num[200001], top, tem1[200001], tem2[200001];
void push(int s, int t) {
  nxt[++k] = head[s];
  head[s] = k;
  b[k] = t;
}
int dfs(int x, int f, int dis = 0) {
  if (x == T) return dis;
  for (int i = head[x]; i; i = nxt[i])
    if (b[i] != f) {
      int u = dfs(b[i], x, dis + 1);
      if (~u) return u;
    }
  return -1;
}
bool getpath(int x, int f, int end) {
  num[++top] = x;
  if (x == end) return 1;
  for (int i = head[x]; i; i = nxt[i])
    if (b[i] != f && getpath(b[i], x, end)) return 1;
  --top;
  return 0;
}
bool vis[200001];
int d[200001];
int check(int len) {
  int p = -1;
  for (int i = 0; i < len; i++)
    if (tem2[i] == tem1[0]) p = i;
  for (int i = 0; i < len; i++)
    if (tem2[(i + p) % len] != tem1[i]) {
      puts("-1");
      exit(0);
    }
  return std::min(p, len - p);
}
int getdis(int x, int f, int dis = 0) {
  if (!vis[x]) return dis;
  for (int i = head[x]; i; i = nxt[i])
    if (b[i] != f) {
      int u = getdis(b[i], x, dis + 1);
      if (~u) return u;
    }
  return -1;
}
void addone(int x, int f) {
  if (!vis[x]) {
    if (d[x] > 1) {
      puts("-1");
      exit(0);
    }
    vis[f] = 0;
    A[f] = B[f] = 0;
    return;
  }
  for (int i = head[x]; i; i = nxt[i])
    if (b[i] != f) {
      addone(b[i], x);
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", A + i);
  for (int i = 1; i <= n; i++) scanf("%d", B + i);
  for (int i = 1; i <= n; i++) pre[i] = next[i] = -1;
  for (int i = 1, u, v; i < n; i++)
    scanf("%d%d", &u, &v), push(u, v), push(v, u);
  bool cando = 1;
  for (int i = 1; i <= n; i++)
    if (A[i] != B[i] || !A[i]) {
      if (!A[i]) S = i;
      if (!B[i]) T = i;
      for (int j = head[i]; j; j = nxt[j])
        if (A[b[j]] == B[i] && B[i]) pre[i] = b[j], next[b[j]] = i;
      if (!~pre[i] && i != T) cando = 0;
    } else
      vis[i] = 1;
  int tem = S;
  long long ans = 0;
  int pp1, pp2;
  while (~pre[tem]) {
    vis[tem] = 1;
    ++ans;
    tem = pre[tem];
  }
  pp1 = tem;
  A[tem] = 0;
  tem = T;
  while (~next[tem]) {
    vis[tem] = 1;
    ++ans;
    tem = next[tem];
  }
  B[tem] = 0;
  pp2 = tem;
  if (pp1 != pp2 && !vis[pp1] && !vis[pp2]) {
    int cnt = 0;
    for (int i = 1; i <= n; i++)
      if (!vis[i]) ++cnt;
    if (cnt == 2) {
      if (pp1 > pp2) std::swap(pp1, pp2);
      printf("%d %d %d\n", pp1, pp2, ans + 1);
      return 0;
    }
  }
  bool bb = 0;
  if ((tem == S && vis[S]) || (S == T)) {
    vis[S] = 1;
    bool over = 1;
    for (int i = 1; i <= n; i++)
      if (!vis[i]) over = 0;
    if (over) {
      printf("0 %d\n", dfs(S, 0));
      return 0;
    } else {
      bb = 1;
      int cnt = 0;
      for (int i = 1; i <= n; i++)
        if (!vis[i])
          for (int j = head[i]; j; j = nxt[j])
            if (!vis[b[j]]) ++d[b[j]];
      for (int i = 1; i <= n; i++)
        if (!vis[i]) {
          if (d[i] > 2) {
            puts("-1");
            return 0;
          }
          if (d[i] == 1) ++cnt;
        }
      if (cnt > 4) {
        puts("-1");
        return 0;
      }
      if (cnt == 4) {
        int node = -1;
        for (int i = 1; i <= n; i++)
          if (vis[i]) {
            int cnt = 0;
            for (int j = head[i]; j; j = nxt[j])
              if (d[b[j]] == 1) ++cnt;
            if (cnt == 2) node = i;
          }
        if (!~node) {
          puts("-1");
          return 0;
        }
        vis[node] = 0;
        A[node] = B[node] = 0;
      } else
        addone(S, 0);
      ans = getdis(S, 0) + getdis(T, 0);
      memset(d, 0, sizeof d);
    }
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i])
      for (int j = head[i]; j; j = nxt[j])
        if (!vis[b[j]]) ++d[b[j]];
  int p1 = -1, p2 = -1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      if (d[i] > 2 || !d[i]) {
        puts("-1");
        return 0;
      }
      if (d[i] == 1) {
        if (!~p1)
          p1 = i;
        else if (!~p2)
          p2 = i;
        else {
          puts("-1");
          return 0;
        }
      }
    }
  if (!~p2) {
    puts("-1");
    return 0;
  }
  getpath(p1, 0, p2);
  int added = 1;
  int pos1 = -1, pos2 = -1;
  for (int i = 1; i <= top; i++)
    if (A[num[i]])
      tem1[i - added] = A[num[i]];
    else
      added++, pos1 = i - 1;
  added = 1;
  for (int i = 1; i <= top; i++)
    if (B[num[i]])
      tem2[i - added] = B[num[i]];
    else
      added++, pos2 = i - 1;
  long long fin = 1e18;
  fin = std::min(fin, 1ll * top * check(top - 1) + abs(pos2 - pos1));
  if (pos1 < pos2) {
    int orig = tem1[top - 2];
    for (int i = top - 2; i; i--) tem1[i] = tem1[i - 1];
    tem1[0] = orig;
  } else if (pos1 > pos2) {
    int orig = tem1[0];
    for (int i = 0; i < top - 2; i++) tem1[i] = tem1[i + 1];
    tem1[top - 2] = orig;
  }
  fin = std::min(fin, 1ll * top * check(top - 1) + top - abs(pos2 - pos1));
  printf("%d %d %d\n", p1, p2, ans + fin);
}