n,k=list(map(int,input().split()))

d=input().split()

while 1:

    t=0

    p=list(str(n))

    q=len(p)

    for c in range(q):

        if t==1:

            break

        for b in range(k):

            if p[c]==d[b]:

                n=n+1

                t=1

                break

    if t==0:

        break

print(n)
n,k=list(map(int,input().split()))

d=input().split()

while 1:

    t=0

    p=list(str(n))

    q=len(p)

    for c in range(q):

        if t==1:

            break

        for b in range(k):

            if p[c]==d[b]:

                r=10**(q-c-1)

                n=int(n/r+1)*r

                t=1

                break

    if t==0:

        break

print(n)
n, k = list(map(int, input().split()))
d = input().split()
while 1:
    t = 0
    p = list(str(n))
    q = len(p)
    for c in range(q):
        if t == 1:
            break
        for b in range(k):
            if p[c] == d[b]:
                n = n + 1
                t = 1
                break
    if t == 0:
        break
print(n)

n, k = list(map(int, input().split()))
d = input().split()
while 1:
    t = 0
    p = list(str(n))
    q = len(p)
    for c in range(q):
        if t == 1:
            break
        for b in range(k):
            if p[c] == d[b]:
                r = 10 ** (q - c - 1)
                n = int(n / r + 1) * r
                t = 1
                break
    if t == 0:
        break
print(n)

-                n = n + 1 +                r = 10 ** (q - c - 1) +                n = int(n / r + 1) * r