#include <bits/stdc++.h>
using namespace std;
void solve(int test_number);
void pre() {
  cout.setf(ios::fixed);
  cout.precision(20);
  cerr.setf(ios::fixed);
  cerr.precision(3);
}
void post() {
  printf("\n");
  fprintf(stderr, "Execution time: %Lf", (long double)clock());
}
const int MAXN = 100100;
int a[222222];
int b[222222];
int num[222222];
int want[222222];
int p[10000000];
int search_substring(string s, string t) {
  int i;
  int r = 0;
  for (i = 0; i < (int)s.size() + (int)t.size() + 1; i++) {
    p[i] = -1;
  }
  string s2;
  s2 = "";
  s2 = t + '$' + s;
  int g = s2.size();
  int j = 0;
  for (i = 1; i <= g - 1; i++) {
    j = p[i - 1];
    while (s2[j + 1] != s2[i] && j >= 0) {
      j = p[j];
    }
    if (s2[j + 1] == s2[i]) {
      j++;
    }
    p[i] = j;
  }
  g = s2.size();
  for (i = 0; i <= g - 1; i++) {
    if (p[i] == t.size() - 1) {
      return true;
    }
  }
  return false;
}
inline void solve(int test_number) {
  int n;
  scanf("%d", &n);
  int i;
  if (n <= 3) {
    puts("YES");
    return;
  }
  string s1, t;
  s1 = "";
  t = "";
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    string s;
    stringstream str;
    str << a[i] << ' ';
    str >> s;
    if (s != "0") s1 += s;
  }
  for (i = 0; i < n; i++) {
    scanf("%d", &b[i]);
    string s;
    stringstream str;
    str << b[i] << ' ';
    str >> s;
    if (s != "0") t += s;
  }
  if (search_substring(s1 + s1, t)) {
    puts("YES");
    return;
  }
  puts("NO");
  return;
}
int main() {
  pre();
  int n = 1;
  for (int i = 0; i < n; i++) {
    solve(i + 1);
  }
  post();
  return 0;
}
