Problem p02720 (python)

Fixed submission:
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K = int(input())
RUN = {
    0:[0, 1],
    1:[0, 1, 2],
    2:[1, 2, 3],
    3:[2, 3, 4],
    4:[3, 4, 5],
    5:[4, 5, 6],
    6:[5, 6, 7],
    7:[6, 7, 8],
    8:[7, 8, 9],
    9:[8, 9]
}
cnt = 0
for i10 in range(10):
    ans10 = i10 * 10 ** 9
    if ans10 == 0:
        i9s = range(10)
    else:
        i9s = RUN[i10]
    for i9 in i9s:
        ans9 = ans10 + i9 * 10 ** 8
        if ans9 == 0:
            i8s = range(10)
        else:
            i8s = RUN[i9]
        for i8 in i8s:
            ans8 = ans9 + i8 * 10 ** 7
            if ans8 == 0:
                i7s = range(10)
            else:
                i7s = RUN[i8]
            for i7 in i7s:
                ans7 = ans8 + i7 * 10 ** 6
                if ans7 == 0:
                    i6s = range(10)
                else:
                    i6s = RUN[i7]
                for i6 in i6s:
                    ans6 = ans7 + i6 * 10 ** 5
                    if ans6 == 0:
                        i5s = range(10)
                    else:
                        i5s = RUN[i6]
                    for i5 in i5s:
                        ans5 = ans6 + i5 * 10 ** 4
                        if ans5 == 0:
                            i4s = range(10)
                        else:
                            i4s = RUN[i5]
                        for i4 in i4s:
                            ans4 = ans5 + i4 * 10 ** 3
                            if ans4 == 0:
                                i3s = range(10)
                            else:
                                i3s = RUN[i4]
                            for i3 in i3s:
                                ans3 = ans4 + i3 * 10 ** 2
                                if ans3 == 0:
                                    i2s = range(10)
                                else:
                                    i2s = RUN[i3]
                                for i2 in i2s:
                                    ans2 = ans3 + i2 * 10
                                    if ans2 == 0:
                                        i1s = range(1, 10)
                                    else:
                                        i1s = RUN[i2]
                                    for i1 in i1s:
                                        ans1 = ans2 + i1
                                        cnt += 1
                                        # print(cnt, ans1)
                                        if cnt == K:
                                            print(ans1)
                                            exit()

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K = int(input())
RUN = {
    0:[0, 1],
    1:[0, 1, 2],
    2:[1, 2, 3],
    3:[2, 3, 4],
    4:[3, 4, 5],
    5:[4, 5, 6],
    6:[5, 6, 7],
    7:[6, 7, 8],
    8:[7, 8, 9],
    9:[8, 9]
}
cnt = 0

def dfs(i, s, p):
    if i == 0:
        cnt = cnt + 1
        if cnt == K:
            print(s)
            exit()
    if s == 0:
        for ii in range(10):
            dfs(i-1, s+ii*10**(i-1), ii)
    else:
        for ii in RUN[p]:
            dfs(i-1, s+ii*10**(i-1), ii)

dfs(10, 0, 0)

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
K = int(input())
RUN = {
    0:[0, 1],
    1:[0, 1, 2],
    2:[1, 2, 3],
    3:[2, 3, 4],
    4:[3, 4, 5],
    5:[4, 5, 6],
    6:[5, 6, 7],
    7:[6, 7, 8],
    8:[7, 8, 9],
    9:[8, 9]
}
cnt = 0

def dfs(i, s, p):
    if i == 0:
        cnt = cnt + 1
        if cnt == K:
            print(s)
            exit()
    if s == 0:
        for ii in range(10):
            dfs(i-1, s+ii*10**(i-1), ii)
    else:
        for ii in RUN[p]:
            dfs(i-1, s+ii*10**(i-1), ii)

dfs(10, 0, 0)