CodeEff/ECCO huggingface dataset:
# Original solution
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    return

#B
def B():
    return

#C
def C():
    n = I()
    ans = float("inf")
    for i in range(n+1):
        m = 0
        k = i
        while k > 0:
            m += k%6
            k //= 6
        k = n-i
        while k > 0:
            m += k%9
            k //= 9
        ans = min(ans,m)
    print(ans)
#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()


# Optimized solution
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    n = I()
    ans = float("inf")
    for i in range(n+1):
        m = 0
        k = i
        while k > 0:
            m += k%6
            k //= 6
        k = n-i
        while k > 0:
            m += k%9
            k //= 9
        if m < ans:
            ans = m
    print(ans)
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#I
def I_():
    return

#J
def J():
    return

#Solve
if __name__ == "__main__":
    A()
# Original solution
# -*- coding: utf-8 -*-

N = int(eval(input()))

# 解説動画通りにDPしてみる

dp = [0] * 100001
# dp[0] = 0

for n in range(1, 100001):
    dp[n] = float("inf")
    power = 1
    while power <= n:
        dp[n] = min(dp[n], dp[n-power] + 1)
        power *= 6
    power = 1
    while power <= n:
        dp[n] = min(dp[n], dp[n-power] + 1)
        power *= 9

print((dp[N]))

# Optimized solution
# -*- coding: utf-8 -*-

N = int(eval(input()))

# ちょっと自分でいじった版

dp = [0] * (N+1)

for n in range(1, N+1):
    dp[n] = float("inf")
    power = 1
    while power <= n:
        dp[n] = min(dp[n], dp[n-power] + 1)
        power *= 6
    power = 1
    while power <= n:
        dp[n] = min(dp[n], dp[n-power] + 1)
        power *= 9

print((dp[N]))
# Original solution
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

# dp[i] := i円引き出すのに必要な最小回数
dp = [INF] * (N+1)
dp[0] = 0
for i in range(N):
    # 3種類の引き出し方に対応する遷移をする
    dp[i+1] = min(dp[i+1], dp[i] + 1)
    j = 1
    while i+6**j <= N:
        dp[i+6**j] = min(dp[i+6**j], dp[i] + 1)
        j += 1
    j = 1
    while i+9**j <= N:
        dp[i+9**j] = min(dp[i+9**j], dp[i] + 1)
        j += 1
print((dp[N]))


# Optimized solution
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N = INT()

# dp[i] := i円引き出すのに必要な最小回数
dp = [INF] * (N+1)
dp[0] = 0
for i in range(N):
    # 3種類の引き出し方に対応する遷移をする
    dp[i+1] = min(dp[i+1], dp[i] + 1)
    j = 6
    while i+j <= N:
        dp[i+j] = min(dp[i+j], dp[i] + 1)
        j *= 6
    j = 9
    while i+j <= N:
        dp[i+j] = min(dp[i+j], dp[i] + 1)
        j *= 9
print((dp[N]))
# Original solution
from itertools import count, takewhile

def dfs(n):
  if n < 6:
    return 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())))
  return min(dfs(n-max9), dfs(n-max6))+1

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

# Optimized solution
from itertools import count, takewhile
 
def dfs(n, d={}):
  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())))))
# Original solution
from itertools import count, takewhile
from functools import lru_cache
 
@lru_cache(maxsize=None)
def dfs(n):
  if n == 0: return 0
  
  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())))
  return min(dfs(n-max9), dfs(n-max6))+1
 
print((dfs(int(eval(input())))))

# Optimized solution
from itertools import count, takewhile, dropwhile

def dfs(n, max9, max6, d={0:0}):
  if n in d: return d[n]
  
  max9 = next(dropwhile(lambda x: x>n, (max9 / 9**i for i in count())))
  max6 = next(dropwhile(lambda x: x>n, (max6 / 6**i for i in count())))
  d[n] = min(dfs(n-max9, max9, max6), dfs(n-max6, max9, max6)) + 1
  return d[n]

n = int(eval(input()))

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())))

print((dfs(n, max9, max6)))
# Original solution
import sys
six = [6,36,216,1296,7776,46656]
nine = [9,81,729,6561,59049]

