import sys

sys.setrecursionlimit(10**7)

N = int(eval(input()))



wd = [1]

n = 1

while 6**n <= N:

    wd.append(6**n)

    n += 1

n = 1

while 9**n <= N:

    wd.append(9**n)

    n += 1

if N in wd:

    print((1))

    exit()

wd.sort()



INF = float('inf')

mem = [INF] * (N+1)

def dfs(n):

    if mem[n] != INF: return mem[n]

    if n < 6: return n

    mn = INF

    for d in wd[1:]:

        if n-d < 0: break

        mn = min(mn, 1 + dfs(n-d))

    mem[n] = mn

    return mn



print((dfs(N)))
import itertools

N = int(eval(input()))



sixs = [6, 36, 216, 1296, 7776, 46656]



ans = N

for ptn in itertools.product(list(range(6)), repeat=6):

    money = ope = 0

    for s,p in zip(sixs,ptn):

        money += s*p

        ope += p

    if money > N: continue

    if ope > ans: continue

    rem = N - money

    while rem:

        ope += rem%9

        rem //= 9

    ans = min(ans, ope)

print(ans)
import sys

sys.setrecursionlimit(10**7)
N = int(eval(input()))
wd = [1]
n = 1
while 6**n <= N:
    wd.append(6**n)
    n += 1
n = 1
while 9**n <= N:
    wd.append(9**n)
    n += 1
if N in wd:
    print((1))
    exit()
wd.sort()
INF = float("inf")
mem = [INF] * (N + 1)


def dfs(n):
    if mem[n] != INF:
        return mem[n]
    if n < 6:
        return n
    mn = INF
    for d in wd[1:]:
        if n - d < 0:
            break
        mn = min(mn, 1 + dfs(n - d))
    mem[n] = mn
    return mn


print((dfs(N)))

import itertools

N = int(eval(input()))
sixs = [6, 36, 216, 1296, 7776, 46656]
ans = N
for ptn in itertools.product(list(range(6)), repeat=6):
    money = ope = 0
    for s, p in zip(sixs, ptn):
        money += s * p
        ope += p
    if money > N:
        continue
    if ope > ans:
        continue
    rem = N - money
    while rem:
        ope += rem % 9
        rem //= 9
    ans = min(ans, ope)
print(ans)

-import sys +import itertools -sys.setrecursionlimit(10**7) -wd = [1] -n = 1 -while 6**n <= N: -    wd.append(6**n) -    n += 1 -n = 1 -while 9**n <= N: -    wd.append(9**n) -    n += 1 -if N in wd: -    print((1)) -    exit() -wd.sort() -INF = float("inf") -mem = [INF] * (N + 1) - - -def dfs(n): -    if mem[n] != INF: -        return mem[n] -    if n < 6: -        return n -    mn = INF -    for d in wd[1:]: -        if n - d < 0: -            break -        mn = min(mn, 1 + dfs(n - d)) -    mem[n] = mn -    return mn - - -print((dfs(N))) +sixs = [6, 36, 216, 1296, 7776, 46656] +ans = N +for ptn in itertools.product(list(range(6)), repeat=6): +    money = ope = 0 +    for s, p in zip(sixs, ptn): +        money += s * p +        ope += p +    if money > N: +        continue +    if ope > ans: +        continue +    rem = N - money +    while rem: +        ope += rem % 9 +        rem //= 9 +    ans = min(ans, ope) +print(ans)