import Data.List anyKadomatsu as = any (== (head as)) (tail as) || any (== (last as)) (init as) isKadomatsu (x:y:z:zs) = ( (x > y) && (z > y) ) || ( (x < y) && (z < y) ) solve as | anyKadomatsu as = 0 -- おなじ長さの竹があるので門松でない | isKadomatsu as = -1 -- すでに門松なので、無限門松 | otherwise = mottoKadomatsu ((last . sort) as) as -- 有限門松 where mottoKadomatsu 0 _ = 0 mottoKadomatsu p as = x + mottoKadomatsu (p - 1) as where bs = map (`mod` p) as x | not (anyKadomatsu bs) && isKadomatsu bs = 1 | otherwise = 0 answer a | a == -1 = "INF" | otherwise = show a main = do as <- map (read :: String -> Int) . words <$> getLine putStrLn $ answer $ solve as