import Prelude
import Control.Monad
import Control.Applicative
import Data.Array
import qualified Data.Set as S

type Button = Array Int Int
type Log = S.Set Int

solver :: Button->Log->Int->Int
solver as log now |now==2 = S.size log
                     |now `S.member` log = -1
                     |otherwise = solver as (now `S.insert` log) $as!now

listButton :: [Int]->Button
listButton list = listArray (1, length list) list

main :: IO ()
main = do
	n <- readLn
	l <- replicateM n readLn
        putStrLn $ show $ solver (listButton l) S.empty 1