#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
const int oo = 0x3f3f3f3f;
const int maxn = 200000;
struct edge {
  int id, nxt;
  edge() {}
  edge(int _id, int _nxt) : id(_id), nxt(_nxt) {}
};
edge e[(maxn << 1) + 5];
int st[maxn + 5], en = 0;
inline void add_edge(int first, int second) {
  e[en] = edge(second, st[first]), st[first] = en++;
}
int n;
int dep[maxn + 5], fa[maxn + 5];
int dfs_tot = 0;
int dfn[maxn + 5], End[maxn + 5];
void dfs(int first, int f = -1) {
  if (~f)
    dep[first] = dep[f] + 1;
  else
    dep[first] = 0;
  dfn[first] = dfs_tot++;
  fa[first] = f;
  for (int i = st[first]; i != -1; i = e[i].nxt) {
    int second = e[i].id;
    if (second == f) continue;
    dfs(second, first);
  }
  End[first] = dfs_tot;
}
int pa, push_back;
int a[maxn + 5], b[maxn + 5];
int main() {
  memset(st, -1, sizeof st), en = 0;
  scanf("%d", &n);
  for (int i = (0), _end_ = (n); i < _end_; ++i) scanf("%d", a + i);
  pa = find(a, a + n, 0) - a;
  for (int i = (0), _end_ = (n); i < _end_; ++i) scanf("%d", b + i);
  push_back = find(b, b + n, 0) - b;
  for (int i = (0), _end_ = (n - 1); i < _end_; ++i) {
    int first, second;
    scanf("%d%d", &first, &second), --first, --second;
    add_edge(first, second), add_edge(second, first);
  }
  dfs(push_back);
  int u = pa;
  while (u != push_back) {
    a[u] = a[fa[u]];
    u = fa[u];
  }
  a[u] = 0;
  int first = -1, rt = -1;
  int cnt = 0;
  for (int i = (0), _end_ = (n); i < _end_; ++i)
    if (a[i] != b[i]) {
      ++cnt;
      if (!~rt || dep[i] < dep[rt]) rt = i;
      if (!~first || dep[i] > dep[first]) first = i;
    }
  if (!cnt) printf("0 %d\n", dep[pa]), exit(0);
  rt = fa[rt];
  int second = rt;
  for (int i = (0), _end_ = (n); i < _end_; ++i)
    if (a[i] != b[i] && (End[i] <= dfn[first] || dfn[i] > dfn[first]) &&
        dep[i] > dep[second])
      second = i;
  if (dep[first] + dep[second] - (dep[rt] << 1) != cnt) printf("-1\n"), exit(0);
  vector<int> all;
  int id = first;
  while (id != rt) {
    all.push_back(id);
    id = fa[id];
  }
  reverse((all).begin(), (all).end());
  id = second;
  while (id != rt) {
    all.push_back(id);
    id = fa[id];
  }
  int go = -1;
  for (int i = (0), _end_ = ((int((all).size()))); i < _end_; ++i)
    if (a[all[0]] == b[all[i]]) {
      go = i;
      break;
    }
  if (!~go) printf("-1\n"), exit(0);
  for (int i = (0), _end_ = ((int((all).size()))); i < _end_; ++i)
    if (a[all[i]] != b[all[(i + go) % (int((all).size()))]])
      printf("-1\n"), exit(0);
  long long ans = dep[pa];
  static bool vis[maxn + 5];
  memset(vis, 0, sizeof vis);
  id = pa;
  while (~id) {
    vis[id] = 1;
    id = fa[id];
  }
  if (vis[all[0]] || vis[all.back()]) {
    if (vis[all.back()])
      reverse((all).begin(), (all).end()), go = (int((all).size())) - go;
    int tmp = (int((all).size()));
    for (int i = (1), _end_ = ((int((all).size()))); i < _end_; ++i)
      if (!vis[all[i]]) {
        tmp = i;
        break;
      }
    int cyc_len = (int((all).size())) + 1;
    ans -= tmp;
    ans += min(abs(tmp + (long long)go * cyc_len),
               abs(tmp + (long long)(go - (int((all).size()))) * cyc_len));
  } else
    ans += min(go, (int((all).size())) - go) * (cnt + 1);
  id = rt;
  while (!vis[id]) {
    ans += 2;
    id = fa[id];
  }
  if (first > second) swap(first, second);
  printf("%d %d %I64d\n", first + 1, second + 1, ans);
  return 0;
}
