N = int(eval(input()))



dp = list(range(N+1))



for amount in range(6, N+1):

    unit = 6

    while unit <= amount:

        dp[amount] = min(dp[amount], dp[amount-unit]+1)

        unit *= 6



    unit = 9

    while unit <= amount:

        dp[amount] = min(dp[amount], dp[amount-unit]+1)

        unit *= 9



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



dp = list(range(N+1))



def calc(unit_base):

    unit = unit_base

    while unit <= amount:

        dp[amount] = min(dp[amount], dp[amount-unit]+1)

        unit *= unit_base



for amount in range(6, N+1):

    calc(6)

    calc(9)



print((dp[N]))
N = int(eval(input()))
dp = list(range(N + 1))
for amount in range(6, N + 1):
    unit = 6
    while unit <= amount:
        dp[amount] = min(dp[amount], dp[amount - unit] + 1)
        unit *= 6
    unit = 9
    while unit <= amount:
        dp[amount] = min(dp[amount], dp[amount - unit] + 1)
        unit *= 9
print((dp[N]))

N = int(eval(input()))
dp = list(range(N + 1))


def calc(unit_base):
    unit = unit_base
    while unit <= amount:
        dp[amount] = min(dp[amount], dp[amount - unit] + 1)
        unit *= unit_base


for amount in range(6, N + 1):
    calc(6)
    calc(9)
print((dp[N]))

-for amount in range(6, N + 1): -    unit = 6 + + +def calc(unit_base): +    unit = unit_base -        unit *= 6 -    unit = 9 -    while unit <= amount: -        dp[amount] = min(dp[amount], dp[amount - unit] + 1) -        unit *= 9 +        unit *= unit_base + + +for amount in range(6, N + 1): +    calc(6) +    calc(9)