N = int(eval(input()))

dp = dict()



dp[0] = 0

# 貰う DP --- dp[n] に遷移を集める

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

  dp[n] = dp[n-1] + 1

  

  # メモ化

  x = 6

  while x <= n:

    dp[n] = min(dp[n], dp[n-x]+1)

    x *= 6

  

  x = 9

  while x <= n:

    dp[n] = min(dp[n], dp[n-x]+1)

    x *= 9

    

  # dp[n] = ans

  

print((dp[N]))
n = int(eval(input()))



max_N = 1000000

memo = [False]*max_N

import sys

import threading

sys.setrecursionlimit(100000)

threading.stack_size(128*1024*1024)





# 貰う DPではなく、再帰を使ったメモ化再帰

from functools import lru_cache



lru_cache(maxsize=100000)

def f(n):

	if n < 6:

		return n

	if memo[n]:

		# print(f"at n: {n}, memo[n]:", memo[n])

		return memo[n]

	

	ans = n

	# メモ化

	x = 6

	while x <= n:

		ans = min(ans, f(n-x)+1)

		x *= 6

	

	x = 9

	while x <= n:

		ans = min(ans, f(n-x)+1)

		x *= 9

	

	memo[n] = ans

	return ans



def main():

	print((f(n)))

	

threading.Thread(target=main).start()
N = int(eval(input()))
dp = dict()
dp[0] = 0
# 貰う DP --- dp[n] に遷移を集める
for n in range(1, N + 1):
    dp[n] = dp[n - 1] + 1
    # メモ化
    x = 6
    while x <= n:
        dp[n] = min(dp[n], dp[n - x] + 1)
        x *= 6
    x = 9
    while x <= n:
        dp[n] = min(dp[n], dp[n - x] + 1)
        x *= 9
    # dp[n] = ans
print((dp[N]))

n = int(eval(input()))
max_N = 1000000
memo = [False] * max_N
import sys
import threading

sys.setrecursionlimit(100000)
threading.stack_size(128 * 1024 * 1024)
# 貰う DPではなく、再帰を使ったメモ化再帰
from functools import lru_cache

lru_cache(maxsize=100000)


def f(n):
    if n < 6:
        return n
    if memo[n]:
        # print(f"at n: {n}, memo[n]:", memo[n])
        return memo[n]
    ans = n
    # メモ化
    x = 6
    while x <= n:
        ans = min(ans, f(n - x) + 1)
        x *= 6
    x = 9
    while x <= n:
        ans = min(ans, f(n - x) + 1)
        x *= 9
    memo[n] = ans
    return ans


def main():
    print((f(n)))


threading.Thread(target=main).start()

-N = int(eval(input())) -dp = dict() -dp[0] = 0 -for n in range(1, N + 1): -    dp[n] = dp[n - 1] + 1 +n = int(eval(input())) +max_N = 1000000 +memo = [False] * max_N +import sys +import threading + +sys.setrecursionlimit(100000) +threading.stack_size(128 * 1024 * 1024) +# 貰う DPではなく、再帰を使ったメモ化再帰 +from functools import lru_cache + +lru_cache(maxsize=100000) + + +def f(n): +    if n < 6: +        return n +    if memo[n]: +        # print(f"at n: {n}, memo[n]:", memo[n]) +        return memo[n] +    ans = n -        dp[n] = min(dp[n], dp[n - x] + 1) +        ans = min(ans, f(n - x) + 1) -        dp[n] = min(dp[n], dp[n - x] + 1) +        ans = min(ans, f(n - x) + 1) -    # dp[n] = ans -print((dp[N])) +    memo[n] = ans +    return ans + + +def main(): +    print((f(n))) + + +threading.Thread(target=main).start()