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;
const int WZP = 6666666;
char LZH[WZP], *SSS = LZH, *TTT = LZH;
inline char gc() {
  if (SSS == TTT) {
    TTT = (SSS = LZH) + fread(LZH, 1, WZP, stdin);
    if (SSS == TTT) return EOF;
  }
  return *SSS++;
}
inline int read01() {
  char s = gc();
  for (; s < '0' || s > '1'; s = gc())
    ;
  return s - '0';
}
inline int read() {
  char s = gc();
  int x = 0;
  for (; s < '0' || s > '9'; s = gc())
    ;
  for (; s >= '0' && s <= '9'; s = gc()) x = x * 10 - 48 + s;
  return x;
}
const int N = 200005;
struct edge {
  int to, next;
} e[N * 2];
int head[N], tot;
int dep[N], dfn[N], ed[N];
int fa[N][19], a[N], b[N];
int n, spe1, spe2, A[N], B[N];
void add(int x, int y) {
  e[++tot] = (edge){y, head[x]};
  head[x] = tot;
}
void dfs(int x) {
  dep[x] = dep[fa[x][0]] + 1;
  dfn[x] = ++*dfn;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].to != fa[x][0]) {
      fa[e[i].to][0] = x;
      dfs(e[i].to);
    }
  ed[x] = *dfn;
}
bool insub(int x, int y) { return dfn[x] <= dfn[y] && ed[y] <= ed[x]; }
int LCA(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int tmp = dep[x] - dep[y];
  for (int i = (int)(0); i <= (int)(18); i++)
    if (tmp & (1 << i)) x = fa[x][i];
  for (int i = (int)(18); i >= (int)(0); i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return x == y ? x : fa[x][0];
}
pair<int, int> merge(const pair<int, int> &a, int x) {
  if (a.first == -1) return pair<int, int>(-1, -1);
  if (a.first == 0) return pair<int, int>(x, x);
  static int c[5], L;
  c[0] = a.first;
  c[1] = a.second;
  c[2] = x;
  L = LCA(c[0], c[1]);
  if (insub(L, c[2]) && (insub(c[2], c[0]) || insub(c[2], c[1])))
    return pair<int, int>(c[0], c[1]);
  L = LCA(c[0], c[2]);
  if (insub(L, c[1]) && (insub(c[1], c[0]) || insub(c[1], c[2])))
    return pair<int, int>(c[0], c[2]);
  L = LCA(c[1], c[2]);
  if (insub(L, c[0]) && (insub(c[0], c[1]) || insub(c[0], c[2])))
    return pair<int, int>(c[1], c[2]);
  return pair<int, int>(-1, -1);
}
long long solve(int x, int y, int L, int *a) {
  int l = 1, r = dep[x] + dep[y] - 2 * dep[L], len = r, at = 1;
  for (; x != L; x = fa[x][0], l++) A[l] = a[x], B[l] = b[x];
  for (; y != L; y = fa[y][0], r--) A[r] = a[y], B[r] = b[y];
  for (int i = (int)(1); i <= (int)(len); i++)
    if (A[i])
      for (; A[1] != B[at]; at++)
        ;
  for (int i = 1; i <= len; i++)
    if (A[i] != B[(i + at - 2) % len + 1]) return 1ll << 60;
  return 1ll * min(at - 1, len + 1 - at) * (len + 1);
}
int cnt, aa[N];
bool dfs(int x, int fa, int spe) {
  if (x == spe) return 1;
  for (int i = head[x]; i; i = e[i].next)
    if (e[i].to != fa)
      if (dfs(e[i].to, x, spe)) {
        cnt++;
        swap(aa[x], aa[e[i].to]);
        return 1;
      }
  return 0;
}
void flip(int p1, int p2) { dfs(p2, 0, p1); }
bool checkSAME(int *a, int *b) {
  for (int i = (int)(1); i <= (int)(n); i++)
    if (a[i] != b[i]) return 0;
  return 1;
}
int tp;
int main() {
  n = read();
  for (int i = (int)(1); i <= (int)(n); i++)
    a[i] = read(), spe1 = (a[i] == 0 ? i : spe1);
  for (int i = (int)(1); i <= (int)(n); i++)
    b[i] = read(), spe2 = (b[i] == 0 ? i : spe2);
  for (int i = (int)(1); i <= (int)(n - 1); i++) {
    int x = read(), y = read();
    add(x, y);
    add(y, x);
  }
  memcpy(aa, a, sizeof(aa));
  dfs(spe2);
  flip(spe1, spe2);
  if (checkSAME(aa, b)) return printf("0 %d\n", cnt), 0;
  for (int i = (int)(1); i <= (int)(18); i++)
    for (int j = (int)(1); j <= (int)(n); j++)
      fa[j][i] = fa[fa[j][i - 1]][i - 1];
  pair<int, int> tmp(0, 0);
  for (int i = (int)(1); i <= (int)(n); i++)
    if (aa[i] != b[i]) tmp = merge(tmp, i);
  if (tmp.first == -1) return puts("-1"), 0;
  if (dep[tmp.first] > dep[tmp.second]) swap(tmp.first, tmp.second);
  if (insub(tmp.first, tmp.second)) tmp.first = fa[tmp.first][0];
  long long ans = 1ll << 60;
  int L1 = LCA(tmp.first, tmp.second);
  cnt = 0;
  memcpy(aa, a, sizeof(aa));
  flip(spe1, tmp.first);
  swap(aa[tmp.first], aa[tmp.second]);
  cnt++;
  flip(tmp.second, spe2);
  ans = min(ans, cnt + solve(tmp.first, tmp.second, L1, aa));
  cnt = 0;
  memcpy(aa, a, sizeof(aa));
  flip(spe1, tmp.second);
  swap(aa[tmp.second], aa[tmp.first]);
  cnt++;
  flip(tmp.first, spe2);
  ans = min(ans, cnt + solve(tmp.first, tmp.second, L1, aa));
  if (ans >= 1ll << 50) return puts("-1"), 0;
  if (tmp.first > tmp.second) swap(tmp.first, tmp.second);
  printf("%d %d ", tmp.first, tmp.second);
  printf("%lld\n", ans);
}