Problem p03212 (python)

Fixed submission:
def function(n):
    digit=len(str(n))
    N=str(n)
    if digit<3:
        return 0
    else:
        k=int(N[0])
        m=int(N[1])
        if k<3:
            return function(10**(digit-1)-1)
        elif k==3 and m>0:
            return function(10**(digit-1)-1)+function(n%(10**(digit-1)))-function(10**(digit-2))+subfunction(5,7,n%(10**(digit-1)))
        elif k==3 and m==0:
            return function(10**(digit-1)-1)
        elif k==4:
            return function(4*(10**(digit-1))-1)
        elif k==5 and m>0:
            return function(4*(10**(digit-1))-1)+function(n%(10**(digit-1)))-function(10**(digit-2))+subfunction(3,7,n%(10**(digit-1)))
        elif k==5 and m==0:
            return function(4*(10**(digit-1))-1)
        elif k==6:
            return function(6*(10**(digit-1))-1)
        elif k==7 and m>0:
            return function(6*(10**(digit-1))-1)+function(n%(10**(digit-1)))-function(10**(digit-2))+subfunction(3,5,n%(10**(digit-1)))
        elif k==7 and m==0:
            return function(6*(10**(digit-1))-1)
        else:
            return function(8*(10**(digit-1))-1)

def subfunction(a,b,n):
    digit=len(str(n))
    N=str(n)
    k=int(N[0])
    if digit<2:
        return 0
    else:
        if k<a:
            return 0
        elif k==a:
            if n%(10**(digit-1))<b*(10**(digit-1)-1)//9:
                s=0
            else:
                s=1
            return subfunction(a,b,n%(10**(digit-1)))+s
        elif a<k<b:
            return subfunction(a,b,(a+1)*(10**(digit-1))-1)
        elif k==b:
            if n%(10**(digit-1))<a*(10**(digit-1)-1)//9:
                s=0
            else:
                s=1
            return subfunction(a,b,(a+1)*(10**(digit-1))-1)+subfunction(a,b,n%(10**(digit-1)))+s
        else:
            return subfunction(a,b,(b+1)*(10**(digit-1))-1)

N=int(input())
print(function(N))

Buggy submission (verdict: Runtime Error):
def function(n):
    digit=len(str(n))
    N=str(n)
    k=int(N[0])
    m=int(N[1])
    if digit<3:
        return 0
    else:
        if k<3:
            return function(10**(digit-1)-1)
        elif k==3 and m>0:
            return function(10**(digit-1)-1)+function(n%(10**(digit-1)))-function(10**(digit-2))+subfunction(5,7,n%(10**(digit-1)))
        elif k==3 and m==0:
            return function(10**(digit-1)-1)
        elif k==4:
            return function(4*(10**(digit-1))-1)
        elif k==5 and m>0:
            return function(4*(10**(digit-1))-1)+function(n%(10**(digit-1)))-function(10**(digit-2))+subfunction(3,7,n%(10**(digit-1)))
        elif k==5 and m==0:
            return function(4*(10**(digit-1))-1)
        elif k==6:
            return function(6*(10**(digit-1))-1)
        elif k==7 and m>0:
            return function(6*(10**(digit-1))-1)+function(n%(10**(digit-1)))-function(10**(digit-2))+subfunction(3,5,n%(10**(digit-1)))
        elif k==7 and m==0:
            return function(6*(10**(digit-1))-1)
        else:
            return function(8*(10**(digit-1))-1)

def subfunction(a,b,n):
    digit=len(str(n))
    N=str(n)
    k=int(N[0])
    if digit<2:
        return 0
    else:
        if k<a:
            return 0
        elif k==a:
            if n%(10**(digit-1))<b*(10**(digit-1)-1)//9:
                s=0
            else:
                s=1
            return subfunction(a,b,n%(10**(digit-1)))+s
        elif a<k<b:
            return subfunction(a,b,(a+1)*(10**(digit-1))-1)
        elif k==b:
            if n%(10**(digit-1))<a*(10**(digit-1)-1)//9:
                s=0
            else:
                s=1
            return subfunction(a,b,(a+1)*(10**(digit-1))-1)+subfunction(a,b,n%(10**(digit-1)))+s
        else:
            return subfunction(a,b,(b+1)*(10**(digit-1))-1)

N=int(input())
print(function(N))