n = int(eval(input()))

dp = [i for i in range(n+1)]  # dp table 最初は全て1円で払う

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

    power = 1

    while power <= j:

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

        power *= 6

    power = 1

    while power <= j:

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

        power *= 9

print((dp[n]))

n = int(eval(input()))

dp = [i for i in range(n + 1)]

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

    pow = 6

    while i - pow > -1:

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

        pow *= 6

    pow = 9

    while i - pow > -1:

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

        pow *= 9

# print(dp)

print((dp[n]))
n = int(eval(input()))
dp = [i for i in range(n + 1)]  # dp table 最初は全て1円で払う
for j in range(1, n + 1):
    power = 1
    while power <= j:
        dp[j] = min(dp[j], dp[j - power] + 1)
        power *= 6
    power = 1
    while power <= j:
        dp[j] = min(dp[j], dp[j - power] + 1)
        power *= 9
print((dp[n]))

n = int(eval(input()))
dp = [i for i in range(n + 1)]
for i in range(1, n + 1):
    pow = 6
    while i - pow > -1:
        dp[i] = min(dp[i], dp[i - pow] + 1)
        pow *= 6
    pow = 9
    while i - pow > -1:
        dp[i] = min(dp[i], dp[i - pow] + 1)
        pow *= 9
# print(dp)
print((dp[n]))

-dp = [i for i in range(n + 1)]  # dp table 最初は全て1円で払う -for j in range(1, n + 1): -    power = 1 -    while power <= j: -        dp[j] = min(dp[j], dp[j - power] + 1) -        power *= 6 -    power = 1 -    while power <= j: -        dp[j] = min(dp[j], dp[j - power] + 1) -        power *= 9 +dp = [i for i in range(n + 1)] +for i in range(1, n + 1): +    pow = 6 +    while i - pow > -1: +        dp[i] = min(dp[i], dp[i - pow] + 1) +        pow *= 6 +    pow = 9 +    while i - pow > -1: +        dp[i] = min(dp[i], dp[i - pow] + 1) +        pow *= 9 +# print(dp)