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 = 4e5 + 10;
int w[N], ne[N], la[N];
int a[N], b[N], r, n, t, sw[N], fa[N], q, s1[N], dep[N], nx[N], s2[N];
void link(int x, int y) {
  w[++t] = y;
  ne[t] = la[x];
  la[x] = t;
}
void dfs(int x) {
  for (int y = la[x]; y; y = ne[y]) {
    int z = w[y];
    if (z == fa[x]) continue;
    fa[z] = x;
    dep[z] = dep[x] + 1;
    dfs(z);
  }
}
void go(int x) {
  if (x == r) return;
  if (fa[x] != r) go(fa[x]);
  swap(a[fa[x]], a[x]), sw[x] = 1;
  nx[fa[x]] = x;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]), r = a[i] ? r : i;
  for (int i = 1; i <= n; i++) scanf("%d", &b[i]), q = b[i] ? q : i;
  int tag1 = 0;
  if (n == 200000 && a[1] == 13809) {
    tag1 = 0;
  }
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    link(x, y);
    link(y, x);
  }
  dfs(r);
  long long ans = dep[q];
  go(q);
  sw[r] = 1;
  int case0 = 0;
  for (int i = 1; i <= n; i++) {
    if (a[i] != b[i] && sw[i]) {
      case0 = 1;
    }
  }
  if (case0) {
    int cnt1 = 0, cnt2 = 0;
    for (int x = 1; x <= n; x++) {
      int cnt = 0;
      if (a[x] != b[x]) {
        for (int y = la[x]; y; y = ne[y]) cnt += a[w[y]] != b[w[y]];
        if (cnt > 2) {
          cout << -1 << endl;
          return 0;
        }
        if (cnt == 1) cnt1++;
        s1[x] = cnt;
        s2[x] = cnt - (a[fa[x]] != b[fa[x]]);
        if (cnt == 0) cnt1 += 2;
      } else {
        if (sw[x] && b[x]) {
          for (int y = la[x]; y; y = ne[y]) cnt += a[w[y]] != b[w[y]];
          if (cnt == 2 && a[fa[x]] != b[fa[x]]) {
            cnt2++;
            s1[x] = 100;
            for (int y = la[x]; y; y = ne[y]) {
              int z = w[y];
              if (z != fa[x] && a[z] != b[z] && sw[z]) {
                cnt2 = 100;
              }
            }
          }
        }
      }
    }
    if (cnt1 == 2 && cnt2 == 0) {
      int x, y, z, X, Y, l = 0;
      for (int i = 1; i <= n; i++) {
        if (s1[i] == 1) y = x, x = i;
        if (a[i] != b[i] && (l == 0 || dep[i] < dep[l])) l = i;
      }
      if (sw[x] && sw[y]) {
        if (dep[x] > dep[y]) swap(x, y);
        X = x, z = Y = nx[y];
        vector<int> a0;
        while (y != x) a0.push_back(y), y = fa[y];
        a0.push_back(x);
        reverse(a0.begin(), a0.end());
        int sum = 0, sum2 = 0;
        for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
        int k = 0;
        for (int i = 0; i < a0.size(); i++)
          if (a[a0[i]] == b[a0[0]]) {
            k = i;
            break;
          }
        for (int i = 0; i < a0.size(); i++)
          if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
            cout << -1 << endl;
            return 0;
          }
        long long ans1 = (a0.size() + 1) * k,
                  ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
        if (tag1) {
          cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
        }
        if (tag1) {
          cout << 1 << endl;
        }
        cout << min(X, Y) << ' ' << max(X, Y) << ' ';
        cout << ans + sum2 + min(ans1, ans2) << endl;
        return 0;
      }
      if (sw[x] + sw[y] == 1) {
        if (sw[x]) swap(x, y);
        X = x, Y = nx[y];
        vector<int> a1, a2, a0;
        for (; x != l; x = fa[x]) a1.push_back(x);
        a1.push_back(l);
        for (; y != l; y = fa[y]) a2.push_back(y);
        a0 = a1;
        while (a2.size()) a0.push_back(a2.back()), a2.pop_back();
        int sum = 0, sum2 = 0;
        for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
        int k = 0;
        for (int i = 0; i < a0.size(); i++)
          if (a[a0[i]] == b[a0[0]]) {
            k = i;
            break;
          }
        for (int i = 0; i < a0.size(); i++)
          if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
            cout << -1 << endl;
            return 0;
          }
        long long ans1 = (a0.size() + 1) * k,
                  ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
        if (tag1) {
          cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
        }
        if (tag1) {
          cout << 2 << endl;
        }
        cout << min(X, Y) << ' ' << max(X, Y) << ' ';
        cout << ans + sum2 + min(ans1, ans2) << endl;
        return 0;
      }
      cout << -1 << endl;
      return 0;
    }
    if (cnt1 == 4 && cnt2 == 1) {
      int x, y, u, X, Y, l = 0;
      for (int i = 1; i <= n; i++)
        if (s1[i] == 100) u = i;
      for (int i = 1; i <= n; i++) {
        if ((s1[i] == 1 && !sw[i] || s1[i] == 0) && s2[i] == 0 && a[i] != b[i])
          y = x, x = i;
        if (a[i] != b[i] && (l == 0 || dep[i] < dep[l])) l = i;
      }
      if (fa[l] == u) {
        cout << -1 << endl;
        return 0;
      }
      static int tag0[N];
      X = x, Y = y;
      assert(sw[l]);
      assert(a[l] != b[l]);
      int x0 = x, y0 = y;
      while (x0 != l && x0 != u) {
        assert(a[x0] != b[x0]);
        assert(sw[x0] == 0);
        tag0[x0] = 1;
        x0 = fa[x0];
      }
      while (y0 != l && y0 != u) {
        assert(a[y0] != b[y0]);
        assert(sw[y0] == 0);
        tag0[y0] = 1;
        y0 = fa[y0];
      }
      assert(x0 != y0);
      int u0 = fa[u];
      assert(sw[u] && a[u] == b[u]);
      while (u0 != l) {
        assert(sw[u0]);
        assert(a[u0] != b[u0]);
        tag0[u0] = 1;
        u0 = fa[u0];
      }
      tag0[l] = 1;
      for (int i = 1; i <= n; i++) {
        if (tag0[i] != (a[i] != b[i])) {
          cout << -1 << endl;
          return 0;
        }
        assert(tag0[i] == (a[i] != b[i]));
      }
      vector<int> a1, a2, a0;
      int tag = 0;
      for (; x != l; x = fa[x]) a1.push_back(x), tag |= 1 * (x == u);
      for (; y != l; y = fa[y]) a2.push_back(y), tag |= 2 * (y == u);
      if (tag == 1) swap(a1, a2);
      a0 = a1;
      a0.push_back(l);
      while (a2.size()) {
        if (a2.back() != u) a0.push_back(a2.back());
        a2.pop_back();
      }
      int sum = 0, sum2 = 0;
      for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
      int k = 0;
      for (int i = 0; i < a0.size(); i++)
        if (a[a0[i]] == b[a0[0]]) {
          k = i;
          break;
        }
      for (int i = 0; i < a0.size(); i++)
        if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
          cout << -1 << endl;
          return 0;
        }
      long long ans1 = (a0.size() + 1) * k,
                ans2 = (a0.size() + 1) * (a0.size() - k) - 2 * sum;
      if (tag1) {
        cout << ans1 << ' ' << ans2 << ' ' << sum << endl;
      }
      if (tag1) {
        cout << l << ' ' << u << ' ' << dep[l] << ' ' << dep[u] << ' ' << dep[X]
             << ' ' << dep[Y] << ' ' << sw[u] << ' ' << nx[u] << endl;
        cout << 3 << endl;
      }
      cout << min(X, Y) << ' ' << max(X, Y) << ' ';
      cout << ans + sum2 + min(ans1, ans2) << endl;
      return 0;
    }
    cout << -1 << endl;
    return 0;
  }
  int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, cnt5 = 0;
  for (int x = 1; x <= n; x++) {
    int cnt = 0;
    if (a[x] == b[x]) {
      for (int y = la[x]; y; y = ne[y])
        if (fa[x] != w[y]) cnt += a[w[y]] != b[w[y]];
      if (cnt == 2) cnt1++;
      if (cnt == 1) cnt3++;
      if (cnt > 2) cnt4++;
    } else {
      cnt5++;
      for (int y = la[x]; y; y = ne[y])
        if (fa[x] != w[y]) cnt += a[w[y]] != b[w[y]];
      if (cnt == 0) cnt2++;
      if (cnt > 1) cnt4++;
    }
    s1[x] = cnt;
  }
  if (!cnt5) {
    cout << 0 << ' ' << ans << endl;
    return 0;
  }
  if (cnt4) {
    cout << -1 << endl;
    return 0;
  }
  if (cnt1 == 1 && cnt2 == 2) {
    int x, y, u, v, X, Y, z;
    for (int i = 1; i <= n; i++) {
      if (a[i] != b[i] && a[fa[i]] == b[fa[i]]) v = u, u = i;
      if (a[i] != b[i] && s1[i] == 0) y = x, x = i;
    }
    X = x, Y = y;
    z = fa[u];
    assert(z == fa[v]);
    if (sw[v]) swap(u, v);
    vector<int> a1, a2, a0;
    for (; x != u && x != v; x = fa[x]) a1.push_back(x);
    a1.push_back(x);
    for (; y != u && y != v; y = fa[y]) a2.push_back(y);
    a2.push_back(y);
    if (a2.back() == u) swap(a1, a2);
    a0 = a1;
    while (a2.size()) a0.push_back(a2.back()), a2.pop_back();
    int sum = 0, sum2 = 0;
    while (!sw[z] && z != r) sum2 += 2, z = fa[z];
    for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
    int k = 0;
    for (int i = 0; i < a0.size(); i++)
      if (a[a0[i]] == b[a0[0]]) {
        k = i;
        break;
      }
    for (int i = 0; i < a0.size(); i++)
      if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
        cout << -1 << endl;
        return 0;
      }
    long long ans1 = (a0.size() + 1) * k - 2 * sum,
              ans2 = (a0.size() + 1) * (a0.size() - k);
    if (tag1) {
      cout << 4 << endl;
    }
    cout << min(X, Y) << ' ' << max(X, Y) << ' ';
    cout << ans + sum2 + min(ans1, ans2) << endl;
    return 0;
  }
  if (cnt3 == 1 && cnt2 == 1) {
    int x, y, z, X, Y;
    for (int i = 1; i <= n; i++) {
      if (a[i] != b[i] && a[fa[i]] == b[fa[i]]) x = i;
      if (a[i] != b[i] && s1[i] == 0) y = i;
    }
    X = fa[x];
    Y = y;
    z = fa[x];
    vector<int> a0;
    while (y != x) a0.push_back(y), y = fa[y];
    a0.push_back(x);
    reverse(a0.begin(), a0.end());
    int sum = 0, sum2 = 0;
    while (!sw[z] && z != r) sum2 += 2, z = fa[z];
    for (int i = 0; i < a0.size(); i++) sum += sw[a0[i]];
    int k = 0;
    for (int i = 0; i < a0.size(); i++)
      if (a[a0[i]] == b[a0[0]]) {
        k = i;
        break;
      }
    for (int i = 0; i < a0.size(); i++)
      if (a[a0[(i + k) % a0.size()]] != b[a0[i]]) {
        cout << -1 << endl;
        return 0;
      }
    long long ans1 = (a0.size() + 1) * k - 2 * sum,
              ans2 = (a0.size() + 1) * (a0.size() - k);
    if (tag1) {
      cout << 5 << endl;
    }
    cout << min(X, Y) << ' ' << max(X, Y) << ' ';
    cout << ans + sum2 + min(ans1, ans2) << endl;
    return 0;
  }
  cout << -1 << endl;
}