import std.stdio, std.string, std.conv;
import std.range, std.algorithm, std.array, std.typecons, std.container;
import std.math, std.numeric, core.bitop;

enum inf = 1<<30;

void main() {
    int n, k;
    scan(n, k);
    auto d = readln.split.to!(int[]);
    auto ng = new bool[](10);
    foreach (di ; d) {
        ng[di] = true;
    }

    auto ns = n.to!string;
    auto dp = new int[][](ns.length + 1, 2);
    fillAll(dp, inf);
    dp[0][0] = 0;
    foreach (dig ; 1 .. 10) {
        if (ng[dig]) continue;
        dp[0][1] = min(dp[0][1], dig);
    }

    foreach (i ; 0 .. ns.length) {
        foreach (j ; 0 .. 2) {
            foreach (dig ; (j ? 0 : ns[i] - '0') .. 10) {
                if (ng[dig]) continue;
                if (dp[i][j] == inf) continue;
                dp[i+1][j | (dig > ns[i] - '0')] = min(dp[i+1][j | (dig > ns[i] - '0')], 10*dp[i][j] + dig);
            }
        }
    }

    debug {
        writefln("%(%s\n%)", dp);
    }

    int ans = inf;

    if (dp[ns.length][0] < inf) ans = min(dp[ns.length][0], ans);
    if (dp[ns.length][1] < inf) ans = min(dp[ns.length][1], ans);

    writeln(ans);
}
















































void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}



void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}
