/*
 *
 *  1. sprawdz corner cases
 *  2. sprawdz limity, jesli sie da pozamieniaj wszystkie lokalne zmienne na LL!
 *  3.  MINIMALNE I MAKSYMALNE WEJŚCIE I WYJŚCIE
 *  4. lokalne zmienne, iteracja po tablicach dwuwymiarowych uwazaj
 *  5. SPRAWDZ CZY input jest POSORTOWANY jaki jest niezmiennik
 *
 *
 */
#include <algorithm>
#include <cstdio>
#include <iomanip> // std::setprecision
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <vector>
#define FOR(x, y, z) for (int x = (y); x < (z); x++)
#define FORD(x, y, z) for (int x = (y); x >= z; x--)
#define REP(r, n) for (int r = 0; r < (n); r++)
#define MP make_pair
#define ST first
#define ND second
#define SIZE(c) ((int)((c).size()))
#define FOREACH(i, x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define ALL(u) (u).begin(), (u).end()
#define RALL(u) (u).rbegin(), (u).rend()
#define PB push_back
#define PF push_front
#define SZ(x) long long((x).size())
#define IS_IN(x, t) (t.find(x) != t.end())
#define BIT(n, i) (((n) >> (i)) & 1)

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) \
    {              \
    }
#endif

#define TYPEMAX(type) std::numeric_limits<type>::max()
#define TYPEMIN(type) std::numeric_limits<type>::min()

using namespace std;

typedef long long LL;
typedef long double LD;
typedef vector<LL> VI;
typedef pair<LL, LL> PR;

inline LL GCD(LL a, LL b)
{
    LL r;
    while (b) {
        r = a % b;
        a = b;
        b = r;
    }
    return a;
};
inline LL LCM(LL a, LL b) { return a / GCD(a, b) * b; };
inline LL fpow(LL n, LL k, LL p)
{
    LL r = 1;
    for (; k; k >>= 1) {
        if (k & 1)
            r = r * n % p;
        n = n * n % p;
    }
    return r;
};
inline LL inv(LL a, LL p) { return fpow(a, p - 2, p); }

[[maybe_unused]] const LL MOD = (LL)1e9 + 7;
[[maybe_unused]] const LL MAXUS = TYPEMAX(LL);
[[maybe_unused]] const LD PI = acos((LD)-1);
[[maybe_unused]] const LD epsilon = 1e-9;

template <typename T>
T input()
{
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e)
{
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    LL total = input<LL>();
    LL K = input<LL>();
    set<char> zakazane;
    string total_str = to_string(total);
    REP(i, K)
    {
        char c;
        cin >> c;
        zakazane.insert(c);
    }
    string answer = total_str;
    int toIncIdx = -1;
    for (int i = 0; i < total_str.size(); i++) {
        char cur = total_str[i];
        if (zakazane.count(cur)) {
            answer[i] = '?';
            toIncIdx = i;
            break;
        }
    }
    if (toIncIdx == -1) {
        cout << answer << endl;
        return 0;
    }
    vector<char> letters;
    for (char i = '0'; i <= '9'; i++) {
        if (!zakazane.count(i))
            letters.PB(i);
    }
    while (toIncIdx >= 0) {
        char cur = total_str[toIncIdx];
        char possible = '?';
        for (auto c : letters) {
            if (c > cur) {
                possible = c;
                break;
            }
        }
        if (possible != '?') {
            answer[toIncIdx] = possible;
            break;
        }
        toIncIdx--;
    }
    if (toIncIdx == -1) {
        char smallestNotZero = letters[0] != '0' ? letters[0] : letters[1];
        answer = smallestNotZero + answer;
        toIncIdx = 0;
    }
    for (int i = toIncIdx + 1; i < answer.size(); i++) {
        answer[i] = letters[0];
    }
    cout << answer << endl;
    return 0;
}


Measured memory_bytes: 256