// #include <bits/stdc++.h>
#include <vector>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include <random>
#include <queue>
#include <algorithm>
#include <climits>
#include <map>
#include <set>
#include <stack>
#include <sstream>
#include <cstring>
#include <bitset>
#include <deque>

 
using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
 
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
 
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
typedef vector<cd> vcd;
typedef vector<string> vs;
typedef vector<vi> vvi;
typedef vector<vl> vvl;

#define FOR(i,a,b) for (int i=a; i<b; i++)
#define F0R(i,a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = b; i > a; i--)
#define F0Rd(i,a) for (int i = a; i > -1; i--)

#define LOCAL
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
// debug & operator << (debug & dd, P p) { dd << "(" << p.x << ", " << p.y << ")"; return dd; }
 
 
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define pob pop_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define que queue
#define pa pair
#define ex(m,i) m.find(i)!=m.end()
#define nex(m,i) m.find(i)==m.end()


#define uniq(x) x.resize(unique(all(x))-x.begin())

 
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
const int MOD = 1000000007;
const ll INF = 1e18;
const int inf = 1e9;
const int MX = 100001; //check the limits, dummy
const double epsilon = 1e-12;
const int ds[4][2] = {
    {-1,0},
    {1,0},
    {0,-1},
    {0,1}
};

const int dds[8][2] = {
    {-1,0},
    {1,0},
    {0,-1},
    {0,1},
    {-1,1},
    {-1,-1},
    {1,-1},
    {1,1},
};

const int ks[8][2] = {
    {2,1},
    {2,-1},
    {-2,1},
    {-2,-1},
    {1,2},
    {1,-2},
    {-1,2},
    {-1,-2}
};

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    set<char> likes;
    vi likev = {'0','1','2','3','4','5','6','7','8','9'};
    for(auto u : likev){
        likes.insert(u);
    }
    string number; char k; cin >> number >> k;
    for(int i = 0; i < k; i++){
        char c; cin >> c;
        likes.erase(c);
    }

    char firstnumb = *likes.begin();
    char lastnumb = *likes.rbegin();
    
    bool flg = false;
    for(int i = 0; i < sz(number); i++){
        if(nex(likes,number[i])){
            while(number[i]<='9'){
                number[i]++;
                if(ex(likes,number[i])) break;
            }
            if(number[i]!='9'+1){
                // auto it = likes.find(number[i]);
                // --it;
                // number[i] = *it;
                for(int j = i + 1; j < sz(number); j++){
                    debug() << imie(i) imie(j) imie(number[i]) imie(number[j]) imie(lastnumb);
                    
                    number[j] = firstnumb;
                }
                break;
            }
            else{
                while(i>=0){
                    while(number[i]<='9'){
                        debug() << imie(i) imie(number[i]) imie(lastnumb);
                        number[i]++;
                        if(ex(likes,number[i])) break;
                    }
                    if(number[i]!='9'+1) break;
                    --i;
                }

                if(i>=0){
                    // auto it = likes.find(number[i]);
                    // ++it;
                    // number[i] = *it;
                }
                else{
                    char pushnum = firstnumb;
                    if(firstnumb=='0'){
                        auto it = likes.begin();
                        ++it;
                        pushnum = *it;
                    }
                    number.insert(number.begin(),pushnum);
                    ++i;
                }
                for(int j = sz(number) - 1; j > i; j--){
                    number[j] = firstnumb;
                }
                break;
            }
            
        }
    }

    printf("%s\n",number.c_str());

    return 0;
}

Measured memory_bytes: 3200