/******************************************************************/
/* ACM ICPC 2017-2018, NEERC                                      */
/* Northern Subregional Contest                                   */
/* St Petersburg, November 4, 2017                                */
/******************************************************************/
/* Problem H. Hidden Supervisors                                  */
/*                                                                */
/* Original idea         Niyaz Nigmatullin                        */
/* Problem statement     Niyaz Nigmatullin                        */
/* Test set              Niyaz Nigmatullin                        */
/******************************************************************/
/* Checker                                                        */
/*                                                                */
/* Author                Niyaz Nigmatullin                        */
/******************************************************************/

#include "testlib.h"

int readAns(InStream &ouf, std::vector<int> const &ip) {
  int n = (int) ip.size();
  int expected = ouf.readInt();
  std::vector<int> deg(n);
  std::vector<int> p(n);
  for (int i = 1; i < n; i++) {
    p[i] = ouf.readInt(1, n, format("p[%d]", i + 1)) - 1;
    if (ip[i] >= 0 && p[i] != ip[i]) {
      ouf.quitf(_wa, "[n = %d] p[%d] has to be equal to %d, but %d found", n, i + 1, ip[i] + 1, p[i] + 1);
    }
    deg[p[i]]++;
  }
  std::vector<int> q(n);
  int head = 0;
  int tail = 0;
  for (int i = 0; i < n; i++) {
    if (deg[i] == 0) {
      q[tail++] = i;
    }
  }
  std::vector<int> is_free(n, 1);
  int answer = 0;
  while (head < tail) {
    int v = q[head++];
    if (v != 0) {
      --deg[p[v]];
      if (deg[p[v]] == 0) {
        q[tail++] = p[v];
      }
      if (is_free[v] && is_free[p[v]]) {
        is_free[v] = false;
        is_free[p[v]] = false;
        ++answer;
      }
    }
  }
  if (head != n) {
    ouf.quitf(_wa, "[n = %d] Participant's list is not a tree", n);
  }
  if (answer != expected) {
    ouf.quitf(_wa, "[n = %d] Promised %d pairs, but maximum matching size is %d in the tree", n, expected, answer);
  }
  return answer;
}


int main(int argc, char **argv) {
  registerTestlibCmd(argc, argv);
  int n = inf.readInt();
  std::vector<int> p(n);
  for (int i = 1; i < n; i++) {
    p[i] = inf.readInt() - 1;
  }
  int ansJ = readAns(ans, p);
  int ansP = readAns(ouf, p);
  quitif(ansJ > ansP, _wa, "[n = %d] Found %d, but judges can make %d", n, ansP, ansJ);
  quitif(ansJ < ansP, _fail, "[n = %d] Participant found better: %d > %d", n, ansP, ansJ);
  quitf(_ok, "[n = %d] ans = %d", n, ansJ);
}

