import Data.Char import Data.List main = getContents >>= print . dmin . tail . words dec c = if isDigit c then digitToInt c else ord c - 55 dmin = minimum . map toDecimal toDecimal d = sum $ zipWith (*) (iterate (*r) 1) (reverse (map dec d)) where r = dec (head (reverse (sort d))) + 1