n = int(eval(input()))

dp = [1000000] * 100010

dp[0] = 0

for i in range(n):

    a = 1

    while i + a <= 100000:

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

        a *= 6

    b = 1

    while i + b <= 100000:

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

        b *= 9

print((dp[n]))

n = int(eval(input()))

dp = [1000000] * 100010

dp[0] = 0

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

    a = 1

    while a <= i:

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

        a *= 6

    b = 1

    while b <= i:

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

        b *= 9

print((dp[n]))

n = int(eval(input()))
dp = [1000000] * 100010
dp[0] = 0
for i in range(n):
    a = 1
    while i + a <= 100000:
        dp[i + a] = min(dp[i + a], dp[i] + 1)
        a *= 6
    b = 1
    while i + b <= 100000:
        dp[i + b] = min(dp[i + b], dp[i] + 1)
        b *= 9
print((dp[n]))

n = int(eval(input()))
dp = [1000000] * 100010
dp[0] = 0
for i in range(1, n + 1):
    a = 1
    while a <= i:
        dp[i] = min(dp[i], dp[i - a] + 1)
        a *= 6
    b = 1
    while b <= i:
        dp[i] = min(dp[i], dp[i - b] + 1)
        b *= 9
print((dp[n]))

-for i in range(n): +for i in range(1, n + 1): -    while i + a <= 100000: -        dp[i + a] = min(dp[i + a], dp[i] + 1) +    while a <= i: +        dp[i] = min(dp[i], dp[i - a] + 1) -    while i + b <= 100000: -        dp[i + b] = min(dp[i + b], dp[i] + 1) +    while b <= i: +        dp[i] = min(dp[i], dp[i - b] + 1)