import Data.List
main=getContents>>=print.(\n -> if c n 2 == -1 then -1 else f n (c n 2) [2] 1).map read.tail.lines
c n s= case elemIndex s n of
 Nothing -> -1
 Just k -> k
f n k b r = if k == 0 then r else if elem (k+1) b then -1 else f n (c n (k+1)) ((k+1):b) (r+1)