# 配るDP



n = int(eval(input()))





max_n = 110000

dp = [n] * max_n



# 初期化

dp[0] = 0



for i in range(n):

    pow6, pow9 = 1, 1

    while i + pow6 <= n:

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

        pow6 *= 6

    while i + pow9 <= n:

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

        pow9 *= 9

print((dp[n]))

# 貰うDP



n = int(eval(input()))





max_n = 110000

dp = [n] * max_n



# 初期化

dp[0] = 0



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

    pow6, pow9 = 1, 1

    while pow6 <= i:

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

        pow6 *= 6

    while pow9 <= i:

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

        pow9 *= 9



print((dp[n]))

# 配るDP
n = int(eval(input()))
max_n = 110000
dp = [n] * max_n
# 初期化
dp[0] = 0
for i in range(n):
    pow6, pow9 = 1, 1
    while i + pow6 <= n:
        dp[i + pow6] = min(dp[i + pow6], dp[i] + 1)
        pow6 *= 6
    while i + pow9 <= n:
        dp[i + pow9] = min(dp[i + pow9], dp[i] + 1)
        pow9 *= 9
print((dp[n]))

# 貰うDP
n = int(eval(input()))
max_n = 110000
dp = [n] * max_n
# 初期化
dp[0] = 0
for i in range(1, n + 1):
    pow6, pow9 = 1, 1
    while pow6 <= i:
        dp[i] = min(dp[i], dp[i - pow6] + 1)
        pow6 *= 6
    while pow9 <= i:
        dp[i] = min(dp[i], dp[i - pow9] + 1)
        pow9 *= 9
print((dp[n]))

-# 配るDP +# 貰うDP -for i in range(n): +for i in range(1, n + 1): -    while i + pow6 <= n: -        dp[i + pow6] = min(dp[i + pow6], dp[i] + 1) +    while pow6 <= i: +        dp[i] = min(dp[i], dp[i - pow6] + 1) -    while i + pow9 <= n: -        dp[i + pow9] = min(dp[i + pow9], dp[i] + 1) +    while pow9 <= i: +        dp[i] = min(dp[i], dp[i - pow9] + 1)