main = do
  [n,_] <- map read . words <$> getLine :: IO [Int]
  ds <- map read . words <$> getLine :: IO [Int]
  let ls = filter (`notElem` ds) [0..9]
  let len = length $ show n
  let made1 = filter (>=n) $ make ls len 
  let made2 = filter (>=n) $ make ls (len+1)
  print $ if not $ null made1 then minimum made1 else minimum made2
   
make :: [Int] -> Int -> [Int]
make xs 1 = xs
make xs 2 = [a+b | a <- xs, b<-map (*10) xs]
make xs 3 = [a+b+c | a <- xs, b<-map (*10) xs, c<-map (*100) xs]
make xs 4 = [a+b+c+d | a <- xs, b<-map (*10) xs, c<-map (*100) xs, d<-map(*1000) xs]
make _  5 = [10000]