main = interact $ unlines.map searchAlphametic.lines

searchAlphametic :: String -> String
searchAlphametic = showNum 0.searchNum.separateNumbers

separateNumbers str = [one,two,res]
    where
        (one, next) =  span (/= '+') str
        (two, next2) = span (/= '=') $ tail next
        res        = tail next2

searchNum :: [String] -> [Bool]
searchNum list = map (testNum list) "0123456789"

testNum :: [String] -> Char -> Bool
testNum list n = left == right
    where
        left = read x' + read y'
        right = read z'
        [x',y',z'] = map (swapX n) list

swapX :: Char -> String -> String
swapX _ []     = []
swapX n (s:ss) = if s == 'X' then n:swapX n ss
                             else s:swapX n ss

showNum _ []     = "NA"
showNum n (x:xs) = if x then show n
                        else showNum (n+1) xs

