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

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

while 1:

    s = list(map(int,list(str(n))))

    for i in dls:

        if i in s:

            break

    else:

        print(n)

        exit()

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

dls = list(map(str,input().split()))

while 1:

    s = str(n)

    for i in dls:

        if i in s:

            break

    else:

        print(n)

        exit()

    n += 1
n, k = list(map(int, input().split()))
dls = list(map(int, input().split()))
while 1:
    s = list(map(int, list(str(n))))
    for i in dls:
        if i in s:
            break
    else:
        print(n)
        exit()
    n += 1

n, k = list(map(int, input().split()))
dls = list(map(str, input().split()))
while 1:
    s = str(n)
    for i in dls:
        if i in s:
            break
    else:
        print(n)
        exit()
    n += 1

-dls = list(map(int, input().split())) +dls = list(map(str, input().split())) -    s = list(map(int, list(str(n)))) +    s = str(n)