n = int(eval(input()))

max_n = 100000

dp = [10000 for i in range(max_n+1)]

dp[0] = 0



for i in range(n+1):

  k = 1

  while i + k <= n:

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

    k *=6

  k = 1

  while i + k <= n:

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

    k*=9



print((dp[n]))
from collections import deque

n = int(eval(input()))

max_n = 100000

dp = [10000 for i in range(max_n+1)]

dp[0] = 0

que = deque()

que.append(0)



while len(que) > 0:

  v = que.popleft()

  k = 1

  while v + k <= n:

    if dp[v+k] == 10000:

      dp[v+k]=dp[v]+1

      que.append(v+k)

    k *= 6

  k = 1

  while v + k <= n:

    if dp[v+k] == 10000:

      dp[v+k]=dp[v]+1

      que.append(v+k)

    k *= 9

print((dp[n]))
n = int(eval(input()))
max_n = 100000
dp = [10000 for i in range(max_n + 1)]
dp[0] = 0
for i in range(n + 1):
    k = 1
    while i + k <= n:
        dp[i + k] = min(dp[i + k], dp[i] + 1)
        k *= 6
    k = 1
    while i + k <= n:
        dp[i + k] = min(dp[i + k], dp[i] + 1)
        k *= 9
print((dp[n]))

from collections import deque

n = int(eval(input()))
max_n = 100000
dp = [10000 for i in range(max_n + 1)]
dp[0] = 0
que = deque()
que.append(0)
while len(que) > 0:
    v = que.popleft()
    k = 1
    while v + k <= n:
        if dp[v + k] == 10000:
            dp[v + k] = dp[v] + 1
            que.append(v + k)
        k *= 6
    k = 1
    while v + k <= n:
        if dp[v + k] == 10000:
            dp[v + k] = dp[v] + 1
            que.append(v + k)
        k *= 9
print((dp[n]))

+from collections import deque + -for i in range(n + 1): +que = deque() +que.append(0) +while len(que) > 0: +    v = que.popleft() -    while i + k <= n: -        dp[i + k] = min(dp[i + k], dp[i] + 1) +    while v + k <= n: +        if dp[v + k] == 10000: +            dp[v + k] = dp[v] + 1 +            que.append(v + k) -    while i + k <= n: -        dp[i + k] = min(dp[i + k], dp[i] + 1) +    while v + k <= n: +        if dp[v + k] == 10000: +            dp[v + k] = dp[v] + 1 +            que.append(v + k)