import Data.Char import Data.List main :: IO () main = interact $ show . minimum . map solve . tail . words g :: [Int] -> Int -> Int g xs n = foldl1' (\acc x -> acc * n + x) xs f :: [Char] -> [Int] f [] = [] f (x:xs) | isDigit x = digitToInt x : f xs | otherwise = (ord x) - 55 : f xs solve :: [Char] -> Int solve s = if length xs == 0 then 0 else g (f xs) n where xs = dropWhile (== '0') s maxc = maximum xs n = if isDigit maxc then (digitToInt maxc) + 1 else (ord maxc) - 54