#include <iostream>
#include <cstdio>
#include <cstring>
//#define fre
//#define DEBUG
using namespace std;
int excl[10];
struct Node
{
    char num[10];
    int len;
    void incre()
    {
        num[len - 1]++;
        int i = len - 1;
        while (num[i] == '9' + 1) 
        {
            num[i] = '0', num[--i]++;
        }
    }
    bool judge()
    {
        if (num[0] != '0' && excl[num[0] - '0']) return false;
        for (int i = 1; i < len; ++i) 
            if (excl[num[i] - '0']) return false;
        return true; 
    }
} tar, cur;
int main()
{
    int k, t;
    tar.num[0] = '0';
    scanf("%s", tar.num + 1);
    tar.len = strlen(tar.num);
    memcpy(cur.num, tar.num, sizeof(tar.num));
    cur.len = tar.len;
    scanf("%d", &k);
    memset(excl, 0, sizeof(excl));
    for (int i = 0; i < k; ++i) scanf("%d", &t), excl[t] = 1;
    while (1)
    {
        if (cur.judge() && strcmp(cur.num, tar.num) >= 0)
        {
            if (cur.num[0] != '0') printf("%s\n", cur.num);
            else printf("%s\n", cur.num + 1);
            break;
        }
        cur.incre();
    }
}