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;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200000;
struct edge {
  int id, nxt;
  edge() {}
  edge(int _id, int _nxt) : id(_id), nxt(_nxt) {}
};
edge e[(maxn << 1) + 5];
int st[maxn + 5], en = 0;
inline void add_edge(int first, int second) {
  e[en] = edge(second, st[first]), st[first] = en++;
}
int n;
int dep[maxn + 5], fa[maxn + 5];
int dfs_tot = 0;
int dfn[maxn + 5], End[maxn + 5];
void dfs(int first, int f = -1) {
  if (~f)
    dep[first] = dep[f] + 1;
  else
    dep[first] = 0;
  dfn[first] = dfs_tot++;
  fa[first] = f;
  for (int i = st[first]; i != -1; i = e[i].nxt) {
    int second = e[i].id;
    if (second == f) continue;
    dfs(second, first);
  }
  End[first] = dfs_tot;
}
int pa, push_back;
int a[maxn + 5], b[maxn + 5];
int main() {
  memset(st, -1, sizeof st), en = 0;
  scanf("%d", &n);
  for (int i = (0), _end_ = (n); i < _end_; ++i) scanf("%d", a + i);
  pa = find(a, a + n, 0) - a;
  for (int i = (0), _end_ = (n); i < _end_; ++i) scanf("%d", b + i);
  push_back = find(b, b + n, 0) - b;
  for (int i = (0), _end_ = (n - 1); i < _end_; ++i) {
    int first, second;
    scanf("%d%d", &first, &second), --first, --second;
    add_edge(first, second), add_edge(second, first);
  }
  dfs(push_back);
  int u = pa;
  while (u != push_back) {
    a[u] = a[fa[u]];
    u = fa[u];
  }
  a[u] = 0;
  int first = -1, rt = -1;
  int cnt = 0;
  for (int i = (0), _end_ = (n); i < _end_; ++i)
    if (a[i] != b[i]) {
      ++cnt;
      if (!~rt || dep[i] < dep[rt]) rt = i;
      if (!~first || dep[i] > dep[first]) first = i;
    }
  if (!cnt) printf("0 %d\n", dep[pa]), exit(0);
  rt = fa[rt];
  int second = rt;
  for (int i = (0), _end_ = (n); i < _end_; ++i)
    if (a[i] != b[i] && (End[i] <= dfn[first] || dfn[i] > dfn[first]) &&
        dep[i] > dep[second])
      second = i;
  if (dep[first] + dep[second] - (dep[rt] << 1) != cnt) printf("-1\n"), exit(0);
  vector<int> all;
  int id = first;
  while (id != rt) {
    all.push_back(id);
    id = fa[id];
  }
  reverse((all).begin(), (all).end());
  id = second;
  while (id != rt) {
    all.push_back(id);
    id = fa[id];
  }
  int go = -1;
  for (int i = (0), _end_ = ((int((all).size()))); i < _end_; ++i)
    if (a[all[0]] == b[all[i]]) {
      go = i;
      break;
    }
  if (!~go) printf("-1\n"), exit(0);
  for (int i = (0), _end_ = ((int((all).size()))); i < _end_; ++i)
    if (a[all[i]] != b[all[(i + go) % (int((all).size()))]])
      printf("-1\n"), exit(0);
  long long ans = dep[pa];
  static bool vis[maxn + 5];
  memset(vis, 0, sizeof vis);
  id = pa;
  while (~id) {
    vis[id] = 1;
    id = fa[id];
  }
  if (vis[all[0]] || vis[all.back()]) {
    if (vis[all.back()])
      reverse((all).begin(), (all).end()), go = (int((all).size())) - go;
    int tmp = (int((all).size()));
    for (int i = (1), _end_ = ((int((all).size()))); i < _end_; ++i)
      if (!vis[all[i]]) {
        tmp = i;
        break;
      }
    int cyc_len = (int((all).size())) + 1;
    ans -= tmp;
    ans += min(abs(tmp + (long long)go * cyc_len),
               abs(tmp + (long long)(go - (int((all).size()))) * cyc_len));
  } else
    ans += min(go, (int((all).size())) - go) * (cnt + 1);
  id = rt;
  while (!vis[id]) {
    ans += 2;
    id = fa[id];
  }
  if (first > second) swap(first, second);
  printf("%d %d %I64d\n", first + 1, second + 1, ans);
  return 0;
}