import Control.Applicative
import Control.Monad

h 0 = 0
h n = 3 * h (n - 1) + 1

fa :: [Int] -> [Int] -> [Int] -> Int -> Int
fa [] [] [] 0 = 0
fa (a:as) b c n
  | a == n = fa as b c (n - 1)
fa a (b:bs) c n
  | b == n = fc a bs c (n - 1) + 1 + 2 * h (n - 1)
fa a b (c:cs) n
  | c == n = fa a b cs (n - 1) + 1 + h (n - 1) + h n

fb :: [Int] -> [Int] -> [Int] -> Int -> Int
fb [] [] [] 0 = 0
fb (a:as) b c n
  | a == n = fc as b c (n - 1) + 1 + h (n - 1)
fb a (b:bs) c n
  | b == n = fb a bs c (n - 1)
fb a b (c:cs) n
  | c == n = fa a b cs (n - 1) + 1 + h (n - 1)

fc :: [Int] -> [Int] -> [Int] -> Int -> Int
fc a b c n = fa c b a n

f :: [Int] -> [Int] -> [Int] -> Int -> Int
f a b c n = min stepA stepC
  where stepA = fa a b c n
        stepC = fc a b c n

g :: [Int] -> [Int] -> [Int] -> Int -> Int
g a0 b0 c0 n = f a b c n
  where a = map (\x -> n + 1 - x) a0
        b = map (\x -> n + 1 - x) b0
        c = map (\x -> n + 1 - x) c0

main = do
  [n, m] <- map read . words <$> getLine :: IO [Int]
  unless ([n, m] == [0, 0]) $ do
    _:a <- map read . words <$> getLine :: IO [Int]
    _:b <- map read . words <$> getLine :: IO [Int]
    _:c <- map read . words <$> getLine :: IO [Int]
    let step = g a b c n
    let answer = if step <= m then step else -1
    print answer
    main
