{-# OPTIONS_GHC -O2 #-}
import Control.Applicative
import Data.List
import qualified Data.ByteString.Char8 as BC
import qualified Data.ByteString as BS
import Data.Maybe (fromJust)
import qualified Data.Map as M

type Edge = (Int, Int)

main :: IO()
main = BC.getContents >>= print . f . map ((\[x,y] -> (x,y)) . map (fst . fromJust . BC.readInt) . BC.words) . BC.lines

f :: [(Int,Int)] -> Int
f ((n,m):xs)
    | null $ filter ((== t).snd) es = -1
    | otherwise = bfs es t [[s]]
    where es    = take m xs
          (s,t) = last xs

bfs :: [Edge] -> Int -> [[Int]] -> Int
bfs _ _ [[]] = -1
bfs es t (pa@(l:p):ps)
    | lp `mod` 3 == 0 && l == t = lp `div` 3
    | lp `mod` 3 == 0 && elemtest l p = -1
    | otherwise =
        if null ns
        then bfs es t ps
        else bfs es t $ ps ++ map (:pa) ns
    where ns = map snd $ filter ((== l).fst) es
          lp = length p

elemtest e l = maybe False ((== 0).(`mod` 3).(+1)) $ elemIndex e l