import std;
import core.bitop;

ulong MAX = 100_100, MOD = 1_000_000_007, INF = 1_000_000_000_000;
alias sread = () => readln.chomp();
alias lread(T = long) = () => readln.chomp.to!(T);
alias aryread(T = long) = () => readln.split.to!(T[]);
alias Pair = Tuple!(long, "x", long, "c");
alias PQueue(T, alias less = "a>b") = BinaryHeap!(Array!T, less);

void main()
{
    string n, k;
    scan(n, k);
    auto d = aryread();
    auto useSet = iota(10).filter!(x => !d.includes(x)).array();
    auto numSet = new long[](0);

    void dfs(long t)
    {
        if (t >= n.to!(long))
            return;

        foreach (e; useSet)
        {
            if (t == 0 && e == 0)
                continue;

            numSet ~= t * 10 + e;
            dfs(t * 10 + e);
        }

        return;
    }

    dfs(0);
    numSet.sort();
    // numSet.writeln();
    long ok = numSet.length + 1, ng = -1;
    while (abs(ok - ng) > 1)
    {
        auto mid = (ok + ng) / 2;
        if (numSet[mid] >= n.to!long)
            ok = mid;
        else
            ng = mid;
    }

    numSet[ok].writeln();
}

bool includes(T)(T[] ary, T value)
{
    return ary.filter!(x => x == value).array.length > 0;
}

void scan(TList...)(ref TList Args)
{
    auto line = readln.split();
    foreach (i, T; TList)
    {
        T val = line[i].to!(T);
        Args[i] = val;
    }
}
