#include <bits/stdc++.h>
using namespace std;
void read(int &x) {
  char c = getchar(), b = 1;
  for (; !(c >= '0' && c <= '9'); c = getchar())
    if (c == '-') b = -1;
  for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
    ;
  x *= b;
}
const int N = 200005;
struct edge {
  int u, v, next;
} G[N << 1];
int head[N], inum;
void add(int u, int v, int p) {
  G[p].u = u;
  G[p].v = v;
  G[p].next = head[u];
  head[u] = p;
}
int n, s, t;
int S[N], T[N], bS[N], bT[N];
int c[N], fat[N], dep[N];
int cir[N];
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  fat[u] = fa;
  for (int p = head[u]; p; p = G[p].next)
    if (G[p].v != fa && !cir[G[p].v]) dfs(G[p].v, u);
}
int vst[N], deg[N];
int tot, a[N], b[N];
int lst[N], pnt;
int Ansu, Ansv;
int main() {
  int iu, iv;
  read(n);
  for (int i = 1; i <= n; i++) {
    read(S[i]);
    if (!S[i]) s = i;
  }
  for (int i = 1; i <= n; i++) {
    read(T[i]);
    if (!T[i]) t = i;
  }
  for (int i = 1; i < n; i++) {
    read(iu), read(iv);
    add(iu, iv, ++inum);
    add(iv, iu, ++inum);
  }
  dfs(t, 0);
  int p = s;
  for (int i = 1; i <= n; i++) c[i] = S[i];
  while (p != t) swap(c[p], c[fat[p]]), p = fat[p];
  int flag = 0;
  for (int i = 1; i <= n; i++) flag |= c[i] != T[i];
  if (!flag) {
    printf("0 %d\n", dep[s] - 1);
    return 0;
  }
  for (int i = 1; i <= n; i++) vst[i] = c[i] != T[i];
  while (1) {
    int k = 0;
    for (int i = 1; i <= n; i++)
      if (vst[i]) {
        k = i;
        break;
      }
    if (!k || tot > 2) break;
    int t = k;
    while (1) {
      vst[t] = 0;
      int tmp = t;
      for (int p = head[t]; p; p = G[p].next)
        if (vst[G[p].v]) {
          t = G[p].v;
          break;
        }
      if (tmp == t) break;
    }
    a[++tot] = t;
    t = k;
    while (1) {
      vst[t] = 0;
      int tmp = t;
      for (int p = head[t]; p; p = G[p].next)
        if (vst[G[p].v]) {
          t = G[p].v;
          break;
        }
      if (tmp == t) break;
    }
    b[tot] = t;
  }
  if (tot > 2) {
    puts("-1");
    return 0;
  }
  if (tot == 1) {
    int &a = ::a[1], &b = ::b[1];
    if (dep[a] > dep[b]) swap(a, b);
    int t = b;
    while (t && t != a) t = fat[t];
    if (!t) {
      puts("-1");
      return 0;
    }
    add(fat[a], b, ++inum);
    add(b, fat[a], ++inum);
    if (fat[a] < b)
      Ansu = fat[a], Ansv = b;
    else
      Ansu = b, Ansv = fat[a];
    t = b;
    while (t != fat[a]) cir[t] = 1, t = fat[t];
    cir[fat[a]] = 1;
  } else {
    for (int i = 1; i <= 2; i++) {
      int &a = ::a[i], &b = ::b[i];
      if (dep[a] > dep[b]) swap(a, b);
      int t = b;
      while (t && t != a) t = fat[t];
      if (!t) return printf("-1\n"), 0;
    }
    if (fat[a[1]] != fat[a[2]]) return printf("-1\n"), 0;
    add(b[1], b[2], ++inum);
    add(b[2], b[1], ++inum);
    if (b[1] < b[2])
      Ansu = b[1], Ansv = b[2];
    else
      Ansu = b[2], Ansv = b[1];
    for (int i = 1; i <= 2; i++) {
      int t = b[i];
      while (t != fat[a[i]]) cir[t] = 1, t = fat[t];
      cir[fat[a[i]]] = 1;
    }
  }
  for (int i = 1; i <= n; i++)
    if (cir[i]) dfs(i, 0);
  long long ans = 0;
  while (!cir[s]) swap(S[s], S[fat[s]]), s = fat[s], ans++;
  while (!cir[t]) swap(T[t], T[fat[t]]), t = fat[t], ans++;
  int _t = s;
  pnt = -1;
  while (1) {
    cir[_t] = 0;
    lst[++pnt] = _t;
    int tmp = _t;
    for (int p = head[_t]; p; p = G[p].next)
      if (cir[G[p].v]) {
        _t = G[p].v;
        break;
      }
    if (tmp == _t) break;
  }
  long long ret = 1LL << 60;
  int step = 0;
  int pt = 0;
  pnt++;
  for (int i = 0; i < pnt; i++)
    if (lst[i] == t) pt = i;
  memcpy(bS, S, sizeof(S));
  memcpy(bT, T, sizeof(T));
  for (int i = pt; i; i--) swap(T[lst[i]], T[lst[i - 1]]), step++;
  int pp = 1, qq = 0, gap;
  for (int i = 1; i < pnt; i++)
    if (T[lst[i]] == S[lst[1]]) qq = i;
  gap = qq;
  for (int i = 1; i < pnt; i++) {
    if (S[lst[pp]] != T[lst[qq]]) return printf("-1\n", 0), 0;
    pp = pp == pnt - 1 ? 1 : pp + 1;
    qq = qq == pnt - 1 ? 1 : qq + 1;
  }
  ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
  memcpy(S, bS, sizeof(S));
  memcpy(T, bT, sizeof(T));
  step = 0;
  if (pt)
    for (int i = pt; i < pnt; i++)
      swap(T[lst[i]], T[lst[(i + 1) % pnt]]), step++;
  pp = 1, qq = 0, gap;
  for (int i = 1; i < pnt; i++)
    if (T[lst[i]] == S[lst[1]]) qq = i;
  gap = qq;
  ret = min(ret, step + (long long)pnt * (min(pnt - gap, gap - 1)));
  printf("%d %d %I64d\n", Ansu, Ansv, ans + ret);
}
