import core.bitop;
import std.algorithm;
import std.ascii;
import std.bigint;
import std.conv;
import std.functional;
import std.format;
import std.math;
import std.numeric;
import std.range;
import std.stdio;
import std.string;
import std.random;
import std.typecons;

alias sread = () => readln.chomp();
alias Point2 = Tuple!(long, "y", long, "x");

T lread(T = long)()
{
    return readln.chomp.to!T();
}

T[] aryread(T = long)()
{
    return readln.split.to!(T[])();
}

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

enum MOD = (10 ^^ 9) + 7;

void main()
{
    string Sn;
    long K;
    scan(Sn, K);
    long N = Sn.to!long();
    auto D = aryread();
    long[] iD;
    {
        bool[10] B;
        B[] = true;
        foreach (d; D)
            B[d] = false;
        foreach (i; 0 .. 10)
            if (B[i])
                iD ~= i;
    }
    long len = Sn.length;
    if ((cast(char)(iD.back + '0')).repeat().take(len).array().to!long() < N)
    {
        len++;
    }
    long ans = long.max;
    long p = iD[0] == 0;
    foreach (d; iD[p .. $])
        ans = ans.min(solve(N, iD, len, [d]));
    writeln(ans);
}

long solve(long N, long[] iD, long len, long[] tmp)
{
    if (tmp.length == len)
    {
        long x = tmp.map!(x => cast(char)(x + '0')).array().to!long();
        return N <= x ? x : long.max;
    }
    long ans = long.max;
    foreach (d; iD)
        ans = ans.min(solve(N, iD, len, tmp ~ d));
    return ans;
}
