import math

n = int(eval(input()))



target_ls = [1]

target_6 = 6

while target_6 <= n:

    target_ls.append(target_6)

    target_6 *= 6

target_9 = 9

while target_9 <= n:

    target_ls.append(target_9)

    target_9 *= 9



dp = [float('inf')] * (n+1)

dp[0] = 0

for i in range(n):

    for j in target_ls:

        if i+j <= n:

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

print((dp[n]))

    
n = int(eval(input()))

min_time = [float('inf')] * (n+1)

min_time[0] = 0

for i in range(n):

    min_time[i+1] = min(min_time[i+1],min_time[i]+1)

    times_6 = 1

    while i+6**times_6<=n:

        min_time[i+6**times_6] = min(min_time[i+6**times_6],min_time[i]+1)

        times_6 += 1

    times_9 = 1

    while i+9**times_9<=n:

        min_time[i+9**times_9] = min(min_time[i+9**times_9],min_time[i]+1)

        times_9 += 1

print((min_time[-1]))









import math

n = int(eval(input()))
target_ls = [1]
target_6 = 6
while target_6 <= n:
    target_ls.append(target_6)
    target_6 *= 6
target_9 = 9
while target_9 <= n:
    target_ls.append(target_9)
    target_9 *= 9
dp = [float("inf")] * (n + 1)
dp[0] = 0
for i in range(n):
    for j in target_ls:
        if i + j <= n:
            dp[i + j] = min(dp[i + j], dp[i] + 1)
print((dp[n]))

n = int(eval(input()))
min_time = [float("inf")] * (n + 1)
min_time[0] = 0
for i in range(n):
    min_time[i + 1] = min(min_time[i + 1], min_time[i] + 1)
    times_6 = 1
    while i + 6**times_6 <= n:
        min_time[i + 6**times_6] = min(min_time[i + 6**times_6], min_time[i] + 1)
        times_6 += 1
    times_9 = 1
    while i + 9**times_9 <= n:
        min_time[i + 9**times_9] = min(min_time[i + 9**times_9], min_time[i] + 1)
        times_9 += 1
print((min_time[-1]))

-import math - -target_ls = [1] -target_6 = 6 -while target_6 <= n: -    target_ls.append(target_6) -    target_6 *= 6 -target_9 = 9 -while target_9 <= n: -    target_ls.append(target_9) -    target_9 *= 9 -dp = [float("inf")] * (n + 1) -dp[0] = 0 +min_time = [float("inf")] * (n + 1) +min_time[0] = 0 -    for j in target_ls: -        if i + j <= n: -            dp[i + j] = min(dp[i + j], dp[i] + 1) -print((dp[n])) +    min_time[i + 1] = min(min_time[i + 1], min_time[i] + 1) +    times_6 = 1 +    while i + 6**times_6 <= n: +        min_time[i + 6**times_6] = min(min_time[i + 6**times_6], min_time[i] + 1) +        times_6 += 1 +    times_9 = 1 +    while i + 9**times_9 <= n: +        min_time[i + 9**times_9] = min(min_time[i + 9**times_9], min_time[i] + 1) +        times_9 += 1 +print((min_time[-1]))