#include <fstream>
#include <iostream>
#include <cassert>
#include <cstring>
#include <vector>
#include <algorithm>

using namespace std;

const int maxN = 100000;
const int maxTr = (1 << 18);

int n, p[2][maxN], root[2];
bool u[2][maxN];
vector <int> to[2][maxN];

void addE( vector <int> to[], int a, int b ) {
  to[a].push_back(b);
}

void dfs( const vector <int> to[], bool u[], int v ) {
  u[v] = true;
  for (int i = 0; i < (int)to[v].size(); i++) {
    int w = to[v][i];
    dfs(to, u, w);
  }
}

int main() {
  ifstream fin("virus.in");
  ofstream fout("virus.out");

  assert(fin >> n);
  for (int j = 0; j < 2; j++) {
    root[j] = -1;
    for (int i = 0; i < n; i++) {
      assert(fin >> p[j][i]);
      p[j][i]--;
      if (p[j][i] == -1) {
        assert(root[j] == -1);
        root[j] = i;
      } else {
        addE(to[j], p[j][i], i);
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    memset(u, 0, sizeof(u));
    dfs(to[0], u[0], i);
    dfs(to[1], u[1], i);
    for (int j = 0; j < n; j++) {
      if (u[0][j] && u[1][j]) {
//        cerr << (i + 1) << ", " << (j + 1) << endl;
        ans++;
      }
    }
  }
  fout << (ans - n) << endl;

  return 0;
}
