N = int(eval(input()))

inf = 10 ** 6

dp = [inf] * (N + 1)

dp[0] = 0

for i in range(N):

  if i + 1 <= N:

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

  for j in range(1, 8):

    if i + 6 ** j <= N:

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

  for j in range(1, 7):

    if i + 9 ** j <= N:

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

print((dp[N]))
import sys

input = sys.stdin.readline

N = int(eval(input()))

dp = [float("inf")] * (N + 1)

dp[0] = 0

for i in range(N):

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

  for j in range(1, 8):

    if i + pow(6, j) <= N: dp[i + pow(6, j)] = min(dp[i + pow(6, j)], dp[i] + 1)

    if i + pow(9, j) <= N: dp[i + pow(9, j)] = min(dp[i + pow(9, j)], dp[i] + 1)

print((dp[-1]))
N = int(eval(input()))
inf = 10**6
dp = [inf] * (N + 1)
dp[0] = 0
for i in range(N):
    if i + 1 <= N:
        dp[i + 1] = min(dp[i] + 1, dp[i + 1])
    for j in range(1, 8):
        if i + 6**j <= N:
            dp[i + 6**j] = min(dp[i] + 1, dp[i + 6**j])
    for j in range(1, 7):
        if i + 9**j <= N:
            dp[i + 9**j] = min(dp[i] + 1, dp[i + 9**j])
print((dp[N]))

import sys

input = sys.stdin.readline
N = int(eval(input()))
dp = [float("inf")] * (N + 1)
dp[0] = 0
for i in range(N):
    dp[i + 1] = min(dp[i + 1], dp[i] + 1)
    for j in range(1, 8):
        if i + pow(6, j) <= N:
            dp[i + pow(6, j)] = min(dp[i + pow(6, j)], dp[i] + 1)
        if i + pow(9, j) <= N:
            dp[i + pow(9, j)] = min(dp[i + pow(9, j)], dp[i] + 1)
print((dp[-1]))

+import sys + +input = sys.stdin.readline -inf = 10**6 -dp = [inf] * (N + 1) +dp = [float("inf")] * (N + 1) -    if i + 1 <= N: -        dp[i + 1] = min(dp[i] + 1, dp[i + 1]) +    dp[i + 1] = min(dp[i + 1], dp[i] + 1) -        if i + 6**j <= N: -            dp[i + 6**j] = min(dp[i] + 1, dp[i + 6**j]) -    for j in range(1, 7): -        if i + 9**j <= N: -            dp[i + 9**j] = min(dp[i] + 1, dp[i + 9**j]) -print((dp[N])) +        if i + pow(6, j) <= N: +            dp[i + pow(6, j)] = min(dp[i + pow(6, j)], dp[i] + 1) +        if i + pow(9, j) <= N: +            dp[i + pow(9, j)] = min(dp[i + pow(9, j)], dp[i] + 1) +print((dp[-1]))