def main():

    N, K = list(map(int, input().split()))

    D = set(map(int, input().split()))

    numset = set(range(0,10))

    d = numset.difference(D)



    for n in range(N, pow(10, 6) + 10):

        check = set(list(str(n)))

        ngflag = False

        for i in check:

            if int(i) in D:

                ngflag = True

                break

        if not ngflag:

            break

    print(n)



if __name__ == "__main__":

    main()

def minN(N:int, usable:list, restrict=True):

    usable.sort()

    keta = False

    if restrict:

        for i in usable:

            if i >= N:

                return str(i)

        # 桁が増える

        return '1'+str(usable[0])

    else:

        return str(usable[0])



def rote(N:list, D:set, d:set):

    ans = []

    flag = True

    lenNstr = len(N)

    for i, n in enumerate(N):

        n = int(n)

        keta = 10**(len(N) - i - 1)

        if flag:

            if n in D:

                ans.append(int(minN(n, d)) * keta)

                flag = False

            else:

                ans.append(keta * n)

        else:

            ans.append(keta * min(d))

    return sum(ans)



def main():

    N, K = list(map(int, input().split()))

    D = set(map(int, input().split()))

    numset = set(range(0,10))

    d = list(numset.difference(D))

    d.sort()

    for _ in range(10):

        Nstr = list(str(N))

        ans = rote(Nstr, D, d)

        N = ans    

    print(ans)



if __name__ == "__main__":

    main()

def main():
    N, K = list(map(int, input().split()))
    D = set(map(int, input().split()))
    numset = set(range(0, 10))
    d = numset.difference(D)
    for n in range(N, pow(10, 6) + 10):
        check = set(list(str(n)))
        ngflag = False
        for i in check:
            if int(i) in D:
                ngflag = True
                break
        if not ngflag:
            break
    print(n)


if __name__ == "__main__":
    main()

def minN(N: int, usable: list, restrict=True):
    usable.sort()
    keta = False
    if restrict:
        for i in usable:
            if i >= N:
                return str(i)
        # 桁が増える
        return "1" + str(usable[0])
    else:
        return str(usable[0])


def rote(N: list, D: set, d: set):
    ans = []
    flag = True
    lenNstr = len(N)
    for i, n in enumerate(N):
        n = int(n)
        keta = 10 ** (len(N) - i - 1)
        if flag:
            if n in D:
                ans.append(int(minN(n, d)) * keta)
                flag = False
            else:
                ans.append(keta * n)
        else:
            ans.append(keta * min(d))
    return sum(ans)


def main():
    N, K = list(map(int, input().split()))
    D = set(map(int, input().split()))
    numset = set(range(0, 10))
    d = list(numset.difference(D))
    d.sort()
    for _ in range(10):
        Nstr = list(str(N))
        ans = rote(Nstr, D, d)
        N = ans
    print(ans)


if __name__ == "__main__":
    main()

+def minN(N: int, usable: list, restrict=True): +    usable.sort() +    keta = False +    if restrict: +        for i in usable: +            if i >= N: +                return str(i) +        # 桁が増える +        return "1" + str(usable[0]) +    else: +        return str(usable[0]) + + +def rote(N: list, D: set, d: set): +    ans = [] +    flag = True +    lenNstr = len(N) +    for i, n in enumerate(N): +        n = int(n) +        keta = 10 ** (len(N) - i - 1) +        if flag: +            if n in D: +                ans.append(int(minN(n, d)) * keta) +                flag = False +            else: +                ans.append(keta * n) +        else: +            ans.append(keta * min(d)) +    return sum(ans) + + -    d = numset.difference(D) -    for n in range(N, pow(10, 6) + 10): -        check = set(list(str(n))) -        ngflag = False -        for i in check: -            if int(i) in D: -                ngflag = True -                break -        if not ngflag: -            break -    print(n) +    d = list(numset.difference(D)) +    d.sort() +    for _ in range(10): +        Nstr = list(str(N)) +        ans = rote(Nstr, D, d) +        N = ans +    print(ans)