import qualified Data.ByteString.Char8 as B
import Data.Maybe
main=print.solve 0 0.map(fst.fromJust.B.readInt).B.words=<<B.getContents
solve i c xs@(n:ns)
 |a==1=c+1
 |c>n= -1
 |otherwise=solve a(c+1)xs
 where a=ns!!i-1