N = int(eval(input()))



ans = 1e9

for i in range(N+1):

  cnt = 0

  tmp = i

  while tmp > 0:

    cnt += tmp%6

    tmp //= 6

  tmp = N-i

  while tmp > 0:

    cnt += tmp%9

    tmp //= 9

  ans = min(ans, cnt)

print(ans)
from itertools import count, takewhile

N = int(eval(input()))



def dfs(n, d={}):

  if(n < 6):

    return n

  if(n in d):

    return d[n]

  mn = max(takewhile(lambda x: x<=n, (9**i for i in count())))

  ms = max(takewhile(lambda x: x<=n, (6**i for i in count())))

  d[n] = min(dfs(n-mn), dfs(n-ms))+1

  return d[n]



print((dfs(N)))
N = int(eval(input()))
ans = 1e9
for i in range(N + 1):
    cnt = 0
    tmp = i
    while tmp > 0:
        cnt += tmp % 6
        tmp //= 6
    tmp = N - i
    while tmp > 0:
        cnt += tmp % 9
        tmp //= 9
    ans = min(ans, cnt)
print(ans)

from itertools import count, takewhile

N = int(eval(input()))


def dfs(n, d={}):
    if n < 6:
        return n
    if n in d:
        return d[n]
    mn = max(takewhile(lambda x: x <= n, (9**i for i in count())))
    ms = max(takewhile(lambda x: x <= n, (6**i for i in count())))
    d[n] = min(dfs(n - mn), dfs(n - ms)) + 1
    return d[n]


print((dfs(N)))

+from itertools import count, takewhile + -ans = 1e9 -for i in range(N + 1): -    cnt = 0 -    tmp = i -    while tmp > 0: -        cnt += tmp % 6 -        tmp //= 6 -    tmp = N - i -    while tmp > 0: -        cnt += tmp % 9 -        tmp //= 9 -    ans = min(ans, cnt) -print(ans) + + +def dfs(n, d={}): +    if n < 6: +        return n +    if n in d: +        return d[n] +    mn = max(takewhile(lambda x: x <= n, (9**i for i in count()))) +    ms = max(takewhile(lambda x: x <= n, (6**i for i in count()))) +    d[n] = min(dfs(n - mn), dfs(n - ms)) + 1 +    return d[n] + + +print((dfs(N)))