a,b = list(map(int,input().split(" ")))

ar = list(map(int,input().split(" ")))

while True:

    count = 0

    br = list(str(a))

    for r in br:

        if int(r) in ar:

            count += 1

    if count == 0:

        print(a)

        break

    else:

        a += 1
a,b = list(map(int,input().split(" ")))

ar = input().split(" ")

while True:

    count = 0

    for r in ar:

        if r in str(a):

            count += 1

            break

    if count == 0:

        print(a)

        break

    a += 1
a, b = list(map(int, input().split(" ")))
ar = list(map(int, input().split(" ")))
while True:
    count = 0
    br = list(str(a))
    for r in br:
        if int(r) in ar:
            count += 1
    if count == 0:
        print(a)
        break
    else:
        a += 1

a, b = list(map(int, input().split(" ")))
ar = input().split(" ")
while True:
    count = 0
    for r in ar:
        if r in str(a):
            count += 1
            break
    if count == 0:
        print(a)
        break
    a += 1

-ar = list(map(int, input().split(" "))) +ar = input().split(" ") -    br = list(str(a)) -    for r in br: -        if int(r) in ar: +    for r in ar: +        if r in str(a): +            break -    else: -        a += 1 +    a += 1