n = int(eval(input()))

dp = [float("inf")]*(n+1)

dp[0] = 0

for i in range(n):

	j = 0

	while i+6**j <= n:

		dp[i+6**j] = min(dp[i+6**j],dp[i]+1)

		j += 1

	k = 0

	while i+9**k <= n:

		dp[i+9**k] = min(dp[i+9**k],dp[i]+1)

		k += 1

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

dp = [10**9+7]*(n+1)

dp[0] = 0

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

    dp[i] = dp[i-1] + 1

    j = 6

    while i-j >= 0:

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

        j *= 6

    k = 9

    while i-k >= 0:

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

        k *= 9

print((dp[n]))
n = int(eval(input()))
dp = [float("inf")] * (n + 1)
dp[0] = 0
for i in range(n):
    j = 0
    while i + 6**j <= n:
        dp[i + 6**j] = min(dp[i + 6**j], dp[i] + 1)
        j += 1
    k = 0
    while i + 9**k <= n:
        dp[i + 9**k] = min(dp[i + 9**k], dp[i] + 1)
        k += 1
print((dp[n]))

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

-dp = [float("inf")] * (n + 1) +dp = [10**9 + 7] * (n + 1) -for i in range(n): -    j = 0 -    while i + 6**j <= n: -        dp[i + 6**j] = min(dp[i + 6**j], dp[i] + 1) -        j += 1 -    k = 0 -    while i + 9**k <= n: -        dp[i + 9**k] = min(dp[i + 9**k], dp[i] + 1) -        k += 1 +for i in range(1, n + 1): +    dp[i] = dp[i - 1] + 1 +    j = 6 +    while i - j >= 0: +        dp[i] = min(dp[i], dp[i - j] + 1) +        j *= 6 +    k = 9 +    while i - k >= 0: +        dp[i] = min(dp[i], dp[i - k] + 1) +        k *= 9