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

// dfmt off
T lread(T = long)(){return readln.chomp.to!T();}
T[] lreads(T = long)(long n){return generate(()=>readln.chomp.to!T()).take(n).array();}
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;}}
alias sread = () => readln.chomp();enum MOD = 10 ^^ 9 + 7;
alias PQueue(T, alias less = "a<b") = BinaryHeap!(Array!T, less);
// dfmt on

void main()
{
    string N, K;
    scan(N, K);
    auto D = aryread();
    uint ng;
    foreach (d; D)
        ng = ng | (1 << d);
    // writefln("%010b", ng);
    // foreach (i; 0 .. 10)
    // {
    //     writeln(!(ng & (1 << i)));
    // }
    auto dp = new long[][](2, N.length + 1);
    dp[0][] = 10 ^^ 9;
    dp[1][] = 10 ^^ 9;
    dp[0][0] = 0;
    foreach (i; 1 .. 10)
        if (!(ng & (1 << i)))
        {
            dp[1][0] = i;
            break;
        }
    foreach (i; 0 .. N.length)
    {
        if (!(ng & (1 << (N[i] - '0'))))
        {
            dp[0][i + 1] = dp[0][i + 1].min(dp[0][i] * 10 + (N[i] - '0'));
        }
        foreach (j; 0 .. 10)
            if (!(ng & (1 << j)))
            {
                dp[1][i + 1] = dp[1][i + 1].min(dp[1][i] * 10 + j);
                if (N[i] - '0' < j)
                    dp[1][i + 1] = dp[1][i + 1].min(dp[0][i] * 10 + j);
            }
    }
    // writeln(dp[0]);
    // writeln(dp[1]);
    writeln(min(dp[0][$ - 1], dp[1][$ - 1]));
}
