import Data.List
import Data.Maybe
import Control.Monad

main = do
  [n,m] <- map read . words <$> getLine :: IO [Int]
  xs <- replicateM n (map read . words <$> getLine) :: IO [[Int]]
  let ps = paths n (xs ++ map reverse xs)
  print $ trav m 1 ps [1..n]

paths :: Int -> [[Int]] -> [(Int,[Int])]
paths 0 xs = []
paths n xs = 
  let 
    dests = last $ transpose $ filter (\[a,_] -> a==n) xs
  in
    (n, dests) : (paths (n-1) xs)

trav :: Int -> Int -> [(Int,[Int])] ->  [Int] -> Int
trav m current paths dests
  | null dests = 1
  | isNothing $ find (==current) dests = 0
  | otherwise = 
    let 
      ds = snd $ head $ filter (\(a,_) -> a==current) paths
    in
      sum $ [x | d<-ds, let x = trav m d paths (delete current dests)]