import Data.Char
import Data.List
import qualified Data.ByteString.Char8 as B
import qualified Data.Vector.Unboxed as V
main=print.solve.V.unfoldr(B.readInt.B.dropWhile isSpace)=<<B.getContents
solve as=f 0 1 where
  n=V.head as
  f k i
    |k>n= -1
    |i==2=k
    |otherwise=f(k+1)(as V.! i)