# C - Strange Bank

import math

N = int(eval(input()))



INF = 10**10



dp = [0]*(N+1)



for i in range(N+1):

    dp[i] = INF



dp[0] = 0

W = [1,6,9]



# dp{N} に最小値を記録する

# 今の金額

for v in range(1,N+1):

    for w in range(3):

        for x in range(int(math.log(N+1,6))+1):

            # 選ぶ数

            n = W[w]**x

            if v-n < 0:

                continue

            dp[v] = min(dp[v],dp[v-n] + 1)

print((dp[N]))
import sys

import math

sys.setrecursionlimit(10**6)



N = int(eval(input()))



INF = 10**10



dp = [0]*(N+1)



for i in range(N+1):

    dp[i] = INF

    

W = [1,6,9]



def rec(v):

    if dp[v] < INF:

        return dp[v]

    if v == 0:

        dp[v] = 0

        return 0

    

    # v-n を再帰

    res = INF

    for w in range(3):

        for x in range(int(math.log(N+1,6))+1):

            n = W[w]**x

            if v<n:

                continue

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

            # メモして返す

            dp[v] = res

    return res

rec(N)

print((dp[N]))
# C - Strange Bank
import math

N = int(eval(input()))
INF = 10**10
dp = [0] * (N + 1)
for i in range(N + 1):
    dp[i] = INF
dp[0] = 0
W = [1, 6, 9]
# dp{N} に最小値を記録する
# 今の金額
for v in range(1, N + 1):
    for w in range(3):
        for x in range(int(math.log(N + 1, 6)) + 1):
            # 選ぶ数
            n = W[w] ** x
            if v - n < 0:
                continue
            dp[v] = min(dp[v], dp[v - n] + 1)
print((dp[N]))

import sys
import math

sys.setrecursionlimit(10**6)
N = int(eval(input()))
INF = 10**10
dp = [0] * (N + 1)
for i in range(N + 1):
    dp[i] = INF
W = [1, 6, 9]


def rec(v):
    if dp[v] < INF:
        return dp[v]
    if v == 0:
        dp[v] = 0
        return 0
    # v-n を再帰
    res = INF
    for w in range(3):
        for x in range(int(math.log(N + 1, 6)) + 1):
            n = W[w] ** x
            if v < n:
                continue
            res = min(res, rec(v - n) + 1)
            # メモして返す
            dp[v] = res
    return res


rec(N)
print((dp[N]))

-# C - Strange Bank +import sys +sys.setrecursionlimit(10**6) -dp[0] = 0 -# dp{N} に最小値を記録する -# 今の金額 -for v in range(1, N + 1): + + +def rec(v): +    if dp[v] < INF: +        return dp[v] +    if v == 0: +        dp[v] = 0 +        return 0 +    # v-n を再帰 +    res = INF -            # 選ぶ数 -            if v - n < 0: +            if v < n: -            dp[v] = min(dp[v], dp[v - n] + 1) +            res = min(res, rec(v - n) + 1) +            # メモして返す +            dp[v] = res +    return res + + +rec(N)