import itertools

N,K = list(map(int,input().split()))

D = list(map(int,input().split()))

A = []

for i in range(10):

    if i not in D:

        A.append(i)

cmin = 100000

k = len(str(N))

for i in range(k,6):

    for x in itertools.product(A,repeat=i):

        cnt = 0

        for j in range(i):

            cnt += x[j]*10**(i-1-j)

        if cnt>=N:

            cmin = min(cmin,cnt)

print(cmin)
N,K = list(map(int,input().split()))

D = list(map(int,input().split()))

E = []

for i in range(10):

    if i not in D:

        E.append(i)

E = sorted(E)

N = str(N)

ind = len(N)

for i in range(len(N)):

    if int(N[i]) not in E:

        ind = i

        break

if ind==len(N):

    print(N)

else:

    flag = 0

    x = ""

    for i in range(ind,-1,-1):

        n = int(N[i])

        for e in E:

            if e>n:

                x = N[:i]+str(e)+str(E[0])*(len(N)-i-1)

                flag = 1

                break

        if flag==1:break

    if flag==0:

        if E[0]>0:

            a = E[0]

        else:

            a = E[1]

        x = str(a)+str(E[0])*len(N)

    print(x)
import itertools

N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
A = []
for i in range(10):
    if i not in D:
        A.append(i)
cmin = 100000
k = len(str(N))
for i in range(k, 6):
    for x in itertools.product(A, repeat=i):
        cnt = 0
        for j in range(i):
            cnt += x[j] * 10 ** (i - 1 - j)
        if cnt >= N:
            cmin = min(cmin, cnt)
print(cmin)

N, K = list(map(int, input().split()))
D = list(map(int, input().split()))
E = []
for i in range(10):
    if i not in D:
        E.append(i)
E = sorted(E)
N = str(N)
ind = len(N)
for i in range(len(N)):
    if int(N[i]) not in E:
        ind = i
        break
if ind == len(N):
    print(N)
else:
    flag = 0
    x = ""
    for i in range(ind, -1, -1):
        n = int(N[i])
        for e in E:
            if e > n:
                x = N[:i] + str(e) + str(E[0]) * (len(N) - i - 1)
                flag = 1
                break
        if flag == 1:
            break
    if flag == 0:
        if E[0] > 0:
            a = E[0]
        else:
            a = E[1]
        x = str(a) + str(E[0]) * len(N)
    print(x)

-import itertools - -A = [] +E = [] -        A.append(i) -cmin = 100000 -k = len(str(N)) -for i in range(k, 6): -    for x in itertools.product(A, repeat=i): -        cnt = 0 -        for j in range(i): -            cnt += x[j] * 10 ** (i - 1 - j) -        if cnt >= N: -            cmin = min(cmin, cnt) -print(cmin) +        E.append(i) +E = sorted(E) +N = str(N) +ind = len(N) +for i in range(len(N)): +    if int(N[i]) not in E: +        ind = i +        break +if ind == len(N): +    print(N) +else: +    flag = 0 +    x = "" +    for i in range(ind, -1, -1): +        n = int(N[i]) +        for e in E: +            if e > n: +                x = N[:i] + str(e) + str(E[0]) * (len(N) - i - 1) +                flag = 1 +                break +        if flag == 1: +            break +    if flag == 0: +        if E[0] > 0: +            a = E[0] +        else: +            a = E[1] +        x = str(a) + str(E[0]) * len(N) +    print(x)