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 char nc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline void read(int &x) {
  char c = nc(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = nc())
    if (c == '-') b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = nc())
    ;
  x *= b;
}
const int N = 200005;
struct edge {
  int u, v, next;
} G[N << 1];
int head[N], inum;
inline void add(int u, int v, int p) {
  G[p].u = u;
  G[p].v = v;
  G[p].next = head[u];
  head[u] = p;
}
int n;
int s, t;
int S[N], T[N], bS[N], bT[N];
int c[N];
int fat[N], depth[N];
int cir[N];
inline void dfs(int u, int fa) {
  depth[u] = depth[fa] + 1;
  fat[u] = fa;
  for (int p = head[u]; p; p = G[p].next)
    if (G[p].v != fa && !cir[G[p].v]) dfs(G[p].v, u);
}
int vst[N], deg[N];
int tot;
int a[N], b[N];
int lst[N], pnt;
int Ansu, Ansv;
int main() {
  int iu, iv;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(S[i]);
    if (!S[i]) s = i;
  }
  for (int i = 1; i <= n; i++) {
    read(T[i]);
    if (!T[i]) t = i;
  }
  for (int i = 1; i < n; i++)
    read(iu), read(iv), add(iu, iv, ++inum), add(iv, iu, ++inum);
  dfs(t, 0);
  int p = s;
  for (int i = 1; i <= n; i++) c[i] = S[i];
  while (p != t) swap(c[p], c[fat[p]]), p = fat[p];
  int flag = 0;
  for (int i = 1; i <= n; i++) flag |= c[i] != T[i];
  if (!flag) return printf("0 %d\n", depth[s] - 1), 0;
  for (int i = 1; i <= n; i++) vst[i] = c[i] != T[i];
  while (1) {
    int k = 0;
    for (int i = 1; i <= n; i++)
      if (vst[i]) {
        k = i;
        break;
      }
    if (!k || tot > 2) break;
    int t = k;
    while (1) {
      vst[t] = 0;
      int tmp = t;
      for (int p = head[t]; p; p = G[p].next)
        if (vst[G[p].v]) {
          t = G[p].v;
          break;
        }
      if (tmp == t) break;
    }
    a[++tot] = t;
    t = k;
    while (1) {
      vst[t] = 0;
      int tmp = t;
      for (int p = head[t]; p; p = G[p].next)
        if (vst[G[p].v]) {
          t = G[p].v;
          break;
        }
      if (tmp == t) break;
    }
    b[tot] = t;
  }
  if (tot > 2) return printf("-1\n"), 0;
  if (tot == 1) {
    int &a = ::a[1], &b = ::b[1];
    if (depth[a] > depth[b]) swap(a, b);
    int t = b;
    while (t && t != a) t = fat[t];
    if (!t) return printf("-1\n"), 0;
    add(fat[a], b, ++inum);
    add(b, fat[a], ++inum);
    if (fat[a] < b)
      Ansu = fat[a], Ansv = b;
    else
      Ansu = b, Ansv = fat[a];
    t = b;
    while (t != fat[a]) cir[t] = 1, t = fat[t];
    cir[fat[a]] = 1;
  } else {
    for (int i = 1; i <= 2; i++) {
      int &a = ::a[i], &b = ::b[i];
      if (depth[a] > depth[b]) swap(a, b);
      int t = b;
      while (t && t != a) t = fat[t];
      if (!t) return printf("-1\n"), 0;
    }
    if (fat[a[1]] != fat[a[2]]) return printf("-1\n"), 0;
    add(b[1], b[2], ++inum);
    add(b[2], b[1], ++inum);
    if (b[1] < b[2])
      Ansu = b[1], Ansv = b[2];
    else
      Ansu = b[2], Ansv = b[1];
    for (int i = 1; i <= 2; i++) {
      int t = b[i];
      while (t != fat[a[i]]) cir[t] = 1, t = fat[t];
      cir[fat[a[i]]] = 1;
    }
  }
  for (int i = 1; i <= n; i++)
    if (cir[i]) dfs(i, 0);
  long long ans = 0;
  while (!cir[s]) swap(S[s], S[fat[s]]), s = fat[s], ans++;
  while (!cir[t]) swap(T[t], T[fat[t]]), t = fat[t], ans++;
  int _t = s;
  pnt = -1;
  while (1) {
    cir[_t] = 0;
    lst[++pnt] = _t;
    int tmp = _t;
    for (int p = head[_t]; p; p = G[p].next)
      if (cir[G[p].v]) {
        _t = G[p].v;
        break;
      }
    if (tmp == _t) break;
  }
  long long ret = 1LL << 60;
  int step = 0;
  int pt = 0;
  pnt++;
  for (int i = 0; i < pnt; i++)
    if (lst[i] == t) pt = i;
  memcpy(bS, S, sizeof(S));
  memcpy(bT, T, sizeof(T));
  for (int i = pt; i; i--) swap(T[lst[i]], T[lst[i - 1]]), step++;
  int pp = 1, qq = 0, gap;
  for (int i = 1; i < pnt; i++)
    if (T[lst[i]] == S[lst[1]]) qq = i;
  gap = qq;
  for (int i = 1; i < pnt; i++) {
    if (S[lst[pp]] != T[lst[qq]]) return printf("-1\n", 0), 0;
    pp = pp == pnt - 1 ? 1 : pp + 1;
    qq = qq == pnt - 1 ? 1 : qq + 1;
  }
  ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
  memcpy(S, bS, sizeof(S));
  memcpy(T, bT, sizeof(T));
  step = 0;
  if (pt)
    for (int i = pt; i < pnt; i++)
      swap(T[lst[i]], T[lst[(i + 1) % pnt]]), step++;
  pp = 1, qq = 0, gap;
  for (int i = 1; i < pnt; i++)
    if (T[lst[i]] == S[lst[1]]) qq = i;
  gap = qq;
  ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
  printf("%d %d %I64d\n", Ansu, Ansv, ans + ret);
  return 0;
}