def dfs(n):
    if n < 6:
        return n
    six_max, nine_max = (1, 1)

    for i in six:
        if n < i:
            break
        six_max = i
    
    for i in nine:
        if n < i:
            break
        nine_max = i
    
    ans = 123
    
    if nine_max != 1:
        ans = dfs(n - nine_max) + 1
    if six_max != 1:
        ans = min(ans, dfs(n - six_max) + 1)
    return ans

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


# Optimized solution
def dfs(n):
    if n < 6:
        return n
    elif n < 9:
        return 1 + dfs(n - 6)
    
    six_max = 1
    nine_max = 1
    while six_max * 6 <= n:
        six_max *= 6
    
    while nine_max * 9 <= n:
        nine_max *= 9
    
    return min(dfs(n - nine_max), dfs(n - six_max)) + 1

print((dfs(int(eval(input())))))
# Original solution
n = int(eval(input()))

INF = float("inf")

dp = [INF]*100010
dp[0] = 0

pow6 = [6**i for i in range(7)]
pow9 = [9**i for i in range(6)]

for i in range(1,n+1):
    for p6 in pow6:
        dp[i] = min(dp[i],dp[i-p6]+1)
    for p9 in pow9:
        dp[i] = min(dp[i],dp[i-p9]+1)
print((dp[n]))

# Optimized solution
n = int(eval(input()))

INF = float("inf")

dp = [INF]*100010
dp[0] = 0

pow6 = [6**i for i in range(1,7)]
pow9 = [9**i for i in range(1,6)]

for i in range(1,n+1):
    dp[i] = dp[i-1] + 1
    for p6 in pow6:
        dp[i] = min(dp[i],dp[i-p6]+1)
    for p9 in pow9:
        dp[i] = min(dp[i],dp[i-p9]+1)

print((dp[n]))
# Original solution
n = int(eval(input()))

INF = float("inf")

dp = [INF]*100010
dp[0] = 0

pow6 = [6**i for i in range(1,7)]
pow9 = [9**i for i in range(1,6)]

for i in range(1,n+1):
    dp[i] = dp[i-1] + 1
    for p6 in pow6:
        dp[i] = min(dp[i],dp[i-p6]+1)
    for p9 in pow9:
        dp[i] = min(dp[i],dp[i-p9]+1)

print((dp[n]))

# Optimized solution
n = int(eval(input()))

dp = [n]*100010
dp[0] = 0
pow69 = [6**i for i in range(1,7)] + [9**i for i in range(1,6)]

for i in range(1,n+1):
    dp[i] = dp[i-1] + 1
    for p in pow69:
        dp[i] = min(dp[i],dp[i-p]+1)

print((dp[n]))
# Original solution
import math

N = int(eval(input()))
# A = [int(x) for x in input().split()]


C = [1]
for i in range(math.ceil(math.log(N,9)) + 3):
    if i !=0 :
        C.append(9 ** i)
for i in range(math.ceil(math.log(N,6)) + 3):
    if i != 0:
        C.append(6 ** i)


INF = 10**10
dp = [INF] * (N+1)
dp[0] = 0

for i in range(1,N+1):
    for c in C:
        if i - c >= 0:
            dp[i] = min(dp[i], dp[i - c] + 1)

print((dp[N]))



# Optimized solution
import math

N = int(eval(input()))
# A = [int(x) for x in input().split()]


C = [1]
for i in range(math.floor(math.log(N,9)) + 2):
    if i !=0 :
        C.append(9 ** i)
for i in range(math.floor(math.log(N,6)) + 2):
    if i != 0:
        C.append(6 ** i)


INF = 10**10
dp = [INF] * (N+1)
dp[0] = 0

for i in range(1,N+1):
    for c in C:
        if i - c >= 0:
            dp[i] = min(dp[i], dp[i - c] + 1)

print((dp[N]))
# Original solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def ret_ans(n):
    li = [1]
    li.extend([6 ** i for i in range(0, 15) if 6 ** i <= n])
    li.extend([9 ** i for i in range(1, 15) if 9 ** i <= n])
    li.sort(reverse=True)
    
    s = set([n])
    cnt = 1
    while True:
        ts = set(s)
        for e in li:
            for c in s:
                if c - e == 0:
                    return cnt
                if c - e > 0:
                    ts.add(c - e)
        s = ts
        cnt += 1
 

