#include <cstring>

#include "testlib.h"

const int maxN = 100000;

int n, p[2][maxN], u[2][maxN], root[2];

int main() {
  registerValidation();
  n = inf.readInt(1, maxN);
  inf.readEoln();
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < n; ++j) {
      p[i][j] = inf.readInt(0, n);
      if (j + 1 < n) {
        inf.readSpace();
      }
    }
    inf.readEoln();
  }
  inf.readEof();
  for (int i = 0; i < 2; ++i) {
    memset(u[i], 0, sizeof(u[i]));
    root[i] = -1;
    for (int j = 0; j < n; ++j) {
      if (p[i][j] == 0) {
        ensure(root[i] == -1);
        root[i] = j;
      } else {
        if (u[i][j] > 0) {
          continue;
        }
        int k = j;
        while (k != -1 && u[i][k] == 0) {
          u[i][k] = 1;
          k = p[i][k] - 1;
        }
        if (u[i][k] == 1) {
          ensure(false);
        }
        k = j;
        while (k != -1 && u[i][k] == 1) {
          u[i][k] = 2;
          k = p[i][k] - 1;
        }
      }
    }
  }
  return 0;
}
