N = int(eval(input()))

A = [1]

for k in range(1,7):

    A.append(6**k)

for k in range(1,6):

    A.append(9**k)

dp = [[0 for _ in range(len(A))] for _ in range(N+1)]

for i in range(N+1):

    dp[i][0] = i

for j in range(len(A)):

    dp[1][j] = 1

for i in range(2,N+1):

    for j in range(1,len(A)):

        if i>=A[j]:

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

        else:

            dp[i][j] = dp[i][j-1]

print((dp[N][len(A)-1]))
memo = {}

def f(x):

    if 0<=x<=5:

        return x

    if x in memo:

        return memo[x]

    k = 0

    while 9**k<=x:

        k += 1

    m = 0

    while 6**m<=x:

        m += 1

    memo[x] = min(f(x-9**(k-1))+1,f(x-6**(m-1))+1)

    return memo[x]

print((f(int(eval(input())))))    
N = int(eval(input()))
A = [1]
for k in range(1, 7):
    A.append(6**k)
for k in range(1, 6):
    A.append(9**k)
dp = [[0 for _ in range(len(A))] for _ in range(N + 1)]
for i in range(N + 1):
    dp[i][0] = i
for j in range(len(A)):
    dp[1][j] = 1
for i in range(2, N + 1):
    for j in range(1, len(A)):
        if i >= A[j]:
            dp[i][j] = min(dp[i][j - 1], dp[i - A[j]][j] + 1)
        else:
            dp[i][j] = dp[i][j - 1]
print((dp[N][len(A) - 1]))

memo = {}


def f(x):
    if 0 <= x <= 5:
        return x
    if x in memo:
        return memo[x]
    k = 0
    while 9**k <= x:
        k += 1
    m = 0
    while 6**m <= x:
        m += 1
    memo[x] = min(f(x - 9 ** (k - 1)) + 1, f(x - 6 ** (m - 1)) + 1)
    return memo[x]


print((f(int(eval(input())))))

-N = int(eval(input())) -A = [1] -for k in range(1, 7): -    A.append(6**k) -for k in range(1, 6): -    A.append(9**k) -dp = [[0 for _ in range(len(A))] for _ in range(N + 1)] -for i in range(N + 1): -    dp[i][0] = i -for j in range(len(A)): -    dp[1][j] = 1 -for i in range(2, N + 1): -    for j in range(1, len(A)): -        if i >= A[j]: -            dp[i][j] = min(dp[i][j - 1], dp[i - A[j]][j] + 1) -        else: -            dp[i][j] = dp[i][j - 1] -print((dp[N][len(A) - 1])) +memo = {} + + +def f(x): +    if 0 <= x <= 5: +        return x +    if x in memo: +        return memo[x] +    k = 0 +    while 9**k <= x: +        k += 1 +    m = 0 +    while 6**m <= x: +        m += 1 +    memo[x] = min(f(x - 9 ** (k - 1)) + 1, f(x - 6 ** (m - 1)) + 1) +    return memo[x] + + +print((f(int(eval(input())))))