{-# LANGUAGE ViewPatterns #-}

import qualified Data.Vector.Unboxed as VU
import Data.ByteString.Char8 as B
import Data.Char
import Data.Maybe
import Control.Applicative
import Control.Monad

m :: Char -> VU.Vector Int -> Int
m c xs = VU.minimum $ VU.filter (>= (digitToInt c)) xs

solve :: B.ByteString -> VU.Vector Int -> Int -> Int -> Int
solve (B.uncons -> Just (x, B.uncons -> Nothing)) use acc n =
    let
        acc' = (acc * 10) + m x use
    in
       if acc' < n then (acc' * 10) + (VU.minimum use) else acc'

solve (B.uncons -> Just (x, B.uncons -> Just (y, xs))) use acc n =
    let
        acc' = (acc * 10) + m x use
    in
        solve (B.cons y xs) use acc' n

main = do
    nk <- VU.unfoldrN 2 (B.readInt . B.dropWhile isSpace) <$> B.getLine :: IO (VU.Vector Int)
    ds <- VU.unfoldrN (VU.last nk) (B.readInt . B.dropWhile isSpace) <$> B.getLine :: IO (VU.Vector Int)
    let use = VU.filter (\i -> VU.notElem i ds) $ VU.fromList [0..9]
    print $ solve (B.pack . show $ VU.head nk) use 0 (VU.head nk)
    