import std.stdio,std.array,std.conv,std.string,std.algorithm;

void main(){
    string[] input = split(readln());
    string nStr = input[0];
    int k = to!int(input[1]);
    string[] d = split(readln());
    int[] nArray = new int[nStr.length];
    for(int i=0;i<nStr.length;i++) nArray[i] = nStr[i] - 48;

    int cou = 0;
    abcd(nArray.length,nArray,d,k);
    while(true){
        cou = 0;
        for(int i=0;i<nArray.length;i++){
            for(int h=0;h<nArray.length;h++){
                for(int j=0;j<k;j++){
                    if(nArray[h] == to!int(d[j])){
                        cou++;
                    }
                }
            }
        }
        if(cou == 0) break;
        abcd(nArray.length,nArray,d,k);
    }
    for(int i=0;i<nArray.length;i++) write(nArray[i]);
}

int[] carry(int[] nArray,ulong nStrlength){
    int sum = 0;
    int car = 1;
    int digit = 0;
    for(int i=to!int(nStrlength);i>=1;i--){
        sum += nArray[i-1] * car;
        car *= 10; 
    }
    int tmp = sum;
    while(tmp != 0){
        tmp /= 10;
        digit++;
    }

    nArray.length = digit;

    for(int i=0;i<digit;i++) nArray[i] = (to!string(sum)[i]) - 48;
    return nArray;
}

int[] abcd(ulong nStrlength,int[] nArray,string[] d,int k){
    for(int i=0;i<nStrlength;i++){
        for(int j=0;j<k;j++){
            if(nArray[i] == to!int(d[j])){
                while(true){
                    nArray[i]++;
                    nArray = carry(nArray,nArray.length);
                    for(int h=0;h<k;h++){
                        if(to!int(d[h]) == nArray[i]){
                            break;
                        }
                    }
                    break; 
                }
            }
        }
    }
    return nArray;
}
