N = int(eval(input()))

dp = [N] * (N + 1)

dp[0] = 0

for i in range(N):

    j = 1

    while i + j <= N:

        dp[i + j] = min(dp[i + j], dp[i] + 1)

        j *= 6

    j = 9

    while i + j <= N:

        dp[i + j] = min(dp[i + j], dp[i] + 1)

        j *= 9

print((dp[-1]))

import sys

from functools import lru_cache





sys.setrecursionlimit(500000)





@lru_cache(maxsize=None)

def rec(n):

    if n == 0:

        return 0

    res = n

    i = 1

    while i <= n:

        res = min(res, rec(n - i) + 1)

        i *= 6

    i = 9

    while i <= n:

        res = min(res, rec(n - i) + 1)

        i *= 9

    return res





N = int(eval(input()))

print((rec(N)))





N = int(eval(input()))
dp = [N] * (N + 1)
dp[0] = 0
for i in range(N):
    j = 1
    while i + j <= N:
        dp[i + j] = min(dp[i + j], dp[i] + 1)
        j *= 6
    j = 9
    while i + j <= N:
        dp[i + j] = min(dp[i + j], dp[i] + 1)
        j *= 9
print((dp[-1]))

import sys
from functools import lru_cache

sys.setrecursionlimit(500000)


@lru_cache(maxsize=None)
def rec(n):
    if n == 0:
        return 0
    res = n
    i = 1
    while i <= n:
        res = min(res, rec(n - i) + 1)
        i *= 6
    i = 9
    while i <= n:
        res = min(res, rec(n - i) + 1)
        i *= 9
    return res


N = int(eval(input()))
print((rec(N)))

+import sys +from functools import lru_cache + +sys.setrecursionlimit(500000) + + +@lru_cache(maxsize=None) +def rec(n): +    if n == 0: +        return 0 +    res = n +    i = 1 +    while i <= n: +        res = min(res, rec(n - i) + 1) +        i *= 6 +    i = 9 +    while i <= n: +        res = min(res, rec(n - i) + 1) +        i *= 9 +    return res + + -dp = [N] * (N + 1) -dp[0] = 0 -for i in range(N): -    j = 1 -    while i + j <= N: -        dp[i + j] = min(dp[i + j], dp[i] + 1) -        j *= 6 -    j = 9 -    while i + j <= N: -        dp[i + j] = min(dp[i + j], dp[i] + 1) -        j *= 9 -print((dp[-1])) +print((rec(N)))