N = int(eval(input()))



INF = N

dp = [0, 1, 2, 3, 4, 5] + [INF for _ in range(N-5)]



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

    dp[amount] = amount



    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))



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()))
INF = N
dp = [0, 1, 2, 3, 4, 5] + [INF for _ in range(N - 5)]
for amount in range(6, N + 1):
    dp[amount] = amount
    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))
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]))

-INF = N -dp = [0, 1, 2, 3, 4, 5] + [INF for _ in range(N - 5)] +dp = list(range(N + 1)) -    dp[amount] = amount