import Data.List
import Data.Char

main = do
    [n,_] <- map read . words <$> getLine :: IO [Int]
    ds <- map read . words <$> getLine :: IO [Int]
    let ds' = [0..9] \\ ds
        n' = map digitToInt $ show n
        rs = map snd $ scanl' (\(c,_) d -> maybe (1,head ds') (\c'->(0,c')) $ find ((d+c)<=) ds') (0,0) n'
        r = read . map intToDigit $ rs :: Int
    print r
