import Control.Applicative
import Data.List.Split

oneSideLenght :: [Int]
oneSideLenght = [9,3,3,3,3,9]

operation1 :: [(Int,Int)]
operation1 = [(7,22),(8,23),(9,24),(10,12),(13,18)]

operation2 :: [(Int,Int)]
operation2 = [(1,28),(2,29),(3,30),(21,19),(16,15)]

operation3 :: [(Int,Int)]
operation3 = [(1,24),(4,27),(7,30),(10,21),(16,18)]

operation4 :: [(Int,Int)]
operation4 = [(3,22),(6,25),(9,28),(12,19),(13,15)]

{- 一面が全て同じ色かチェックする -}
checkOneSideAns :: Bool -> [Int] -> Bool
checkOneSideAns tf [] = tf
checkOneSideAns tf (x1 : xs)
    | null xs = tf
    | x1 == head xs = checkOneSideAns tf xs
    | otherwise = False

{- 面ごとに同じ色かチェックする -}
checkAllSideAns :: [Int] -> Bool
checkAllSideAns xs = all (checkOneSideAns True) (side xs)
    where
        side = splitPlaces oneSideLenght

{- 値交換 -}
swap i j xs = swap' withIdx
    where
        withIdx = zip [0..] xs
        swap' = map f
            where
                f (idx,x)
                    | idx == i = xs !! j
                    | idx == j = xs !! i
                    | otherwise = x

{-  -}
swapOperation :: [Int] -> [(Int,Int)] -> [Int]
swapOperation xs op = swapOperation' xs op 0
    where
        swapOperation' xs' op' idx
            | length op' <= idx = xs'
            | otherwise = swapOperation' (swapPair (op' !! idx) xs') op' (idx+1)
        swapPair (pair1, pair2) = swap (pair1-1) (pair2-1)

solve :: [Int] -> Int
solve xs = solve' xs 0
    where
        solve' xs' count
            | count > 8 = count
            | checkAllSideAns xs' = count
            | otherwise = minimum [solve' (swapOperation xs' operation1) (count+1),solve' (swapOperation xs' operation2) (count+1),solve' (swapOperation xs' operation3) (count+1),solve' (swapOperation xs' operation4) (count+1)]

spaceToDot = map (read :: String -> Int) . words

main :: IO()
main = do
    ss <- (map (read :: String -> Int) . words) <$> getLine
    print $ solve ss