def solve():
    print((ret_ans(int(eval(input())))))

if __name__=="__main__":
    solve()


# Optimized solution
#!/usr/bin/env python
# -*- coding: utf-8 -*-

def ret_ans(n):
    li = [1]
    li.extend([6 ** i for i in range(1, 15) if 6 ** i <= n])
    li.extend([9 ** i for i in range(1, 15) if 9 ** i <= n])
    li.sort(reverse=True)
    
    s = set([n])
    cnt = 1
    while True:
        ts = set(s)
        for e in li:
            for c in s:
                if c - e == 0:
                    return cnt
                if c - e > 0:
                    ts.add(c - e)
        s = ts
        cnt += 1
 

def solve():
    print((ret_ans(int(eval(input())))))

if __name__=="__main__":
    solve()
# Original solution
N=int(eval(input()))
inf=100000
dp=[inf for _ in range(N+1)]
dp[0]=0
for n in range(1,N+1):
    num=100000
    i=0
    j=0
    while 9**(i+1)<=n:
        i+=1
    while 6**(j+1)<=n:
        j+=1
    for k in range(i+1):
        num=min(num,dp[n-9**k]+1)
    for k in range(j+1):
        num=min(num,dp[n-6**k]+1)
    dp[n]=min(dp[n],num)
print((dp[N]))

# Optimized solution
def main3():
    import sys
    sys.setrecursionlimit(100000000)

    N=int(eval(input()))
    dp=[-1 for n in range(N+1)]
    dp[0]=0
    dp[1]=1
    def rec(x,dp):
        if x==1:
            return dp[1]
        elif x==0:
            return dp[0]
        else:
            l=[]
            i=0
            j=0
            while 9**(i+1)<=x:
                i+=1
            while 6**(j+1)<=x:
                j+=1
            for k in reversed(list(range(i+1))):
                tmp=x-9**k
                if dp[tmp]!=-1:
                    l.append(dp[tmp]+1)
                else:
                    l.append(rec(tmp,dp)+1)
            for k in reversed(list(range(j+1))):
                tmp=x-6**k
                if dp[tmp]!=-1:
                    l.append(dp[tmp]+1)
                else:
                    l.append(rec(tmp,dp)+1)
            dp[x]=min(l)
            return dp[x]

    return rec(N,dp)
        
if __name__=="__main__":
    print((main3()))
