N = int(eval(input()))

from math import log



max6 = int(log(N, 6))

max9 = int(log(N, 9))

dp = [10**18]*(N+1)

dp[0] = 0

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

    dp[i] = dp[i-1]+1

    for j in range(max6+2):

        if 6**j>i:

            break

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

    for k in range(max9+2):

        if 9**k>i:

            break

        dp[i] = min(dp[i-9**k]+1, dp[i])



print((dp[-1]))
N = int(eval(input()))



dp = [0]*(10**5+1)

for i in range(1, 10**5+1):

	dp[i] = dp[i-1]+1

	cnt = 0

	while 6**cnt<=i:

		dp[i] = min(dp[i], dp[i-6**cnt]+1)

		cnt += 1

	cnt = 0

	while 9**cnt<=i:

		dp[i] = min(dp[i], dp[i-9**cnt]+1)

		cnt += 1

print((dp[N]))
N = int(eval(input()))
from math import log

max6 = int(log(N, 6))
max9 = int(log(N, 9))
dp = [10**18] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    dp[i] = dp[i - 1] + 1
    for j in range(max6 + 2):
        if 6**j > i:
            break
        dp[i] = min(dp[i - 6**j] + 1, dp[i])
    for k in range(max9 + 2):
        if 9**k > i:
            break
        dp[i] = min(dp[i - 9**k] + 1, dp[i])
print((dp[-1]))

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

-from math import log - -max6 = int(log(N, 6)) -max9 = int(log(N, 9)) -dp = [10**18] * (N + 1) -dp[0] = 0 -for i in range(1, N + 1): +dp = [0] * (10**5 + 1) +for i in range(1, 10**5 + 1): -    for j in range(max6 + 2): -        if 6**j > i: -            break -        dp[i] = min(dp[i - 6**j] + 1, dp[i]) -    for k in range(max9 + 2): -        if 9**k > i: -            break -        dp[i] = min(dp[i - 9**k] + 1, dp[i]) -print((dp[-1])) +    cnt = 0 +    while 6**cnt <= i: +        dp[i] = min(dp[i], dp[i - 6**cnt] + 1) +        cnt += 1 +    cnt = 0 +    while 9**cnt <= i: +        dp[i] = min(dp[i], dp[i - 9**cnt] + 1) +        cnt += 1 +print((dp[N]))