import Control.Monad
import Data.List
import Data.Maybe

main = sol <$> get <*> get >>= print

get = fmap read . words <$> getLine

sol (n:_) ds = l2i $ ps' ++ qs'
  where
  cs = [0..9] \\ ds
  (c0,c1) = (minimum cs,maximum cs)
  (ps,qs) = break (>c1) . reverse $ unfoldr upd n
  upd n = if n==0 then Nothing else Just (n `mod` 10,n `div` 10)
  ps'
    | null qs   = fnd <$> ps
    | null ps   = fnd <$> [max 1 c0] 
    | otherwise = fnd <$> init ps ++ [last ps]
  qs' = replicate (length qs) c0
  fnd m = fromJust $ find (>=m) cs
  l2i = foldl1' (\a b -> a*10+b)