MAX_N = 10**5



N = int(eval(input()))



dp = [MAX_N]*(N+1)

dp[0] = 0

dp[1] = 1



MONEY_LIST = [6**i for i in range(7)]

MONEY_LIST.extend([9**i for i in range(1,6)])

MONEY_LIST.sort()



for i in range(N+1):

    for m in MONEY_LIST:

        if m>i:

            break

        

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

        

print((dp[N]))
from itertools import count, takewhile



def dfs(n:int, d={}) -> int:

    if n<6:

        return n

    

    if n in d:

        return d[n]

    

    max9 = max(takewhile(lambda x: x<=n, (9**i for i in count())))

    max6 = max(takewhile(lambda x: x<=n, (6**i for i in count())))

    

    d[n] = min(dfs(n-max9), dfs(n-max6))+1

    

    return d[n]



print((dfs(int(eval(input())))))
MAX_N = 10**5
N = int(eval(input()))
dp = [MAX_N] * (N + 1)
dp[0] = 0
dp[1] = 1
MONEY_LIST = [6**i for i in range(7)]
MONEY_LIST.extend([9**i for i in range(1, 6)])
MONEY_LIST.sort()
for i in range(N + 1):
    for m in MONEY_LIST:
        if m > i:
            break
        dp[i] = min(dp[i], dp[i - m] + 1)
print((dp[N]))

from itertools import count, takewhile


def dfs(n: int, d={}) -> int:
    if n < 6:
        return n
    if n in d:
        return d[n]
    max9 = max(takewhile(lambda x: x <= n, (9**i for i in count())))
    max6 = max(takewhile(lambda x: x <= n, (6**i for i in count())))
    d[n] = min(dfs(n - max9), dfs(n - max6)) + 1
    return d[n]


print((dfs(int(eval(input())))))

-MAX_N = 10**5 -N = int(eval(input())) -dp = [MAX_N] * (N + 1) -dp[0] = 0 -dp[1] = 1 -MONEY_LIST = [6**i for i in range(7)] -MONEY_LIST.extend([9**i for i in range(1, 6)]) -MONEY_LIST.sort() -for i in range(N + 1): -    for m in MONEY_LIST: -        if m > i: -            break -        dp[i] = min(dp[i], dp[i - m] + 1) -print((dp[N])) +from itertools import count, takewhile + + +def dfs(n: int, d={}) -> int: +    if n < 6: +        return n +    if n in d: +        return d[n] +    max9 = max(takewhile(lambda x: x <= n, (9**i for i in count()))) +    max6 = max(takewhile(lambda x: x <= n, (6**i for i in count()))) +    d[n] = min(dfs(n - max9), dfs(n - max6)) + 1 +    return d[n] + + +print((dfs(int(eval(input())))))