Problem p04045 (python)

Fixed submission:
n, k = map( int, input().split())
hate = set( input().split())

usable = sorted( list( {str(i) for i in range(10)} - hate))

l = len(str(n))

def check(l):
    num = []
    if l == 1:
        for i in usable:
            num.append(int(i))
    elif l == 2:
        for ii in usable:
            for i in usable:
                num.append(int( ii + i))
    elif l == 3:
        for iii in usable:
            for ii in usable:
                for i in usable:
                    num.append(int( iii + ii + i))
    elif l == 4:
        for iv in usable:
            for iii in usable:
                for ii in usable:
                    for i in usable:
                        num.append(int( iv + iii + ii + i))
    else:
        for v in usable:
            for iv in usable:
                for iii in usable:
                    for ii in usable:
                        for i in usable:
                            num.append(int( str(v) + str(iv) + str(iii) + str(ii) + str(i)))
    return num

truf = False
num = check(l)
for i in num:
    if n <= i:
        print(i)
        truf = True
        break
if not truf:
    num = check(l+1)
    for i in num:
        if n <= i:
            print(i)
            break

Buggy submission (verdict: Runtime Error):
n, k = map( int, input().split())
hate = set( input().split())

usable = sorted( list( {str(i) for i in range(10)} - hate))

l = len(str(n))

def check(l):
    num = []
    if l == 1:
        for i in usable:
            num.append(i)
    if l == 2:
        for ii in usable:
            for i in usable:
                num.append(int( str(ii) + str(i)))
    elif l == 3:
        for iii in usable:
            for ii in usable:
                for i in usable:
                    num.append(int( str(iii) + str(ii) + str(i)))
    elif l == 4:
        for iv in usable:
            for iii in usable:
                for ii in usable:
                    for i in usable:
                        num.append(int( str(iv) + str(iii) + str(ii) + str(i)))
    else:
        for v in usable:
            for iv in usable:
                for iii in usable:
                    for ii in usable:
                        for i in usable:
                            num.append(int( str(v) + str(iv) + str(iii) + str(ii) + str(i)))
    return num

truf = False
num = check(l)
for i in num:
    if n <= i:
        print(i)
        truf = True
        break
if not truf:
    num = check(l+1)
    for i in num:
        if n <= i:
            print(i)
            break

Buggy submission (verdict: Runtime Error):
n, k = map( int, input().split())
hate = set( input().split())

usable = sorted( list( {str(i) for i in range(10)} - hate))

l = len(str(n))

def check(l):
    num = []
    if l == 1:
        for i in usable:
            num.append(i)
    if l == 2:
        for ii in usable:
            for i in usable:
                num.append(int( str(ii) + str(i)))
    elif l == 3:
        for iii in usable:
            for ii in usable:
                for i in usable:
                    num.append(int( str(iii) + str(ii) + str(i)))
    elif l == 4:
        for iv in usable:
            for iii in usable:
                for ii in usable:
                    for i in usable:
                        num.append(int( str(iv) + str(iii) + str(ii) + str(i)))
    else:
        for v in usable:
            for iv in usable:
                for iii in usable:
                    for ii in usable:
                        for i in usable:
                            num.append(int( str(v) + str(iv) + str(iii) + str(ii) + str(i)))
    return num

truf = False
num = check(l)
for i in num:
    if n <= i:
        print(i)
        truf = True
        break
if not truf:
    num = check(l+1)
    for i in num:
        if n <= i:
            print(i)