import qualified Data.Vector as V
main=getContents>>=print.(\n ->if elem 2 n then f (V.fromList n) (head n-1) [0] 1 else -1).map read.tail.lines
f n k b r =  if k == 1 then r else if elem k b then -1 else f n (n V.! k -1) (k:b) (r+1)