from collections import Counter,defaultdict

import sys,heapq,bisect,math,itertools,string,queue,datetime

mod = 10**9+7

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))

def inpl_str(): return list(sys.stdin.readline().split())



n = inp()

dp = [mod for i in range(2*n)]

dp[0] = 0

p_6 = []

p_9 = []

tmp = 1

while tmp <= n:

    p_6.append(tmp)

    tmp *= 6

tmp = 1

while tmp <= n:

    p_9.append(tmp)

    tmp *= 9





for i in range(n):

    for j in p_6:

        if i+j <= n:

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

    for k in p_9:

        if i+k <= n:

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



print((dp[n]))

from collections import Counter,defaultdict,deque

from heapq import heappop,heappush,heapify

import sys,bisect,math,itertools,fractions

sys.setrecursionlimit(10**8)

mod = 10**9+7

INF = float('inf')

def inp(): return int(sys.stdin.readline())

def inpl(): return list(map(int, sys.stdin.readline().split()))



n = inp()

res = [INF] * (n+1)

res[0] = 0

ge = [1]

for i in [6,9]:

    now = 1

    while True:

        now *= i

        if now > n:

            break

        ge.append(now)

ge.sort()

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

    for x in ge:

        if x > i:

            continue

        res[i] = min(res[i], res[i-x]+1)

print((res[n]))
from collections import Counter, defaultdict
import sys, heapq, bisect, math, itertools, string, queue, datetime

mod = 10**9 + 7


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


def inpl_str():
    return list(sys.stdin.readline().split())


n = inp()
dp = [mod for i in range(2 * n)]
dp[0] = 0
p_6 = []
p_9 = []
tmp = 1
while tmp <= n:
    p_6.append(tmp)
    tmp *= 6
tmp = 1
while tmp <= n:
    p_9.append(tmp)
    tmp *= 9
for i in range(n):
    for j in p_6:
        if i + j <= n:
            dp[i + j] = min(dp[i + j], dp[i] + 1)
    for k in p_9:
        if i + k <= n:
            dp[i + k] = min(dp[i + k], dp[i] + 1)
print((dp[n]))

from collections import Counter, defaultdict, deque
from heapq import heappop, heappush, heapify
import sys, bisect, math, itertools, fractions

sys.setrecursionlimit(10**8)
mod = 10**9 + 7
INF = float("inf")


def inp():
    return int(sys.stdin.readline())


def inpl():
    return list(map(int, sys.stdin.readline().split()))


n = inp()
res = [INF] * (n + 1)
res[0] = 0
ge = [1]
for i in [6, 9]:
    now = 1
    while True:
        now *= i
        if now > n:
            break
        ge.append(now)
ge.sort()
for i in range(1, n + 1):
    for x in ge:
        if x > i:
            continue
        res[i] = min(res[i], res[i - x] + 1)
print((res[n]))

-from collections import Counter, defaultdict -import sys, heapq, bisect, math, itertools, string, queue, datetime +from collections import Counter, defaultdict, deque +from heapq import heappop, heappush, heapify +import sys, bisect, math, itertools, fractions +sys.setrecursionlimit(10**8) +INF = float("inf") -def inpl_str(): -    return list(sys.stdin.readline().split()) - - -dp = [mod for i in range(2 * n)] -dp[0] = 0 -p_6 = [] -p_9 = [] -tmp = 1 -while tmp <= n: -    p_6.append(tmp) -    tmp *= 6 -tmp = 1 -while tmp <= n: -    p_9.append(tmp) -    tmp *= 9 -for i in range(n): -    for j in p_6: -        if i + j <= n: -            dp[i + j] = min(dp[i + j], dp[i] + 1) -    for k in p_9: -        if i + k <= n: -            dp[i + k] = min(dp[i + k], dp[i] + 1) -print((dp[n])) +res = [INF] * (n + 1) +res[0] = 0 +ge = [1] +for i in [6, 9]: +    now = 1 +    while True: +        now *= i +        if now > n: +            break +        ge.append(now) +ge.sort() +for i in range(1, n + 1): +    for x in ge: +        if x > i: +            continue +        res[i] = min(res[i], res[i - x] + 1) +print((res[n]))