N = int(eval(input()))



s = set()

a = 1

while a <= 100000:

    s.add(a)

    a *= 6

a = 1

while a <= 100000:

    s.add(a)

    a *= 9

s = sorted(s)



INF = float('inf')

dp = [INF] * (N+1)

dp[0] = 0

for i in range(N):

    for a in s:

        if i+a > N: break

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

print((dp[N]))
import sys

sys.setrecursionlimit(10**8)



N = int(eval(input()))



ss = set()

n = 6

while n <= 10**5:

    ss.add(n)

    n *= 6

n = 9

while n <= 10**5:

    ss.add(n)

    n *= 9



dp = [None] * (N+1)



def rec(n):

    if dp[n] is not None:

        return dp[n]

    tmp = n

    for s in ss:

        if s > n: continue

        if s == n:

            tmp = 1

            break

        tmp = min(tmp, 1 + rec(n-s))

    dp[n] = tmp

    return tmp



print((rec(N)))
N = int(eval(input()))
s = set()
a = 1
while a <= 100000:
    s.add(a)
    a *= 6
a = 1
while a <= 100000:
    s.add(a)
    a *= 9
s = sorted(s)
INF = float("inf")
dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    for a in s:
        if i + a > N:
            break
        dp[i + a] = min(dp[i + a], dp[i] + 1)
print((dp[N]))

import sys

sys.setrecursionlimit(10**8)
N = int(eval(input()))
ss = set()
n = 6
while n <= 10**5:
    ss.add(n)
    n *= 6
n = 9
while n <= 10**5:
    ss.add(n)
    n *= 9
dp = [None] * (N + 1)


def rec(n):
    if dp[n] is not None:
        return dp[n]
    tmp = n
    for s in ss:
        if s > n:
            continue
        if s == n:
            tmp = 1
            break
        tmp = min(tmp, 1 + rec(n - s))
    dp[n] = tmp
    return tmp


print((rec(N)))

+import sys + +sys.setrecursionlimit(10**8) -s = set() -a = 1 -while a <= 100000: -    s.add(a) -    a *= 6 -a = 1 -while a <= 100000: -    s.add(a) -    a *= 9 -s = sorted(s) -INF = float("inf") -dp = [INF] * (N + 1) -dp[0] = 0 -for i in range(N): -    for a in s: -        if i + a > N: +ss = set() +n = 6 +while n <= 10**5: +    ss.add(n) +    n *= 6 +n = 9 +while n <= 10**5: +    ss.add(n) +    n *= 9 +dp = [None] * (N + 1) + + +def rec(n): +    if dp[n] is not None: +        return dp[n] +    tmp = n +    for s in ss: +        if s > n: +            continue +        if s == n: +            tmp = 1 -        dp[i + a] = min(dp[i + a], dp[i] + 1) -print((dp[N])) +        tmp = min(tmp, 1 + rec(n - s)) +    dp[n] = tmp +    return tmp + + +print((rec(N)))