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 long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f == 1 ? x : ~x + 1;
}
void Fail() {
  printf("-1\n");
  exit(0);
}
long long n;
long long a[200010], b[200010];
long long dep[200010];
vector<long long> vec[200010];
long long s, t;
vector<long long> to;
bool dfs(long long x, long long fa) {
  to.push_back(x);
  if (x == t) {
    return 1;
  }
  for (long long v : vec[x]) {
    if (v != fa && dfs(v, x)) return 1;
  }
  to.pop_back();
  return 0;
}
bool vis[200010];
long long deg[200010];
long long up = -1;
vector<long long> A, B;
vector<long long> cir;
void getcircle(long long x, long long fa) {
  dep[x] = dep[fa] + 1;
  if (a[x] != b[x]) {
    if (up == -1 || dep[up] >= dep[x]) up = fa;
    cir.push_back(x);
    vis[x] = 1;
  }
  for (long long v : vec[x]) {
    if (v != fa) getcircle(v, x);
  }
}
void getseq(long long x, long long fa) {
  if (x != up) {
    A.push_back(a[x]);
    B.push_back(b[x]);
  }
  for (long long v : vec[x]) {
    if (vis[v] && v != fa) getseq(v, x);
  }
}
long long calc(vector<long long> A, vector<long long> B) {
  long long siz = A.size();
  long long pos = 0;
  for (long long i = 0; i < siz; ++i) {
    if (B[i] == A[0]) {
      pos = i;
      break;
    }
  }
  if (pos == 0) return 0;
  for (long long i = 0; i < siz; ++i) {
    if (A[i] != B[(i + pos) % siz]) return 0;
  }
  return pos;
}
long long getdis(long long x, long long fa, long long dis, long long y) {
  if (x == y) return dis;
  for (long long v : vec[x]) {
    if (v == fa) continue;
    long long d = getdis(v, x, dis + 1, y);
    if (d) return d;
  }
  return 0;
}
long long dis(long long x, long long y) { return getdis(x, 0, 0, y); }
long long get(long long u, long long v) {
  long long c = calc(A, B);
  return dis(s, u) + (c - 1) * (A.size() + 1) + 1 + dis(v, t);
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; ++i) a[i] = read();
  for (long long i = 1; i <= n; ++i) b[i] = read();
  for (long long i = 1; i <= n; ++i)
    if (a[i] == 0) s = i;
  for (long long i = 1; i <= n; ++i)
    if (b[i] == 0) t = i;
  for (long long i = 1; i < n; ++i) {
    long long u = read(), v = read();
    vec[u].push_back(v), vec[v].push_back(u);
  }
  dfs(s, 0);
  for (long long i = 0; i < to.size() - 1; ++i) swap(a[to[i]], a[to[i + 1]]);
  bool ok = 1;
  for (long long i = 1; i <= n; ++i) ok = ok & (a[i] == b[i]);
  if (ok) {
    printf("0 %d\n", to.size() - 1);
    return 0;
  }
  getcircle(t, 0);
  vis[up] = 1;
  cir.push_back(up);
  vector<long long> u;
  for (long long x : cir) {
    for (long long v : vec[x]) {
      if (vis[v]) ++deg[x];
    }
    if (deg[x] == 1) {
      u.push_back(x);
    } else if (deg[x] != 2) {
      Fail();
    }
  }
  if (u.size() != 2) {
    Fail();
  }
  if (u[0] > u[1]) swap(u[0], u[1]);
  getseq(u[0], 0);
  if (!calc(A, B)) {
    Fail();
  }
  printf("%d %d ", u[0], u[1]);
  long long ans = get(u[0], u[1]);
  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  printf("%lld\n", min(ans, get(u[1], u[0])));
}