n = int(eval(input()))

L = [1]

for i in range(1, 6):

    L.append(pow(9, i))

for i in range(1, 7):

    L.append(pow(6, i))

L = sorted(L)

dp = [float("inf") for _ in range(n + 1)]

dp[0] = 0

for i in range(1, n + 1):

    for l in L:

        if i - l >= 0:

            dp[i] = min(dp[i], dp[i - l] + 1)

print((dp[n]))

import sys



sys.setrecursionlimit(10 ** 7)

f_inf = float('inf')

mod = 10 ** 9 + 7





def resolve():

    n = int(eval(input()))

    List = [1]



    for i in range(1, 7):

        List.append(pow(6, i))

        List.append(pow(9, i))



    List.sort()

    dp = [f_inf for _ in range(n + 1)]

    dp[0] = 0



    for i in range(1, n + 1):

        for L in List:

            if i - L >= 0:

                dp[i] = min(dp[i], dp[i - L] + 1)



    print((dp[n]))





if __name__ == '__main__':

    resolve()

n = int(eval(input()))
L = [1]
for i in range(1, 6):
    L.append(pow(9, i))
for i in range(1, 7):
    L.append(pow(6, i))
L = sorted(L)
dp = [float("inf") for _ in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
    for l in L:
        if i - l >= 0:
            dp[i] = min(dp[i], dp[i - l] + 1)
print((dp[n]))

import sys

sys.setrecursionlimit(10**7)
f_inf = float("inf")
mod = 10**9 + 7


def resolve():
    n = int(eval(input()))
    List = [1]
    for i in range(1, 7):
        List.append(pow(6, i))
        List.append(pow(9, i))
    List.sort()
    dp = [f_inf for _ in range(n + 1)]
    dp[0] = 0
    for i in range(1, n + 1):
        for L in List:
            if i - L >= 0:
                dp[i] = min(dp[i], dp[i - L] + 1)
    print((dp[n]))


if __name__ == "__main__":
    resolve()

-n = int(eval(input())) -L = [1] -for i in range(1, 6): -    L.append(pow(9, i)) -for i in range(1, 7): -    L.append(pow(6, i)) -L = sorted(L) -dp = [float("inf") for _ in range(n + 1)] -dp[0] = 0 -for i in range(1, n + 1): -    for l in L: -        if i - l >= 0: -            dp[i] = min(dp[i], dp[i - l] + 1) -print((dp[n])) +import sys + +sys.setrecursionlimit(10**7) +f_inf = float("inf") +mod = 10**9 + 7 + + +def resolve(): +    n = int(eval(input())) +    List = [1] +    for i in range(1, 7): +        List.append(pow(6, i)) +        List.append(pow(9, i)) +    List.sort() +    dp = [f_inf for _ in range(n + 1)] +    dp[0] = 0 +    for i in range(1, n + 1): +        for L in List: +            if i - L >= 0: +                dp[i] = min(dp[i], dp[i - L] + 1) +    print((dp[n])) + + +if __name__ == "__main__": +    resolve()