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 INF = (int)1e9 + 100;
const long long lINF = 1e18;
const double EPS = 1e-12;
int myrand() { return rand(); }
long long rdtsc() {
  long long ans;
  asm("rdtsc" : "=A"(ans));
  return ans;
}
int rnd(int x) { return myrand() % x; }
const int maxn = (int)2e5 + 100;
int n;
int a[maxn], b[maxn], c[maxn];
int used[maxn], maxu, depth[maxn], par[maxn];
vector<int> tree[maxn];
bool read() {
  if (scanf("%d", &n) < 1) {
    return false;
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", a + i);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", b + i);
  }
  for (int i = 0; i < n; i++) {
    tree[i].clear();
  }
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--, v--;
    tree[u].push_back(v);
    tree[v].push_back(u);
  }
  return true;
}
void dfs(int v, int curpar = -1, int dep = 0) {
  depth[v] = dep;
  par[v] = curpar;
  for (int i = 0; i < ((int)((tree[v]).size())); i++) {
    int to = tree[v][i];
    if (to == curpar) {
      continue;
    }
    dfs(to, v, dep + 1);
  }
}
int move0(int v, int* perm, int curpar = -1) {
  if (perm[v] == 0) {
    return 0;
  }
  for (int i = 0; i < ((int)((tree[v]).size())); i++) {
    int to = tree[v][i];
    if (to == curpar || used[to] == maxu) {
      continue;
    }
    int res = move0(to, perm, v);
    if (res >= 0) {
      swap(perm[v], perm[to]);
      return res + 1;
    }
  }
  return -1;
}
void solve() {
  int root = (int)(find(a, a + n, 0) - a);
  dfs(root);
  memcpy(c, b, n * sizeof(int));
  maxu++;
  int restree = move0(root, c);
  assert(a[root] == 0 && c[root] == 0);
  vector<int> ends;
  int anc = -1;
  for (int i = 0; i < n; i++) {
    if (a[i] != c[i]) {
      if (anc == -1 || depth[anc] > depth[par[i]]) {
        anc = par[i];
      }
      bool leaf = true;
      for (int j = 0; j < ((int)((tree[i]).size())); j++) {
        int to = tree[i][j];
        if (to == par[i]) {
          continue;
        }
        if (a[to] != c[to]) {
          leaf = false;
          break;
        }
      }
      if (leaf) {
        ends.push_back(i);
      }
    }
  }
  if (((int)((ends).size())) > 2) {
    printf("-1\n");
    return;
  }
  if (((int)((ends).size())) == 0) {
    printf("0 %d\n", restree);
    return;
  }
  assert(anc != -1);
  if (((int)((ends).size())) == 1) {
    ends.push_back(anc);
  }
  vector<int> cycle, cycle1, cycle2;
  maxu++;
  for (int it = 0; it < 2; it++) {
    for (int i = ends[it]; i != anc; i = par[i]) {
      if (i == -1 || used[i] == maxu) {
        printf("-1\n");
        return;
      }
      if (!it) {
        cycle1.push_back(i);
      } else {
        cycle2.push_back(i);
      }
      used[i] = maxu;
    }
  }
  cycle.push_back(anc);
  used[anc] = maxu;
  for (int i = ((int)((cycle2).size())) - 1; i >= 0; i--) {
    cycle.push_back(cycle2[i]);
  }
  for (int i = 0; i < ((int)((cycle1).size())); i++) {
    cycle.push_back(cycle1[i]);
  }
  int m = ((int)((cycle).size()));
  int res = 0;
  for (int i = 0; i < m; i++) {
    res += max(0, move0(cycle[i], a));
    res += max(0, move0(cycle[i], b));
  }
  vector<int> incycle(n, -1), inb(n);
  for (int i = 0; i < n; i++) {
    inb[b[i]] = i;
  }
  for (int i = 0; i < m; i++) {
    incycle[cycle[i]] = i;
  }
  vector<int> nperm(m);
  for (int i = 0; i < m; i++) {
    int val = incycle[inb[a[cycle[i]]]];
    if (val == -1) {
      printf("-1\n");
      return;
    }
    nperm[i] = val;
  }
  vector<int> rev(m);
  for (int i = 0; i < m; i++) {
    rev[nperm[i]] = i;
  }
  assert(m > 1);
  long long rescycle = lINF;
  vector<int> curperm = rev;
  for (int it = 0; it < 2; it++) {
    vector<int> now(m);
    int curres = 0;
    for (int i = 0; i < m; i++) {
      now[i] = i;
    }
    for (int i = 0; curperm[i] != 0; i++) {
      curres++;
      swap(now[i], now[i + 1]);
    }
    int diff = -1;
    for (int j = 0; j < m; j++) {
      if (now[j] != 0) {
        int curdiff = ((now[j] - curperm[j]) % (m - 1) + (m - 1)) % (m - 1);
        if (diff != -1 && curdiff != diff) {
          printf("-1\n");
          return;
        }
        diff = curdiff;
      }
    }
    rescycle = min(rescycle, curres + (long long)(min(diff, m - 1 - diff)) * m);
    for (int i = 0; i < m; i++) {
      int j = !i ? 0 : (m - i);
      curperm[i] = rev[j] ? (m - rev[j]) : 0;
    }
  }
  if (ends[0] > ends[1]) {
    swap(ends[0], ends[1]);
  }
  printf(
      "%d %d "
      "%lld"
      "\n",
      ends[0] + 1, ends[1] + 1, rescycle + res);
}
int main() {
  srand(rdtsc());
  while (true) {
    if (!read()) {
      break;
    }
    solve();
    break;
    fprintf(stderr, "Time: %.18f\n", (double)clock() / CLOCKS_PER_SEC);
    fflush(stderr);
  }
  return 0;
}