import math



N=int(eval(input()))

number_of_withdrawal=[100000]



# def floor(float_number):

#     integer = 0

#     while integer < float_number:

#         integer+=1

#     if integer == float_number:

#         return integer

#     else:

#         integer-=1

#         return integer



for money_6 in range(0, N+1):

    money_9 = N-money_6



    i=0

    count_6_pow_number=0

    while pow(6,i)<=money_6:

        count_6_pow_number += (math.floor(money_6/pow(6,i)))%6

        i+=1

    j=0

    count_9_pow_number=0

    while pow(9,j)<=money_9:

        count_9_pow_number += (math.floor(money_9/pow(9,j)))%9

        j+=1



    # print(money_6, money_9, sum(count_6_pow_number), sum(count_9_pow_number))

    number_of_withdrawal.append(count_6_pow_number+count_9_pow_number)



print((min(number_of_withdrawal)))
N=int(eval(input()))

money_list=[1,6,36,216,1296,7776,46656,9,81,729,6561,59049]

dp=[0]

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

    dp.append(100000)



for i in range(N):

    for money in money_list:

        if i+money <= N:

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

print((dp[N]))
import math

N = int(eval(input()))
number_of_withdrawal = [100000]
# def floor(float_number):
#     integer = 0
#     while integer < float_number:
#         integer+=1
#     if integer == float_number:
#         return integer
#     else:
#         integer-=1
#         return integer
for money_6 in range(0, N + 1):
    money_9 = N - money_6
    i = 0
    count_6_pow_number = 0
    while pow(6, i) <= money_6:
        count_6_pow_number += (math.floor(money_6 / pow(6, i))) % 6
        i += 1
    j = 0
    count_9_pow_number = 0
    while pow(9, j) <= money_9:
        count_9_pow_number += (math.floor(money_9 / pow(9, j))) % 9
        j += 1
    # print(money_6, money_9, sum(count_6_pow_number), sum(count_9_pow_number))
    number_of_withdrawal.append(count_6_pow_number + count_9_pow_number)
print((min(number_of_withdrawal)))

N = int(eval(input()))
money_list = [1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049]
dp = [0]
for i in range(1, N + 1):
    dp.append(100000)
for i in range(N):
    for money in money_list:
        if i + money <= N:
            dp[i + money] = min(dp[i + money], dp[i] + 1)
print((dp[N]))

-import math - -number_of_withdrawal = [100000] -# def floor(float_number): -#     integer = 0 -#     while integer < float_number: -#         integer+=1 -#     if integer == float_number: -#         return integer -#     else: -#         integer-=1 -#         return integer -for money_6 in range(0, N + 1): -    money_9 = N - money_6 -    i = 0 -    count_6_pow_number = 0 -    while pow(6, i) <= money_6: -        count_6_pow_number += (math.floor(money_6 / pow(6, i))) % 6 -        i += 1 -    j = 0 -    count_9_pow_number = 0 -    while pow(9, j) <= money_9: -        count_9_pow_number += (math.floor(money_9 / pow(9, j))) % 9 -        j += 1 -    # print(money_6, money_9, sum(count_6_pow_number), sum(count_9_pow_number)) -    number_of_withdrawal.append(count_6_pow_number + count_9_pow_number) -print((min(number_of_withdrawal))) +money_list = [1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049] +dp = [0] +for i in range(1, N + 1): +    dp.append(100000) +for i in range(N): +    for money in money_list: +        if i + money <= N: +            dp[i + money] = min(dp[i + money], dp[i] + 1) +print((dp[N]))