import sys

sys.setrecursionlimit(10**7)

N = int(eval(input()))

memo = [-1]*(N+1)

def rec(n):

    if n == 0:

        return 0

    if memo[n] != -1:

        return memo[n]

    res = n

    m = 1

    while m <= n:

        res = min(res,rec(n-m)+1)

        m *= 6

    m = 1

    while m <= n:

        res = min(res,rec(n-m)+1)

        m *= 9

    memo[n] = res

    return memo[n]



print((rec(N)))
N = int(eval(input()))

dp = [float('inf')]*(N+1)

dp[0] = 0

for i in range(N+1):

    if i+1 <= N:

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

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

        if i + 6**j > N:

            break

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

    for k in range(1,N+1):

        if i + 9**k > N:

            break

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

print((dp[N]))
import sys

sys.setrecursionlimit(10**7)
N = int(eval(input()))
memo = [-1] * (N + 1)


def rec(n):
    if n == 0:
        return 0
    if memo[n] != -1:
        return memo[n]
    res = n
    m = 1
    while m <= n:
        res = min(res, rec(n - m) + 1)
        m *= 6
    m = 1
    while m <= n:
        res = min(res, rec(n - m) + 1)
        m *= 9
    memo[n] = res
    return memo[n]


print((rec(N)))

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

-import sys - -sys.setrecursionlimit(10**7) -memo = [-1] * (N + 1) - - -def rec(n): -    if n == 0: -        return 0 -    if memo[n] != -1: -        return memo[n] -    res = n -    m = 1 -    while m <= n: -        res = min(res, rec(n - m) + 1) -        m *= 6 -    m = 1 -    while m <= n: -        res = min(res, rec(n - m) + 1) -        m *= 9 -    memo[n] = res -    return memo[n] - - -print((rec(N))) +dp = [float("inf")] * (N + 1) +dp[0] = 0 +for i in range(N + 1): +    if i + 1 <= N: +        dp[i + 1] = min(dp[i + 1], dp[i] + 1) +    for j in range(1, N + 1): +        if i + 6**j > N: +            break +        dp[i + 6**j] = min(dp[i + 6**j], dp[i] + 1) +    for k in range(1, N + 1): +        if i + 9**k > N: +            break +        dp[i + 9**k] = min(dp[i + 9**k], dp[i] + 1) +print((dp[N]))