import qualified Data.Char as C import qualified Data.List as L main :: IO () main = do n <- getLine let ns = L.permutations . L.nub $ n result = if length ns == 1 then read n else proc (read n) $ convert ns print result proc :: Integer -> [(Integer, Integer)] -> Integer proc n ns = foldr gcd n ns'' where ns' = L.nub . filter (\(x, y) -> x > y) $ ns ns'' = map (\(a, b) -> 9 * (a - b)) ns' convert :: [String] -> [(Integer, Integer)] convert = map (\(x:y:zs) -> (toInteger $ C.digitToInt x, toInteger $ C.digitToInt y))