import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Char

main :: IO ()
main = readContents 2 >>= print . solve

solve :: [[Int]] -> Int
solve [[n, k], ds] = head $ filter ff [(max n $ 10 ^ e)..(10 ^ (e + 2))]
    where
        e = truncate . logBase 10 $ fromIntegral n
        ff = all (`notElem` ds) . map (read . (: [])) . show


readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine