Buggy python submission (verdict: Runtime Error):
import math
import sys

N = int(sys.stdin.readline())

queue = [(N, 1)]
min_cnt = math.inf

while queue:
    N, cnt = queue.pop()
    if N >= 9:
        v = math.pow(9, math.floor(math.log(N, 9)))
        if N - v >= 6 and cnt < min_cnt:
            queue.append((N - v, cnt + 1))
        elif cnt + (N - v) < min_cnt:
            min_cnt = cnt + int(N - v)
    if N >= 6:
        v = math.pow(6, math.floor(math.log(N, 6)))
        if cnt < min_cnt:
            queue.append((N - v, cnt + 1))
        elif cnt + (N - v) < min_cnt:
            min_cnt = cnt + int(N - v)

if math.isinf(min_cnt):
    print(N)
else:
    print(min_cnt)

Fixed submission:
import itertools
import math
import sys

MAX_N = 100000
MAX_LOG6 = int(math.log(MAX_N, 6))
MAX_LOG9 = int(math.log(MAX_N, 9))

def main(N):
    dp = [MAX_N] * (N + 1)
    for i in range(min(N + 1, 6)):
        dp[i] = i
    for i in range(N + 1):
        t = 1
        for j in range(1, MAX_LOG6 + 1):
            t *= 6
            if i - t < 0:
                break
            dp[i] = min(dp[i], dp[i - t] + 1)
        t = 1
        for j in range(1, MAX_LOG9 + 1):
            t *= 9
            if i - t < 0:
                break
            dp[i] = min(dp[i], dp[i - t] + 1)
    print(dp[N])

if __name__ == '__main__':
    N = int(sys.stdin.readline())
    main(N)

Buggy python submission (verdict: Runtime Error):
import math
import sys

N = int(sys.stdin.readline())

queue = [(N, 1)]
blacklist = set()
min_cnt = math.inf

while queue:
    N, cnt = queue.pop()
    if N >= 9:
        v = math.pow(9, math.floor(math.log(N, 9)))
        if N - v >= 6 and cnt < min_cnt:
            queue.append((N - v, cnt + 1))
        elif cnt + (N - v) < min_cnt:
            min_cnt = cnt + int(N - v)
    if N >= 6:
        v = math.pow(6, math.floor(math.log(N, 6)))
        if cnt < min_cnt:
            queue.append((N - v, cnt + 1))
        elif cnt + (N - v) < min_cnt:
            min_cnt = cnt + int(N - v)

if math.isinf(min_cnt):
    print(N)
else:
    print(min_cnt)

Fixed submission:
import itertools
import math
import sys

MAX_N = 100000
MAX_LOG6 = int(math.log(MAX_N, 6))
MAX_LOG9 = int(math.log(MAX_N, 9))

def main(N):
    dp = [MAX_N] * (N + 1)
    for i in range(min(N + 1, 6)):
        dp[i] = i
    for i in range(N + 1):
        t = 1
        for j in range(1, MAX_LOG6 + 1):
            t *= 6
            if i - t < 0:
                break
            dp[i] = min(dp[i], dp[i - t] + 1)
        t = 1
        for j in range(1, MAX_LOG9 + 1):
            t *= 9
            if i - t < 0:
                break
            dp[i] = min(dp[i], dp[i - t] + 1)
    print(dp[N])

if __name__ == '__main__':
    N = int(sys.stdin.readline())
    main(N)

Buggy python submission (verdict: Runtime Error):
import math
import sys

N = int(sys.stdin.readline())

min_leaf_cnt = math.inf

class Node:
    def __init__(self, N, cnt, v):
        self.N = N
        self.cnt = cnt
        self.v = v
        self.left = None
        self.right = None
        if self.cnt < min_leaf_cnt:
            self._fill_leaf()

    def _fill_leaf(self):
        if self.N >= 6:
            v = math.pow(6, math.floor(math.log(self.N, 6)))
            self.left = Node(self.N - v, self.cnt + 1, v)
        if self.N >= 9:
            v = math.pow(9, math.floor(math.log(self.N, 9)))
            self.right = Node(self.N - v, self.cnt + 1, v)
        if self.left is None and self.right is None:
            global min_leaf_cnt
            if self.cnt + self.N < min_leaf_cnt:
                min_leaf_cnt = self.cnt + int(self.N)

Node(N, 0, 0)
print(min_leaf_cnt)

Fixed submission:
import itertools
import math
import sys

MAX_N = 100000
MAX_LOG6 = int(math.log(MAX_N, 6))
MAX_LOG9 = int(math.log(MAX_N, 9))

def main(N):
    dp = [MAX_N] * (N + 1)
    for i in range(min(N + 1, 6)):
        dp[i] = i
    for i in range(N + 1):
        t = 1
        for j in range(1, MAX_LOG6 + 1):
            t *= 6
            if i - t < 0:
                break
            dp[i] = min(dp[i], dp[i - t] + 1)
        t = 1
        for j in range(1, MAX_LOG9 + 1):
            t *= 9
            if i - t < 0:
                break
            dp[i] = min(dp[i], dp[i - t] + 1)
    print(dp[N])

if __name__ == '__main__':
    N = int(sys.stdin.readline())
    main(N)