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

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

while 1:

    for c in str(n):

        if int(c) in d:

            break

    else:

        print(n)

        exit()

    n += 1

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

while 1:

    for c in str(n):

        if c in d:break

    else:

        print(n)

        exit()

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

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

-d = list(map(int, input().split())) +d = input().split() -        if int(c) in d: +        if c in d: