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 long long maxn = 4e5 + 10;
long long n, Begin[maxn], Next[maxn], To[maxn], e, ans, x1, x2;
long long a[maxn], b[maxn], x, y, dis[maxn], anc[maxn], t, len;
bool use[maxn];
vector<vector<long long> > f;
vector<long long> A, B;
inline void add(long long u, long long v) {
  To[++e] = v;
  Next[e] = Begin[u];
  Begin[u] = e;
}
inline void DFS(long long u, long long fa) {
  for (long long i = Begin[u]; i; i = Next[i]) {
    long long v = To[i];
    if (v == fa) continue;
    dis[v] = dis[u] + 1;
    anc[v] = u;
    DFS(v, u);
  }
}
inline vector<long long> Get(long long x) {
  vector<long long> p;
  while (!use[x] && a[x] != b[x]) {
    p.push_back(x);
    x = anc[x];
  }
  return p;
}
inline long long LCA(long long x, long long y) {
  if (dis[x] < dis[y]) swap(x, y);
  while (dis[x] > dis[y]) x = anc[x];
  if (x == y) return x;
  while (anc[x] != anc[y]) {
    x = anc[x];
    y = anc[y];
  }
  return anc[x];
}
inline long long Dis(long long x, long long y) {
  return dis[x] + dis[y] - 2 * dis[LCA(x, y)];
}
inline void DFS2(long long u, long long fa) {
  for (long long i = Begin[u]; i; i = Next[i]) {
    long long v = To[i];
    if (v == fa) continue;
    DFS2(v, u);
  }
  vector<long long> p = Get(u);
  if (p.size()) {
    f.push_back(p);
    for (auto it : p) use[it] = true;
    if (f.size() > 2) {
      puts("-1");
      exit(0);
    }
  }
}
signed main() {
  scanf("%lld", &n);
  for (long long i = (1), _end_ = (n); i <= _end_; ++i) {
    scanf("%lld", &a[i]);
    if (!a[i]) x = i;
  }
  for (long long i = (1), _end_ = (n); i <= _end_; ++i) {
    scanf("%lld", &b[i]);
    if (!b[i]) y = i;
  }
  for (long long i = (1), _end_ = (n - 1); i <= _end_; ++i) {
    long long u, v, w;
    scanf("%lld%lld", &u, &v);
    add(u, v);
    add(v, u);
  }
  dis[y] = 1;
  DFS(y, 0);
  long long u = x;
  while (u != y) {
    a[u] = a[anc[u]];
    u = anc[u];
  }
  a[y] = 0;
  DFS2(y, 0);
  if (f.size() == 0) {
    printf("0 %lld\n", dis[x] - dis[y]);
    return 0;
  }
  if (f.size() == 1) {
    vector<long long> p = f[0];
    t = anc[p.back()];
    x1 = p.front();
    x2 = t;
    len = dis[x1] - dis[t] + 1;
    for (auto it : p) {
      A.push_back(a[it]);
      B.push_back(b[it]);
    }
  }
  if (f.size() == 2) {
    vector<long long> p = f[0];
    t = anc[p.back()];
    len = dis[p.front()] - dis[t];
    x1 = p.front();
    for (auto it : p) {
      A.push_back(a[it]);
      B.push_back(b[it]);
    }
    p = f[1];
    if (anc[p.back()] != t) {
      puts("-1");
      return 0;
    }
    len += dis[p.front()] - dis[t] + 1;
    x2 = p.front();
    for (long long i = p.size() - 1; i >= 0; --i) {
      A.push_back(a[p[i]]);
      B.push_back(b[p[i]]);
    }
  }
  long long ans = Dis(x, t) + Dis(t, y);
  long long pos = -1;
  for (long long i = 0; i < B.size(); ++i)
    if (B[i] == A.front()) pos = i;
  if (pos == -1) {
    puts("-1");
    return 0;
  }
  for (long long i = 0; i < A.size(); ++i)
    if (A[i] != B[(i + pos) % B.size()]) {
      puts("-1");
      return 0;
    }
  long long sum1 = pos * len, sum2 = (len - pos - 1) * len;
  if (f.size() == 2) {
    vector<long long> p, q = f[0];
    while (x) {
      p.push_back(x);
      x = anc[x];
    }
    while (!p.empty() && p.back() != t) p.pop_back();
    if (!p.empty() && p.back() == t) p.pop_back();
    vector<long long> g = p;
    if (p.empty()) goto Next;
    while (!p.empty() && !q.empty() && p.back() == q.back()) {
      sum1 -= 2;
      p.pop_back();
      q.pop_back();
    }
    p = g;
    q = f[1];
    while (!p.empty() && !q.empty() && p.back() == q.back()) {
      sum2 -= 2;
      p.pop_back();
      q.pop_back();
    }
  }
Next:;
  ans += min(sum1, sum2);
  if (x1 > x2) swap(x1, x2);
  printf("%lld %lld %lld\n", x1, x2, ans);
  return 0;
}