n = int(eval(input()))

dp = [200000] * (n+10)

dp[0] = 0

# dp[1] = 1

for i in range(n):

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

    six = 6

    while i + six <= n:

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

        six *= 6 



    nine = 9

    while i + nine <= n:

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

        nine *= 9



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

dp = [200000] * (n+10)

dp[0] = 0

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

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

    

    six = 6

    while i - six >= 0:

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

        six *= 6 



    nine = 9

    while i - nine >= 0:

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

        nine *= 9



print((dp[n]))
n = int(eval(input()))
dp = [200000] * (n + 10)
dp[0] = 0
# dp[1] = 1
for i in range(n):
    dp[i + 1] = min(dp[i + 1], dp[i] + 1)
    six = 6
    while i + six <= n:
        dp[i + six] = min(dp[i + six], dp[i] + 1)
        six *= 6
    nine = 9
    while i + nine <= n:
        dp[i + nine] = min(dp[i + nine], dp[i] + 1)
        nine *= 9
print((dp[n]))

n = int(eval(input()))
dp = [200000] * (n + 10)
dp[0] = 0
for i in range(1, n + 1):
    dp[i] = min(dp[i - 1] + 1, dp[i])
    six = 6
    while i - six >= 0:
        dp[i] = min(dp[i - six] + 1, dp[i])
        six *= 6
    nine = 9
    while i - nine >= 0:
        dp[i] = min(dp[i - nine] + 1, dp[i])
        nine *= 9
print((dp[n]))

-# dp[1] = 1 -for i in range(n): -    dp[i + 1] = min(dp[i + 1], dp[i] + 1) +for i in range(1, n + 1): +    dp[i] = min(dp[i - 1] + 1, dp[i]) -    while i + six <= n: -        dp[i + six] = min(dp[i + six], dp[i] + 1) +    while i - six >= 0: +        dp[i] = min(dp[i - six] + 1, dp[i]) -    while i + nine <= n: -        dp[i + nine] = min(dp[i + nine], dp[i] + 1) +    while i - nine >= 0: +        dp[i] = min(dp[i - nine] + 1, dp[i])