amount, num = list(map(int, input().split()))



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



while(True):



    check = 0



    for digit in str(amount):



        if digit in hate_num_list:

            check += 1



    if check == 0:

        print(amount)

        break



    else:

        amount += 1
amount, num = list(map(int, input().split()))



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



while True:

    check = 0

    for hd in hated_digit:

        if hd not in str(amount):

            check += 1



        else:

            amount += 1



    if check == num:

        print(amount)

        break
amount, num = list(map(int, input().split()))
hate_num_list = list(map(str, input().split()))
while True:
    check = 0
    for digit in str(amount):
        if digit in hate_num_list:
            check += 1
    if check == 0:
        print(amount)
        break
    else:
        amount += 1

amount, num = list(map(int, input().split()))
hated_digit = list(map(str, input().split()))
while True:
    check = 0
    for hd in hated_digit:
        if hd not in str(amount):
            check += 1
        else:
            amount += 1
    if check == num:
        print(amount)
        break

-hate_num_list = list(map(str, input().split())) +hated_digit = list(map(str, input().split())) -    for digit in str(amount): -        if digit in hate_num_list: +    for hd in hated_digit: +        if hd not in str(amount): -    if check == 0: +        else: +            amount += 1 +    if check == num: -    else: -        amount += 1