Problem p04045 (python)

Fixed submission:
import sys
def main():
    input=sys.stdin.readline
    N,K=map(int,input().split())
    D=list(map(int,input().split()))
    L=[]
    for i in range(10):
        if i not in D:
            L.append(i)
    for p in L:
        if len(str(N))==1:
            if p>=N:
                print(p)
                return
        else:
            for q in L:
                if len(str(N))==2:
                    pay=p*10+q
                    if pay>=N:
                        print(pay)
                        return
                else:
                    for r in L:
                        if len(str(N))==3:
                            pay=p*100+q*10+r
                            if pay>=N:
                                print(pay)
                                return
                        else:
                            for s in L:
                                pay=p*1000+q*100+r*10+s
                                if pay>=N:
                                    print(pay)
                                    return
    if L[0]==0:
        print(L[1]*10**len(str(N)))
    else:
        print("".join([str(L[0]) for _ in range(1+len(str(N)))]))
    return
            

if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
import sys
def main():
    input=sys.stdin.readline
    N,K=map(int,input().split())
    D=list(map(int,input().split()))
    L=[]
    for i in range(10):
        if i not in D:
            L.append(i)
    for p in L:
        if len(str(N))==1:
            if p>=N:
                print(p)
                return
        else:
            for q in L:
                if len(str(N))==2:
                    pay=p*10+q
                    if pay>=N:
                        print(pay)
                        return
                else:
                    for r in L:
                        if len(str(N))==3:
                            pay=p*100+q*10+r
                            if pay>=N:
                                print(pay)
                                return
                        else:
                            for s in L:
                                pay=p*1000+q*100+r*10+s
                                if pay>=N:
                                    print(pay)
                                    return
    if L[0]==0:
        print(L[1]*10**len(str(N)))
    else:
        print("".join([L[0] for _ in range(len(str(N)))]))
    return
            

if __name__=="__main__":
    main()

Buggy submission (verdict: Runtime Error):
def main():
    input=sys.stdin.readline
    N,K=map(int,input().split())
    D=list(map(int,input().split()))
    L=[]
    for i in range(10):
        if i not in D:
            L.append(i)
    for p in L:
        if len(str(N))==1:
            if p>=N:
                print(p)
                return
        else:
            for q in L:
                if len(str(N))==2:
                    pay=p*10+q
                    if pay>=N:
                        print(pay)
                        return
                else:
                    for r in L:
                        if len(str(N))==3:
                            pay=p*100+q*10+r
                            if pay>=N:
                                print(pay)
                                return
                        else:
                            for s in L:
                                pay=p*1000+q*100+r*10+s
                                if pay>=N:
                                    print(pay)
                                    return
    if L[0]==0:
        print(L[1]*10**len(str(N)))
    else:
        print("".join([L[0] for _ in range(len(str(N)))]))
    return
            

if __name__=="__main__":
    main()