Message Relay

No attempts yetTime limit1sMemory limit128 MB

Problem

Farmer John's $N$ cows ($1 \le N \le 1000$) are conveniently numbered from $1$ to $N$. Using an old-fashioned communication mechanism based on tin cans and strings, the cows have figured out how to send messages to one another without Farmer John noticing.

Each cow can forward messages to at most one other cow: for cow $i$, the value $F(i)$ is the index of the cow to which cow $i$ forwards any message she receives (this number is always different from $i$). If $F(i)$ is $0$, then cow $i$ does not forward messages.

A message that starts at some cow may ultimately get stuck in a loop, forwarded around a cycle forever. A cow is said to be "loopy" if a message sent from that cow eventually gets stuck in such a loop. The cows want to avoid sending messages from loopy cows. Count how many of Farmer John's cows are not loopy.

Input

  • Line 1: the number of cows, $N$.
  • Lines 2 to $N+1$: line $i+1$ contains the value of $F(i)$.

Output

  • Line 1: the total number of non-loopy cows.

Hint

Consider a case with 5 cows. Cow 1 is not loopy because she does not forward messages. Cow 3 is also not loopy because she forwards to cow 1, who then forwards nothing further. The remaining cows are all loopy because their messages get stuck in the cycle $4 \to 5 \to 4 \to 5 \to \cdots$. Hence there are 2 non-loopy cows.