{-# LANGUAGE ScopedTypeVariables #-} module Main where import Data.List import Control.Monad readLine :: Read a => IO [a] readLine = map read . words <$> getLine check :: Int -> Int -> Bool check s p = elem s $ map ((`mod` 12) . (+p)) [0,2,4,5,7,9,11] calc :: [Int] -> Int -> Bool calc t p | p == -1 = False | null t = True | check (head t) p = calc (tail t) p | otherwise = False main :: IO () main = do getLine t <- readLine let ans = [p | p<-[0..11], calc t p] print $ if length ans == 1 then head ans else -1