import           Control.Monad
import           Data.List

main :: IO ()
main = do
  [m, _] <- map read . words <$> getLine
  d      <- map toInt . words <$> getLine
  print $ minimum $ filter
    (\x -> x >= m && null (findIndices (`elem` show d) (show x)))
    [ 10000 * m1 + 1000 * m2 + 100 * m3 + 10 * m4 + m5
    | m1 <- [0 .. 9]
    , m2 <- [0 .. 9]
    , m3 <- [0 .. 9]
    , m4 <- [0 .. 9]
    , m5 <- [0 .. 9]
    ]
  where toInt s = read s :: Int
