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

dlist = [s for s in input().split()]

ans = n

while True:

    blnOk = True

    for d in str(ans):

        if d in dlist:

            blnOk = False

    if blnOk:

        print(ans)

        exit()

    else:

        ans += 1

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

dlist = [s for s in input().split()]

ans = n

while True:

    blnOk = True

    for d in str(ans):

        if d in dlist:

            blnOk = False

            break

    if blnOk:

        print(ans)

        exit()

    else:

        ans += 1

n, k = list(map(int, input().split()))
dlist = [s for s in input().split()]
ans = n
while True:
    blnOk = True
    for d in str(ans):
        if d in dlist:
            blnOk = False
    if blnOk:
        print(ans)
        exit()
    else:
        ans += 1

n, k = list(map(int, input().split()))
dlist = [s for s in input().split()]
ans = n
while True:
    blnOk = True
    for d in str(ans):
        if d in dlist:
            blnOk = False
            break
    if blnOk:
        print(ans)
        exit()
    else:
        ans += 1

+            break