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 mod = 1e9 + 7;
inline int Add(int x) { return x >= mod ? x - mod : x; }
inline void Add(int &x, int y) {
  x += y;
  if (x >= mod) x -= mod;
}
inline int Sub(int x) { return x < 0 ? x + mod : x; }
inline void Sub(int &x, int y) { x = Sub(x - y); }
inline int Mul(int x, int y) { return (int)((unsigned long long)x * y % mod); }
inline int Mul(int x, int y, int z) {
  return (int)((unsigned long long)x * y % mod * z % mod);
}
int Pow(int x, int y) {
  int z = 1;
  for (; y; y >>= 1) {
    if (y & 1) z = Mul(z, x);
    x = Mul(x, x);
  }
  return z;
}
int Inv(int x) { return Pow(x, mod - 2); }
template <class T>
inline void Max(T &x, T y) {
  if (y > x) x = y;
}
template <class T>
inline void Min(T &x, T y) {
  if (y < x) x = y;
}
void Reverse(vector<int> &v) { reverse(v.begin(), v.end()); }
void Concat(vector<int> &a, vector<int> &b) {
  copy(b.begin(), b.end(), inserter(a, a.end()));
}
const int maxn = 1e6;
int n, S, T, A, B;
vector<int> g[maxn];
int label[maxn], origin[maxn], target[maxn];
int par[maxn], dep[maxn];
int id[maxn], ord[maxn];
int spos[maxn], tpos[maxn];
void Die(void) {
  puts("-1");
  exit(0);
}
void DfsI(int u) {
  for (int v : g[u]) {
    if (v == par[u]) continue;
    dep[v] = dep[u] + 1;
    par[v] = u;
    DfsI(v);
  }
}
void InitTree(int r) {
  par[r] = -1;
  dep[r] = 0;
  DfsI(r);
}
vector<int> GetPath(int s, int t) {
  vector<int> L1, L2;
  while (dep[s] > dep[t]) {
    L1.push_back(s);
    s = par[s];
  }
  while (dep[t] > dep[s]) {
    L2.push_back(t);
    t = par[t];
  }
  while (s != t) {
    L1.push_back(s);
    s = par[s];
    L2.push_back(t);
    t = par[t];
  }
  L1.push_back(s);
  Reverse(L2);
  Concat(L1, L2);
  return L1;
}
int Dist(int s, int t) { return (int)GetPath(s, t).size() - 1; }
void DfsF(int u, vector<int> &L) {
  L.push_back(u);
  int cnt = 0;
  for (int v : g[u]) {
    if (v == par[u] || label[v] == target[v]) continue;
    if (++cnt == 1) {
      DfsF(v, L);
    } else {
      Die();
    }
  }
}
void FindOrDie(int u) {
  vector<int> line1, line2;
  int cnt = 0;
  for (int v : g[u]) {
    if (v == par[u] || label[v] == target[v]) continue;
    switch (++cnt) {
      case 1:
        DfsF(v, line1);
        break;
      case 2:
        DfsF(v, line2);
        break;
      default:
        Die();
    }
  }
  A = line1.back();
  B = cnt == 2 ? line2.back() : u;
  int tot1 = (int)(line1.size() + line2.size());
  int tot2 = 0;
  for (int i = 0; i < n; ++i) {
    if (label[i] != target[i]) ++tot2;
  }
  if (tot1 != tot2) Die();
}
long long Solve(int low) {
  vector<int> path = GetPath(A, B);
  int clk = 0;
  for (int x : path) {
    if (x != low) {
      id[x] = clk;
      ord[clk++] = x;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (label[i] != -1) {
      spos[label[i]] = i;
    }
    if (target[i] != -1) {
      tpos[target[i]] = i;
    }
  }
  int c = id[tpos[label[ord[0]]]];
  for (int i = 0; i < path.size(); ++i) {
    int u = path[i];
    if (u == low) continue;
    int p = label[u];
    if ((id[tpos[p]] - id[spos[p]] + clk) % clk != c) {
      Die();
    }
  }
  return Dist(S, A) + (long long)(c - 1) * (clk + 1) + 1 + Dist(B, T);
}
int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", label + i);
    if (--label[i] == -1) {
      S = i;
    }
  }
  for (int i = 0; i < n; ++i) {
    scanf("%d", target + i);
    if (--target[i] == -1) {
      T = i;
    }
  }
  for (int i = 0; i < n - 1; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  InitTree(T);
  memcpy((origin), (label), sizeof(label));
  vector<int> path = GetPath(S, T);
  for (int i = 0; i < (int)path.size() - 1; ++i) {
    swap(label[path[i]], label[path[i + 1]]);
  }
  int low = -1;
  for (int i = 0; i < n; ++i) {
    if (label[i] != target[i] && (low == -1 || dep[i] < dep[low])) {
      low = i;
    }
  }
  if (low == -1) {
    int dist = (int)path.size() - 1;
    printf("0 %d\n", dist);
    return 0;
  }
  low = par[low];
  FindOrDie(low);
  long long ans = Solve(low);
  swap(A, B);
  Min(ans, Solve(low));
  if (A > B) swap(A, B);
  printf("%d %d %lld\n", A + 1, B + 1, ans);
  return 0;
}