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, typename S>
inline bool upmin(T& a, const S& b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T, typename S>
inline bool upmax(T& a, const S& b) {
  return a < b ? a = b, 1 : 0;
}
template <typename N, typename PN>
inline N flo(N a, PN b) {
  return a >= 0 ? a / b : -((-a - 1) / b) - 1;
}
template <typename N, typename PN>
inline N cei(N a, PN b) {
  return a > 0 ? (a - 1) / b + 1 : -(-a / b);
}
template <typename N>
N gcd(N a, N b) {
  return b ? gcd(b, a % b) : a;
}
inline void gn(long long& x) {
  int sg = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (sg = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= sg;
}
inline void gn(int& x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(unsigned long long& x) {
  long long t;
  gn(t);
  x = t;
}
inline void gn(double& x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline void gn(long double& x) {
  double t;
  scanf("%lf", &t);
  x = t;
}
inline long long sqr(long long a) { return a * a; }
inline double sqrf(double a) { return a * a; }
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
int mo = 1000000007;
int qp(int a, long long b) {
  int n = 1;
  do {
    if (b & 1) n = 1ll * n * a % mo;
    a = 1ll * a * a % mo;
  } while (b >>= 1);
  return n;
}
const int TREE_MAXV = 200000 + 5;
struct edge {
  int v, next;
} e[TREE_MAXV * 2];
int g[TREE_MAXV], etot;
int qu[TREE_MAXV], pre[TREE_MAXV], h[TREE_MAXV];
int n;
void ae(int u, int v) {
  e[etot].v = v;
  e[etot].next = g[u];
  g[u] = etot++;
}
void bfs(int rt) {
  int p = 0, q = 0;
  pre[rt] = 0;
  h[rt] = 0;
  qu[q++] = rt;
  while (p != q) {
    int u = qu[p++];
    for (int i = g[u]; ~i; i = e[i].next)
      if (e[i].v != pre[u]) {
        pre[e[i].v] = u;
        h[e[i].v] = h[u] + 1;
        qu[q++] = e[i].v;
      }
  }
}
void tree_init() {
  static bool ini = 0;
  if (!ini) {
    ini = 1;
    memset(g, -1, sizeof(g));
  } else {
    for (int i = 0; i <= n; i++) g[i] = -1;
  }
  etot = 0;
}
void readedge() {
  for (int i = 1; i < n; i++) {
    int x, y;
    gn(x);
    gn(y);
    ae(x, y);
    ae(y, x);
  }
}
int ai[222222], bi[222222];
int a0, b0;
int aii[222222];
int dif[222222];
int des[222222];
int lis[222222];
int ltot = 0;
int s1[422222], s2[422222];
int on[222222];
int disb, disa;
int lcapos;
long long gao(int ia, int ib, int ia0, int ib0, int ltot) {
  long long t = 0;
  ia--, ib--, ia0--, ib0--;
  while (ia0 != ib0) {
    if ((ia0 + 1) % ltot == ia) ia = ia0;
    ia0 = (ia0 + 1) % ltot;
    t++;
  }
  while (ia != ib) {
    t += ltot;
    ia = (ia + ltot - 1) % ltot;
    if (ia == ia0) ia = (ia + ltot - 1) % ltot;
  }
  return t;
}
int main() {
  tree_init();
  gn(n);
  for (int i = (1), _ed = (n + 1); i < _ed; i++) {
    gn(ai[i]);
    if (ai[i] == 0) a0 = i;
    aii[i] = ai[i];
  }
  for (int i = (1), _ed = (n + 1); i < _ed; i++) {
    gn(bi[i]);
    if (bi[i] == 0) b0 = i;
  }
  readedge();
  bfs(b0);
  int u = a0;
  int hi = 0;
  while (u != b0) {
    hi++;
    swap(aii[u], aii[pre[u]]);
    u = pre[u];
  }
  int bo = 0;
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (aii[i] != bi[i]) bo++, dif[i] = 1;
  if (bo == 0) {
    printf("%d %d\n", 0, hi);
    return 0;
  }
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (dif[i]) {
      u = i;
      break;
    }
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (dif[i] && h[i] > h[u]) u = i;
  int w = u;
  while (w) {
    des[w] ^= 1;
    w = pre[w];
  }
  int v;
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (dif[i] != des[i]) {
      v = i;
      break;
    }
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (dif[i] != des[i] && h[i] > h[v]) {
      v = i;
    }
  w = v;
  while (w) {
    des[w] ^= 1;
    w = pre[w];
  }
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (dif[i] != des[i]) {
      printf("-1\n");
      return 0;
    }
  bfs(u);
  w = v;
  int lca;
  while (w) {
    if (dif[w])
      lis[++ltot] = w;
    else
      lca = w, lcapos = ltot;
    w = pre[w];
  }
  bfs(b0);
  disb = h[lca];
  w = u;
  while (w) {
    des[w] ^= 1;
    w = pre[w];
  }
  w = v;
  while (w) {
    des[w] ^= 1;
    w = pre[w];
  }
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++) {
    s1[i] = aii[lis[i]];
    s2[i] = bi[lis[i]];
  }
  int st = 0;
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++)
    if (s1[i] == s2[1]) {
      st = i;
      break;
    }
  if (!st) {
    printf("-1\n");
    return 0;
  }
  s1[0] = s1[ltot];
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++)
    if (s1[(st - 1 + i) % ltot] != s2[i]) {
      printf("-1\n");
      return 0;
    }
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++) {
    on[lis[i]] = 1;
  }
  on[lca] = 1;
  bfs(a0);
  int au = lca;
  for (int i = (1), _ed = (n + 1); i < _ed; i++)
    if (on[i] && h[i] < h[au]) au = i;
  disa = h[au];
  for (int i = ltot; i > lcapos; i--) lis[i + 1] = lis[i];
  lis[lcapos + 1] = lca;
  ltot++;
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++)
    s1[i] = lis[i] == au ? 0 : ai[lis[i]],
    s2[i] = lis[i] == lca ? 0 : bi[lis[i]];
  long long ans = disa + disb;
  int c, ia, ib;
  int ia0, ib0;
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++)
    if (s1[i]) {
      c = s1[i];
      ia = i;
      break;
    }
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++)
    if (s2[i] == c) {
      ib = i;
      break;
    }
  for (int i = (1), _ed = (ltot + 1); i < _ed; i++) {
    if (s1[i] == 0) ia0 = i;
    if (s2[i] == 0) ib0 = i;
  }
  ans += min(
      gao(ia, ib, ia0, ib0, ltot),
      gao(ltot - ia + 1, ltot - ib + 1, ltot - ia0 + 1, ltot - ib0 + 1, ltot));
  cout << min(u, v) << " " << max(u, v) << " " << ans << endl;
  return 0;
}