import heapq

N=int(eval(input()))

N_max=100000



d=set()

n=1

while n<=N_max:

    d.add(n)

    n*=6

n=1

while n<=N_max:

    d.add(n)

    n*=9



dp=[N_max+1 for _ in range(N_max+1)]

dp[0]=0

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

    for n in d:

        if i>=n:

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



print((dp[N]))
N = int(eval(input()))



a = [1]

n6 = 6

while n6 <= 100000:

    a.append(n6)

    n6 *= 6

n9 = 9

while n9 <= 100000:

    a.append(n9)

    n9 *= 9



#dp[i]=ちょうどi円払う場合に最小の操作回数

dp = [10**9] * (N+100010)

dp[0] = 0



for i in range(N):

    for n in a:

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

print((dp[N]))
import heapq

N = int(eval(input()))
N_max = 100000
d = set()
n = 1
while n <= N_max:
    d.add(n)
    n *= 6
n = 1
while n <= N_max:
    d.add(n)
    n *= 9
dp = [N_max + 1 for _ in range(N_max + 1)]
dp[0] = 0
for i in range(1, N_max + 1):
    for n in d:
        if i >= n:
            dp[i] = min(dp[i], dp[i - n] + 1)
print((dp[N]))

N = int(eval(input()))
a = [1]
n6 = 6
while n6 <= 100000:
    a.append(n6)
    n6 *= 6
n9 = 9
while n9 <= 100000:
    a.append(n9)
    n9 *= 9
# dp[i]=ちょうどi円払う場合に最小の操作回数
dp = [10**9] * (N + 100010)
dp[0] = 0
for i in range(N):
    for n in a:
        dp[i + n] = min(dp[i + n], dp[i] + 1)
print((dp[N]))

-import heapq - -N_max = 100000 -d = set() -n = 1 -while n <= N_max: -    d.add(n) -    n *= 6 -n = 1 -while n <= N_max: -    d.add(n) -    n *= 9 -dp = [N_max + 1 for _ in range(N_max + 1)] +a = [1] +n6 = 6 +while n6 <= 100000: +    a.append(n6) +    n6 *= 6 +n9 = 9 +while n9 <= 100000: +    a.append(n9) +    n9 *= 9 +# dp[i]=ちょうどi円払う場合に最小の操作回数 +dp = [10**9] * (N + 100010) -for i in range(1, N_max + 1): -    for n in d: -        if i >= n: -            dp[i] = min(dp[i], dp[i - n] + 1) +for i in range(N): +    for n in a: +        dp[i + n] = min(dp[i + n], dp[i] + 1)