#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 3) + (x << 1) + (c ^ 48);
    c = getchar();
  }
  return f == 1 ? x : ~x + 1;
}
void Fail() {
  printf("-1\n");
  exit(0);
}
long long n;
long long a[200010], b[200010];
long long dep[200010];
vector<long long> vec[200010];
long long s, t;
vector<long long> to;
bool dfs(long long x, long long fa) {
  to.push_back(x);
  if (x == t) {
    return 1;
  }
  for (long long v : vec[x]) {
    if (v != fa && dfs(v, x)) return 1;
  }
  to.pop_back();
  return 0;
}
bool vis[200010];
long long deg[200010];
long long up = -1;
vector<long long> A, B;
vector<long long> cir;
void getcircle(long long x, long long fa) {
  dep[x] = dep[fa] + 1;
  if (a[x] != b[x]) {
    if (up == -1 || dep[up] >= dep[x]) up = fa;
    cir.push_back(x);
    vis[x] = 1;
  }
  for (long long v : vec[x]) {
    if (v != fa) getcircle(v, x);
  }
}
void getseq(long long x, long long fa) {
  if (x != up) {
    A.push_back(a[x]);
    B.push_back(b[x]);
  }
  for (long long v : vec[x]) {
    if (vis[v] && v != fa) getseq(v, x);
  }
}
long long calc(vector<long long> A, vector<long long> B) {
  long long siz = A.size();
  long long pos = 0;
  for (long long i = 0; i < siz; ++i) {
    if (B[i] == A[0]) {
      pos = i;
      break;
    }
  }
  if (pos == 0) return 0;
  for (long long i = 0; i < siz; ++i) {
    if (A[i] != B[(i + pos) % siz]) return 0;
  }
  return pos;
}
long long getdis(long long x, long long fa, long long dis, long long y) {
  if (x == y) return dis;
  for (long long v : vec[x]) {
    if (v == fa) continue;
    long long d = getdis(v, x, dis + 1, y);
    if (d) return d;
  }
  return 0;
}
long long dis(long long x, long long y) { return getdis(x, 0, 0, y); }
long long get(long long u, long long v) {
  long long c = calc(A, B);
  return dis(s, u) + (c - 1) * (A.size() + 1) + 1 + dis(v, t);
}
signed main() {
  n = read();
  for (long long i = 1; i <= n; ++i) a[i] = read();
  for (long long i = 1; i <= n; ++i) b[i] = read();
  for (long long i = 1; i <= n; ++i)
    if (a[i] == 0) s = i;
  for (long long i = 1; i <= n; ++i)
    if (b[i] == 0) t = i;
  for (long long i = 1; i < n; ++i) {
    long long u = read(), v = read();
    vec[u].push_back(v), vec[v].push_back(u);
  }
  dfs(s, 0);
  for (long long i = 0; i < to.size() - 1; ++i) swap(a[to[i]], a[to[i + 1]]);
  bool ok = 1;
  for (long long i = 1; i <= n; ++i) ok = ok & (a[i] == b[i]);
  if (ok) {
    printf("0 %d\n", to.size() - 1);
    return 0;
  }
  getcircle(t, 0);
  vis[up] = 1;
  cir.push_back(up);
  vector<long long> u;
  for (long long x : cir) {
    for (long long v : vec[x]) {
      if (vis[v]) ++deg[x];
    }
    if (deg[x] == 1) {
      u.push_back(x);
    } else if (deg[x] != 2) {
      Fail();
    }
  }
  if (u.size() != 2) {
    Fail();
  }
  if (u[0] > u[1]) swap(u[0], u[1]);
  getseq(u[0], 0);
  if (!calc(A, B)) {
    Fail();
  }
  printf("%d %d ", u[0], u[1]);
  long long ans = get(u[0], u[1]);
  reverse(A.begin(), A.end());
  reverse(B.begin(), B.end());
  printf("%lld\n", min(ans, get(u[1], u[0])));
}
