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 N = 2e5 + 7;
long long val;
bool vas, fl[N], ft[N], gt[N], qt[N];
int n, p1[N], p2[N], p3[N], og[N], vac[N], st[N << 2];
vector<int> v[N];
inline int read() {
  int num = 0;
  char g = getchar();
  while (g < 48 || 57 < g) g = getchar();
  while (47 < g && g < 58)
    num = (num << 1) + (num << 3) + g - 48, g = getchar();
  return num;
}
inline void calc(int u, int t) {
  st[++st[0]] = u;
  if (u == t) {
    for (int i = 1; i <= st[0]; i++) qt[st[i]] = 1;
    for (int i = 1; i < st[0]; i++)
      swap(p1[st[i]], p1[st[i + 1]]), val++, vac[st[i + 1]] = st[i];
    st[0]--;
    return;
  }
  for (int i = 0; i < v[u].size(); i++)
    if (st[0] == 1 || st[st[0] - 1] != v[u][i]) calc(v[u][i], t);
  st[0]--;
}
inline pair<int, int> getans() {
  int a = 0, b = 0;
  for (int i = 1; i <= n; i++)
    if (!ft[i] && fl[i]) a = i, b = i;
  ft[a] = ft[b] = 1;
  while (1) {
    for (int i = 0; i < v[a].size(); i++)
      if (!ft[v[a][i]] && fl[v[a][i]]) {
        a = v[a][i];
        break;
      }
    if (ft[a]) break;
    ft[a] = 1;
  }
  swap(a, b);
  while (1) {
    for (int i = 0; i < v[a].size(); i++)
      if (!ft[v[a][i]] && fl[v[a][i]]) {
        a = v[a][i];
        break;
      }
    if (ft[a]) break;
    ft[a] = 1;
  }
  return make_pair(a, b);
}
inline bool check() {
  for (int i = 1; i <= n; i++)
    if (p1[i] != p2[i]) return 0;
  return 1;
}
inline long long getval() {
  memcpy(p3, p1, sizeof(p3));
  int a = 0, b = 0;
  long long sum = 0;
  for (int i = 1; i <= st[0] * 2; i++) {
    int x = st[i];
    if (p1[x] == 0) a = i, b = 0;
    if (p2[x] == 0) b = i;
    if (a > 0 && b > 0) break;
  }
  for (int i = a; i < b; i++) swap(p3[st[i]], p3[st[i + 1]]), sum++;
  int rt = 0, len = 0, pos = 0;
  for (int i = 1; i <= st[0]; i++)
    if (p3[st[i]]) og[len++] = st[i];
  for (int i = 0; i < len; i++)
    if (p2[og[i]] == p3[og[0]]) pos = i;
  for (int i = 0; i < len; i++)
    if (p2[og[(i + pos) % len]] != p3[og[i]]) return -1;
  if ((len - pos) % len > 0) {
    for (int i = 1; i <= st[0]; i++)
      if (vac[st[i]] == st[i + 1]) sum -= 2;
  } else {
    for (int i = a; i < b; i++)
      if (vac[st[i]] == st[i + 1]) sum -= 2;
  }
  return 1ll * ((len - pos) % len) * (len + 1) + sum;
}
inline void solvex(int u) {
  gt[u] = 1;
  st[++st[0]] = u;
  for (int i = 0; i < v[u].size(); i++) {
    int x = v[u][i];
    if (!gt[x] && fl[x]) {
      solvex(x);
      return;
    }
  }
}
inline void solvey(int u, int fa, int c) {
  if (vas) return;
  if (fl[u]) {
    vas = 1;
    if (vac[fa] != u) c -= 2;
    val += c, p1[fa] = p2[fa] = 0, fl[fa] = 1;
    return;
  }
  for (int i = 0; i < v[u].size(); i++) {
    int x = v[u][i];
    if (x == fa) continue;
    if (vac[u] != x)
      solvey(x, u, c + 2);
    else
      solvey(x, u, c);
  }
}
inline void solvez(int u, int fa) {
  if (fl[u]) {
    p2[u] = 0;
    return;
  }
  for (int i = 0; i < v[u].size(); i++) {
    int x = v[u][i];
    if (x == fa) continue;
    solvez(x, u);
  }
}
int main() {
  n = read();
  int u, t;
  for (int i = 1; i <= n; i++) p1[i] = read();
  for (int i = 1; i <= n; i++) p2[i] = read();
  for (int i = 1; i < n; i++)
    u = read(), t = read(), v[u].push_back(t), v[t].push_back(u);
  for (int i = 1; i <= n; i++)
    if (p1[i] == 0) u = i;
  for (int i = 1; i <= n; i++)
    if (p2[i] == 0) t = i;
  calc(u, t);
  if (check()) {
    cout << 0 << " " << val << endl;
    return 0;
  }
  int pos = 0;
  for (int i = 1; i <= n; i++)
    if (p1[i] != p2[i] || i == pos) fl[i] = 1;
  solvey(t, 0, 0);
  pair<int, int> f1 = getans(), f2 = getans();
  if (f2.first || f2.second) {
    cout << -1 << endl;
    return 0;
  }
  memset(gt, 0, sizeof(gt)), solvex(f1.first);
  for (int i = 1; i <= st[0] + st[0]; i++)
    st[i + st[0] + st[0]] = st[i + st[0]] = st[i];
  long long v1 = getval();
  for (int i = 1; i <= st[0] / 2; i++) swap(st[i], st[st[0] - i + 1]);
  for (int i = 1; i <= st[0] + st[0]; i++)
    st[i + st[0] + st[0]] = st[i + st[0]] = st[i];
  long long v2 = getval();
  if (v1 == -1 && v2 == -1) {
    cout << -1 << endl;
    return 0;
  }
  val += min(v1, v2);
  if (f1.first > f1.second) swap(f1.first, f1.second);
  cout << f1.first << " " << f1.second << " " << val << endl;
}