# 配るDP



N=int(eval(input()))



dp = [100000] * 200005;



g = [1]

a = 6

while a <= 100000:

  g.append(a)

  a *= 6

a = 9

while a <= 100000:

  g.append(a)

  a *= 9

g.sort(reverse=True)



dp[0] = 0

for i in range(N):

  for j in g:

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



print((dp[N]))



# BFS

from collections import deque



N=int(eval(input()))



dp = [-1] * 200005;

q = deque()



g = [1]

a = 6

while a <= 100000:

  g.append(a)

  a *= 6

a = 9

while a <= 100000:

  g.append(a)

  a *= 9

g.sort(reverse=True)



dp[0] = 0

q.append(0)



while len(q) > 0:

  v = q.popleft()

  if v == N: break

  for j in g:

    if dp[v+j] != -1: continue

    if v+j > N: continue

    dp[v+j] = dp[v] + 1

    q.append(v+j)



print((dp[N]))

# 配るDP
N = int(eval(input()))
dp = [100000] * 200005
g = [1]
a = 6
while a <= 100000:
    g.append(a)
    a *= 6
a = 9
while a <= 100000:
    g.append(a)
    a *= 9
g.sort(reverse=True)
dp[0] = 0
for i in range(N):
    for j in g:
        dp[i + j] = min(dp[i + j], dp[i] + 1)
print((dp[N]))

# BFS
from collections import deque

N = int(eval(input()))
dp = [-1] * 200005
q = deque()
g = [1]
a = 6
while a <= 100000:
    g.append(a)
    a *= 6
a = 9
while a <= 100000:
    g.append(a)
    a *= 9
g.sort(reverse=True)
dp[0] = 0
q.append(0)
while len(q) > 0:
    v = q.popleft()
    if v == N:
        break
    for j in g:
        if dp[v + j] != -1:
            continue
        if v + j > N:
            continue
        dp[v + j] = dp[v] + 1
        q.append(v + j)
print((dp[N]))

-# 配るDP +# BFS +from collections import deque + -dp = [100000] * 200005 +dp = [-1] * 200005 +q = deque() -for i in range(N): +q.append(0) +while len(q) > 0: +    v = q.popleft() +    if v == N: +        break -        dp[i + j] = min(dp[i + j], dp[i] + 1) +        if dp[v + j] != -1: +            continue +        if v + j > N: +            continue +        dp[v + j] = dp[v] + 1 +        q.append(v + j)