public void solve() throws IOException {
    int n = nextInt();
    Clerk[] clerks = new Clerk[n];
    for (int x = 0; x < n; x++)
      clerks[x] = new Clerk();

    for (int x = 0; x < n; x++) {
      int clerknum = nextInt() - 1;
      int numchild = nextInt();
      int[] children = new int[numchild];
      for (int y = 0; y < numchild; y++) {
        children[y] = nextInt() - 1;
        clerks[children[y]].setParent(clerknum);
      }
      clerks[clerknum].setChildren(children);
    }

    int pairs = 0;
    boolean done;
    do {
      done = true;
      for (int x = 0; x < n; x++) {
        if (clerks[x] != null && !clerks[x].hasChildren() && clerks[x].parent != -1) {
          pairs++;
          clerks[clerks[x].parent] = null;
          removeParent(clerks, clerks[x].parent);
          clerks[x] = null;
          done = false;
        }
      }
    } while (!done);

    System.out.println(pairs);
  }