# Original solution
n = int(eval(input()))
dp = [float("Inf")]*(n+1)
dp[0] = 0
for i in range(n):
    for j in range(1,6):
        if i + j > n:
            break
        dp[i+j] = min(dp[i+j], dp[i] + j)
    for j in range(1,n//6+1):
        if i + 6**j > n:
            break
        dp[i+6**j] = min(dp[i+6**j], dp[i] + 1)
    for j in range(1,n//9+1):
        if i + 9**j > n:
            break
        dp[i+9**j] = min(dp[i+9**j], dp[i] + 1)
print((dp[-1]))

# Optimized solution
n = int(eval(input()))
dp = [float("Inf")]*(n+1)
for i in range(min(6, n+1)):
    dp[i] = i
for i in range(n):
    for j in range(1,n//6+1):
        if i + 6**j > n:
            break
        dp[i+6**j] = min(dp[i+6**j], dp[i] + 1)
    for j in range(1,n//9+1):
        if i + 9**j > n:
            break
        dp[i+9**j] = min(dp[i+9**j], dp[i] + 1)
print((dp[-1]))
# Original solution
N = int(eval(input()))

l = [1] + [6 ** x for x in range(1, 7)] + [9 ** x for x in range(1, 6)]
l.sort()

INF = 10 ** 3
dp = [0] + [INF] * N

for n in range(1, N + 1):
	#for f in [1, 6, 9, 6 ** 2, 9 ** 2, 6 ** 3, 9 ** 3]:
	for f in l:
		if 0 <= n - f:
			dp[n] = min([dp[n], dp[n - f] + 1])
			#print(dp)
print((dp[-1]))


# Optimized solution
N = int(eval(input()))

l = [1] + [6 ** x for x in range(1, 7)] + [9 ** x for x in range(1, 6)]
l.sort()

INF = 10 ** 3
dp = [0] * (N + 1)

for n in range(1, N + 1):
	dp[n] = min([dp[n - f] + 1 if 0 <= n - f else INF for f in l])
	#print(dp)
print((dp[-1]))
# Original solution
import sys

def f(n):
  X6 = []
  X9 = []
  for i in range(1, 10):
    if n >= 6**i:
      X6.append(6**i)
    if n >= 9**i:
      X9.append(9**i)
  return X6, X9


def main():
  input = sys.stdin.readline
  N = int(eval(input()))
  dp = [0 for _ in range(N+1)]
  for i in range(1, N+1):
    can = []
    X6, X9 = f(i)
    X = [1] + X6 + X9
    for x in X:
      can.append(dp[i-x] + 1)
    dp[i] = min(can)
  print((dp[N]))

if __name__ == '__main__':
  main()

# Optimized solution
import sys

def f(n):
  X6 = []
  X9 = []
  for i in range(0, 10):
    if n >= 6**i:
      X6.append(6**i)
    if n >= 9**i:
      X9.append(9**i)
  return max(X6), max(X9)


def main():
  input = sys.stdin.readline
  N = int(eval(input()))
  dp = [0 for _ in range(N+1)]
  for i in range(1, N+1):
    can = []
    max6, max9 = f(i)
    X = [1, max6, max9]
    for x in X:
      can.append(dp[i-x] + 1)
    dp[i] = min(can)
  print((dp[N]))

if __name__ == '__main__':
  main()
# Original solution
import sys

def f(n):
  X6 = []
  X9 = []
  for i in range(0, 10):
    if n >= 6**i:
      X6.append(6**i)
    if n >= 9**i:
      X9.append(9**i)
  return max(X6), max(X9)


def main():
  input = sys.stdin.readline
  N = int(eval(input()))
  dp = [0 for _ in range(N+1)]
  for i in range(1, N+1):
    can = []
    max6, max9 = f(i)
    X = [1, max6, max9]
    for x in X:
      can.append(dp[i-x] + 1)
    dp[i] = min(can)
  print((dp[N]))

if __name__ == '__main__':
  main()

# Optimized solution
import sys
from itertools import count, takewhile

def dfs(n, d={}):
  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]


def main():
  input = sys.stdin.readline
  N = int(eval(input()))
  print((dfs(N)))
  
if __name__ == '__main__':
  main()
# Original solution
N=int(eval(input()))
A=[]
for i in range(1,10):
    if 6**i<=100000:
        A.append(6**i)
    if 9**i<=100000:
        A.append(9**i)
L=[i for i in range(100001)]
A.sort()

for i in range(len(A)):
    for j in range(1,100001):
        if j>=A[i]:
            L[j]=min(L[j],L[j-A[i]]+1)
print((L[N]))

# Optimized solution
N=int(eval(input()))

C=[1]
for i in range(1,100):
    if 6**i<=100000:
        C.append(6**i)
    else:
        break
    
for i in range(1,100):
    if 9**i<=100000:
        C.append(9**i)
    else:
        break

dp=[10**6 for i in range(N+1)]
dp[0]=0
for i in C:
    for j in range(1,len(dp)):
        if j-i>=0:
            dp[j]=min(dp[j],dp[j-i]+1)
print((dp[N]))
# Original solution
import copy
N=eval(input())

def nine(m):
	if m<9: return m
	for i in range(1000):
		t=9**i
		if t>m:
			t1=9**(i-1)
			return m-t1
def six(m):
	if m<6: return m
	for i in range(1000):
		t=6**i
		if t>m:
			t2=6**(i-1)
			return m-t2


Z=15
k=1<<Z

b=set()
for i in range(k):

	n=copy.deepcopy(N)
	ans=0
	for j in range(Z):
		if n<6:
			b.add(ans+n)
			break
		if i&(1<<j)!=0:
			#9
			n=nine(n)
			ans+=1
		else:
			#6
			n=six(n)
			ans+=1


print(min(b))




# Optimized solution
N=eval(input())

dp=[ float("inf") for i in range(N+1) ]
dp[0]=0

i=1

for i in range(1,8):
	for n in range(N):
		if n+ 9**i  <=N: 	dp[ n+ 9**i ]=min( dp[n]+1, dp[ n+9**i ] )
		if n+ 6**i  <=N: 	dp[ n+ 6**i ]=min( dp[n]+1, dp[ n+6**i ] )
		if n+ 1  <=N:	dp[ n+ 1 ]=min( dp[n]+1, dp[ n+1 ] )


print(dp[N])
# Original solution
import sys
sys.setrecursionlimit(101000)

N=eval(input())

memo={}
def dp(n):
	ret=float("inf")
	if n==0: return 0
	if n<0: return float("inf")
	if n in memo: return memo[n]

	for k in range(1,8):
		t=min(dp(n-6**k)+1,dp(n-9**k)+1,dp(n-1)+1)
		ret=min(ret,t)
	
	memo[n]=ret
	return ret

#print dp(8)
#print dp(12)
#print dp(83)
#print dp(N)
dp(N)
print(memo[N])


# Optimized solution
N=eval(input())

nl=[]
nl.append(1)
for i in range(1,7): nl.append(6**i)
for i in range(1,6): nl.append(9**i)

dp=[ float("inf") for i in range(N+1)]
dp[0]=0

for i in range(N+1):
	for dx in nl:
		if i+dx<=N:
			dp[i+dx]=min(dp[i+dx], dp[i]+1)


print(dp[N])
# Original solution
N=eval(input())

nl=[]
nl.append(1)
for i in range(1,7): nl.append(6**i)
for i in range(1,6): nl.append(9**i)

dp=[ float("inf") for i in range(N+1)]
dp[0]=0

for i in range(N+1):
	for dx in nl:
		if i+dx<=N:
			dp[i+dx]=min(dp[i+dx], dp[i]+1)


print(dp[N])

# Optimized solution
# -*- coding: utf-8 -*-
N=eval(input())
dp=[ float("inf") for _ in range(N+1) ]
dp[0]=0

#1円
for i in range(N+1):
    if i+1<=N:
        dp[i+1]=min(dp[i+1],dp[i]+1)
#6円
for k in range(1,10**5):
    yen=6**k
    for i in range(N+1):
        if i+yen<=N:
            dp[i+yen]=min(dp[i+yen],dp[i]+1)
    if N<=yen:break

#9円
for k in range(1,10**5):
    yen=9**k
    for i in range(N+1):
        if i+yen<=N:
            dp[i+yen]=min(dp[i+yen],dp[i]+1)
    if N<=yen:break

print(dp[N])
# Original solution
max_n = 100000

N = int(eval(input()))

dp = [N for _ in range(N+1)]
dp[0] = 0
for n in range(1, N+1):
    for pow6 in range(n+1):
        if 6 ** pow6 <= n:
            dp[n] = min(dp[n], dp[n - 6 ** pow6] + 1)
        else:
            break

    for pow9 in range(n+1):
        if 9 ** pow9 <= n:
            dp[n] = min(dp[n], dp[n - 9 ** pow9] + 1)
        else:
            break
print((dp[N]))


# Optimized solution
N = int(eval(input()))

one = [1]
six_multi = [6, 6**2, 6**3, 6**4, 6**5, 6**6]
nine_multi = [9, 9**2, 9**3, 9**4, 9**5]
deposit = one + six_multi + nine_multi

dp = [10**10 for _ in range(N+1)] 
dp[0] = 0

for i in range(0, N):
    for d in range(len(deposit)):
        if i+deposit[d] <= N:
            dp[i+deposit[d]] = min(dp[i+deposit[d]], dp[i] + 1)

print((dp[N]))
# Original solution
import math
N = int(eval(input()))
 
def Nsinpou(x, base):
    #配列を返す Nsin[0]が上位桁 ex. 56(base=6) => [1,3,2]
    if x == 0: return [0] #log_n(0)の回避
    Nsin = []
    for i in range((int(round(math.log(x,base))//1)+1)):
        ithdigit = x%(base**(i+1)) // (base ** i)
        Nsin.append(ithdigit)
    Nsin.reverse()
    return Nsin
 
ans = N #最大でも1円玉N枚
for i in range(N+1): #分け方全探索
    coins = sum(Nsinpou(i,6)) + sum(Nsinpou(N-i,9))
    if coins < ans:
        ans = coins
    
print(ans)

# Optimized solution
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]))
# Original solution
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]))

# Optimized solution
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())))))
# Original solution
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]))

# Optimized solution
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())))))
# Original solution
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]))


# Optimized solution
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]))