N = int(eval(input()))

money = [1]

i = 1

while True:

    i *= 6

    if i <= 100000:

        money.append(i)

    else:

        break

i = 1

while True:

    i *= 9

    if i <= 100000:

        money.append(i)

    else:

        break



M = len(money)



dp = [[10 ** 9 for _ in range(N + 1)] for _ in range(M + 1)]



for i in range(M + 1):

    dp[i][0] = 0



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

    for j in range(N + 1):

        if j - money[i - 1] >= 0:

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

        else:

            dp[i][j] = dp[i - 1][j]



print((dp[M][N]))



import sys

import math

N = int(sys.stdin.readline().rstrip())



# def create_money(i):

#     money = []

#     tmp = 1

#     for _ in range(math.floor(math.log(N, i))):

#         tmp *= i

#         money.append(tmp)

    

#     return money



# money = [1]

# money += create_money(6)

# money += create_money(9)



money = [1]

for i in range(20):

    if 100000 >= 6**i:

        money.append(6**i)

    if 100000 >= 9**i:

        money.append(9**i)





dp = [[10**18] * (N + 1) for _ in range(len(money) + 1)]

dp[0][0] = 0



for i in range(1, len(money) + 1):

    m = money[i - 1]

    for j in range(N + 1):

        if j - m >= 0:

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

        else:

            dp[i][j] = dp[i - 1][j]



print((dp[len(money)][N]))
N = int(eval(input()))
money = [1]
i = 1
while True:
    i *= 6
    if i <= 100000:
        money.append(i)
    else:
        break
i = 1
while True:
    i *= 9
    if i <= 100000:
        money.append(i)
    else:
        break
M = len(money)
dp = [[10**9 for _ in range(N + 1)] for _ in range(M + 1)]
for i in range(M + 1):
    dp[i][0] = 0
for i in range(1, M + 1):
    for j in range(N + 1):
        if j - money[i - 1] >= 0:
            dp[i][j] = min(dp[i][j - money[i - 1]] + 1, dp[i - 1][j])
        else:
            dp[i][j] = dp[i - 1][j]
print((dp[M][N]))

import sys
import math

N = int(sys.stdin.readline().rstrip())
# def create_money(i):
#     money = []
#     tmp = 1
#     for _ in range(math.floor(math.log(N, i))):
#         tmp *= i
#         money.append(tmp)
#     return money
# money = [1]
# money += create_money(6)
# money += create_money(9)
money = [1]
for i in range(20):
    if 100000 >= 6**i:
        money.append(6**i)
    if 100000 >= 9**i:
        money.append(9**i)
dp = [[10**18] * (N + 1) for _ in range(len(money) + 1)]
dp[0][0] = 0
for i in range(1, len(money) + 1):
    m = money[i - 1]
    for j in range(N + 1):
        if j - m >= 0:
            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - m] + 1, dp[i][j - m] + 1)
        else:
            dp[i][j] = dp[i - 1][j]
print((dp[len(money)][N]))

-N = int(eval(input())) +import sys +import math + +N = int(sys.stdin.readline().rstrip()) +# def create_money(i): +#     money = [] +#     tmp = 1 +#     for _ in range(math.floor(math.log(N, i))): +#         tmp *= i +#         money.append(tmp) +#     return money +# money = [1] +# money += create_money(6) +# money += create_money(9) -i = 1 -while True: -    i *= 6 -    if i <= 100000: -        money.append(i) -    else: -        break -i = 1 -while True: -    i *= 9 -    if i <= 100000: -        money.append(i) -    else: -        break -M = len(money) -dp = [[10**9 for _ in range(N + 1)] for _ in range(M + 1)] -for i in range(M + 1): -    dp[i][0] = 0 -for i in range(1, M + 1): +for i in range(20): +    if 100000 >= 6**i: +        money.append(6**i) +    if 100000 >= 9**i: +        money.append(9**i) +dp = [[10**18] * (N + 1) for _ in range(len(money) + 1)] +dp[0][0] = 0 +for i in range(1, len(money) + 1): +    m = money[i - 1] -        if j - money[i - 1] >= 0: -            dp[i][j] = min(dp[i][j - money[i - 1]] + 1, dp[i - 1][j]) +        if j - m >= 0: +            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - m] + 1, dp[i][j - m] + 1) -print((dp[M][N])) +print((dp[len(money)][N]))