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 MaxN = 200010, MaxM = 400010;
const long long inf = 1ll << 60;
class Graph {
 public:
  int en[MaxN], next[MaxM], to[MaxM], tot;
  void add_edge(int x, int y) {
    next[++tot] = en[x];
    en[x] = tot;
    to[tot] = y;
  }
} G;
int n, a[MaxN], b[MaxN];
int edge_u, edge_v;
bool diff[MaxN];
long long step;
inline bool check() {
  for (int i = 1; i <= n; ++i)
    if (a[i] != b[i]) return 0;
  return 1;
}
int cycle[MaxN], ctot;
bool vis[MaxN], flag;
int father[MaxN], depth[MaxN];
void dfs_init(int now) {
  vis[now] = 1;
  for (int i = G.en[now]; i; i = G.next[i])
    if (!vis[G.to[i]]) {
      father[G.to[i]] = now;
      depth[G.to[i]] = depth[now] + 1;
      dfs_init(G.to[i]);
    }
}
int get_lca(int u, int v) {
  while (u != v) {
    if (depth[u] > depth[v])
      u = father[u];
    else
      v = father[v];
  }
  return u;
}
int get_dist(int l, int u) { return depth[u] - depth[l]; }
void dfs_edge(int now) {
  vis[now] = 1;
  int deg = 0;
  for (int i = G.en[now]; i; i = G.next[i])
    if (diff[G.to[i]]) {
      ++deg;
      if (!vis[G.to[i]]) dfs_edge(G.to[i]);
    }
  if (deg == 1) {
    if (edge_u)
      edge_v = now;
    else
      edge_u = now;
  }
  if (deg > 2) flag = 1;
}
void dfs_cycle(int now) {
  cycle[++ctot] = now;
  vis[now] = 1;
  for (int i = G.en[now]; i; i = G.next[i])
    if (!vis[G.to[i]] && diff[G.to[i]]) dfs_cycle(G.to[i]);
}
int get_deg(int u) {
  int deg = 0;
  for (int i = G.en[u]; i; i = G.next[i]) deg += diff[G.to[i]];
  return deg;
}
int pos[MaxN];
void solve_cycle(int in, int out) {
  for (int u = 1; u <= n; ++u)
    if (get_deg(u) == 2) diff[u] = 1;
  memset(vis, 0, sizeof(vis));
  ;
  dfs_edge(in);
  if (flag) return;
  if (edge_u > edge_v) swap(edge_u, edge_v);
  G.add_edge(edge_u, edge_v);
  G.add_edge(edge_v, edge_u);
  memset(vis, 0, sizeof(vis));
  ;
  dfs_cycle(in);
  if (!vis[out]) return;
  int node = 0;
  for (int i = 1; i <= ctot; ++i)
    if (cycle[i] != out) {
      pos[b[cycle[i]]] = ++node;
    }
  int delta = pos[a[cycle[2]]] - 1;
  for (int i = 2; i <= ctot; ++i) {
    int tmp = pos[a[cycle[i]]] - i + 1;
    if (tmp < 0) tmp += ctot - 1;
    if (tmp != delta) return;
  }
  int post = 0;
  for (int i = 1; i <= ctot; ++i)
    if (cycle[i] == out) post = i;
  long long now = inf, st;
  st = 1ll * delta * ctot - (post - 1);
  if (st < 0) st = -st;
  now = min(now, st);
  st = 1ll * (ctot - delta - 1) * ctot + (post - 1);
  if (st < 0) st = -st;
  now = min(now, st);
  step += now;
  for (int i = 1; i <= ctot; ++i) a[cycle[i]] = b[cycle[i]];
}
void go(int& s, int* a, int* b) {
  while (true) {
    int pos = 0, deg = 0;
    for (int i = G.en[s]; i; i = G.next[i])
      if (a[G.to[i]] != b[G.to[i]]) {
        ++deg;
        if (a[G.to[i]] == b[s]) pos = G.to[i];
      }
    if (!pos || (deg != 1)) return;
    swap(a[s], a[pos]);
    s = pos;
    ++step;
  }
}
int bfs(int& s) {
  static int q[MaxN], dist[MaxN];
  int l = 0, r = 0;
  memset(dist, 63, sizeof(dist));
  q[++r] = s;
  dist[s] = 0;
  while (l != r) {
    int u = q[++l];
    for (int i = G.en[u]; i; i = G.next[i])
      if (dist[G.to[i]] > dist[u] + 1) {
        dist[G.to[i]] = dist[u] + 1;
        q[++r] = G.to[i];
        if (diff[G.to[i]]) {
          s = u;
          return dist[u];
        }
      }
  }
  return -1;
}
int main() {
  int s, t;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    if (a[i] == 0) s = i;
  }
  for (int i = 1; i <= n; ++i) {
    scanf("%d", b + i);
    if (b[i] == 0) t = i;
  }
  for (int i = 1; i < n; ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    G.add_edge(u, v);
    G.add_edge(v, u);
  }
  int ss = s, tt = t;
  go(s, a, b);
  if (check()) {
    printf("0 %I64d\n", step);
    return 0;
  }
  go(t, b, a);
  if ((a[t] == b[s]) && (a[s] == b[t])) {
    swap(a[t], a[s]);
    if (check()) {
      printf("%d %d %I64d\n", min(s, t), max(s, t), step + 1);
      return 0;
    }
    swap(a[t], a[s]);
  }
  int root = 0;
  for (int i = 1; i <= n; ++i) {
    diff[i] = (a[i] != b[i]);
    if (diff[i]) root = i;
  }
  memset(vis, 0, sizeof(vis));
  ;
  dfs_init(root);
  if (a[s] == b[s]) {
    int l = get_lca(ss, tt);
    step = get_dist(l, ss) + get_dist(l, tt);
    step += 2 * bfs(l);
    solve_cycle(l, l);
  } else
    solve_cycle(s, t);
  if (!check())
    printf("-1\n");
  else
    printf("%d %d %I64d\n", edge_u, edge_v, step);
  return 0;
}