import Data.Set( Set, null, fromList, deleteAt, lookupIndex, minView)
import qualified Data.Set as S
import Data.Maybe( fromJust)
main = do
    n <- readLn
    as <- return . map (read::String->Int) . words =<< getLine
    let ps = zip [1..n] as
	pairs = fromList ps
	process::Set (Int,Int) -> Int -> Maybe Int
	process set n
	  | S.null set = Just n
	  | otherwise = do
	    ((a,b), remain) <- minView set
	    case lookupIndex (b,a) remain of
		Nothing -> process remain n
		Just ix -> process (deleteAt ix remain) (n+1)
    print . fromJust $ process pairs 0