CodeEff/ECCO huggingface dataset:
# Original solution
# 貪欲的だが「6^i円で払う分」と「9^i円で払う分」にNを分割してN+1通り全探索．


def how_many_times_withdraw(withdraw, base, limit):
    result = 0
    while withdraw >= base:
        result += withdraw // (base ** limit)
        withdraw = withdraw % (base ** limit)
        limit -= 1
    return result + withdraw


def main():
    N = int(eval(input()))
    limit = []
    answer = float("inf")
    for yen in [6, 9]:
        i = 1
        while yen ** i <= N:
            i += 1
        limit.append(i - 1)
    for i in range(N + 1):
        answer = min(answer, how_many_times_withdraw(i, 6, limit[0]) + how_many_times_withdraw(N - i, 9, limit[1]))
    print(answer)


if __name__ == '__main__':
    main()

# Optimized solution
def make_list(a, n):
    res = [1]
    i = 1
    while pow(a, i) <= n:
        res.append(pow(a, i))
        i += 1
    return res[::-1]


def find_withdraw_times(n, w):
    res = 0
    for ww in w:
        res += n // ww
        n %= ww
    return res


def main():
    n = int(eval(input()))
    answer = float("inf")
    way_6 = make_list(6, n)
    way_9 = make_list(9, n)
    for i in range(n + 1):
        answer = min(answer, find_withdraw_times(i, way_6) + find_withdraw_times(n - i, way_9))
    print(answer)


if __name__ == '__main__':
    main()
# Original solution
import sys
import math
N = int(sys.stdin.readline().rstrip())

# def create_money(i):
#     money = []
#     tmp = 1
#     for _ in range(math.floor(math.log(N, i))):
#         tmp *= i
#         money.append(tmp)
    
#     return money

# money = [1]
# money += create_money(6)
# money += create_money(9)

money = [1]
for i in range(20):
    if 100000 >= 6**i:
        money.append(6**i)
    if 100000 >= 9**i:
        money.append(9**i)


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

for i in range(1, len(money) + 1):
    m = money[i - 1]
    for j in range(N + 1):
        if j - m >= 0:
            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - m] + 1, dp[i][j - m] + 1)
        else:
            dp[i][j] = dp[i - 1][j]

print((dp[len(money)][N]))

# Optimized solution
import sys
import math
N = int(sys.stdin.readline().rstrip())

# def create_money(i):
#     money = []
#     tmp = 1
#     for _ in range(math.floor(math.log(N, i))):
#         tmp *= i
#         money.append(tmp)
    
#     return money

# money = [1]
# money += create_money(6)
# money += create_money(9)

money = [1]
for i in range(20):
    if 100000 >= 6**i:
        money.append(6**i)
    if 100000 >= 9**i:
        money.append(9**i)

money = sorted(money)[::-1]


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

for i in range(1, len(money) + 1):
    m = money[i - 1]
    for j in range(N + 1):
        if j - m >= 0:
            dp[i][j] = min(dp[i - 1][j], dp[i - 1][j - m] + 1, dp[i][j - m] + 1)
        else:
            dp[i][j] = dp[i - 1][j]

print((dp[len(money)][N]))
# Original solution
n = int(eval(input()))
dp = [float("Inf")]*(n+1)
for i in range(n+1):
    if i < 6:dp[i] = i
    power = 1
    while i >= 6 ** power:
        dp[i] = min(dp[i],dp[i-6**power]+1)
        power += 1
    power = 1
    while i >= 9 ** power:
        dp[i] = min(dp[i],dp[i-9**power]+1)
        power += 1
print((dp[n]))

# Optimized solution
n = int(eval(input()))
dp = [float("Inf")]*(n+1)
for i in range(n+1):
    if i < 6:dp[i] = i
    power = 1
    while i >= 6 ** power:
        if dp[i] > dp[i-6**power]+1:dp[i] = dp[i-6**power] + 1
        power += 1
    power = 1
    while i >= 9 ** power:
        if dp[i] > dp[i-9**power]+1:dp[i] = dp[i-9**power] + 1
        power += 1
print((int(dp[n])))
# Original solution
l9 = []
l9.append(1)
tmp = 1
while tmp <= 10**5:
  tmp *= 9
  l9.append(tmp)
l6 = []
l6.append(1)
tmp = 1
while tmp <= 10**5:
  tmp *= 6
  l6.append(tmp)

  
import bisect 
n = int(eval(input()))
ans = 10**6

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

  a = i
  b = n - i
  p = 0
  while a > 0:
    idx = bisect.bisect_left(l6, a)
    if a == l6[idx]:
      a -= l6[idx]
    else:
      a -= l6[idx-1]
    p += 1
    
  q = 0
  while b > 0:
    idx = bisect.bisect_left(l9, b)
    if b == l9[idx]:
      b -= l9[idx]
    else:
      b -= l9[idx-1]
    q += 1
    
  ans = min(ans, p+q)

print(ans)

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

ans = N
sixs = [1]
i = 6
while i <= N:
    sixs.append(i)
    i *= 6
nines = [1]
i = 9
while i <= N:
    nines.append(i)
    i *= 9

for i in range(N+1):
    #iの分だけ6**xで構成
    cnt = 0
    a = i
    b = N-i
    while a > 0:
        idx = bisect.bisect_right(sixs, a)
        cnt += a // sixs[idx-1]
        a -= (a // sixs[idx-1]) * sixs[idx-1]

    while b > 0:
        idx = bisect.bisect_right(nines, b)
        cnt += b // nines[idx-1]
        b -= (b // nines[idx-1]) * nines[idx-1]

    ans = min(ans, cnt)

print(ans)
# Original solution
import bisect
N = int(eval(input()))

ans = N
sixs = [1]
i = 6
while i <= N:
    sixs.append(i)
    i *= 6
nines = [1]
i = 9
while i <= N:
    nines.append(i)
    i *= 9

for i in range(N+1):
    #iの分だけ6**xで構成
    cnt = 0
    a = i
    b = N-i
    while a > 0:
        idx = bisect.bisect_right(sixs, a)
        cnt += a // sixs[idx-1]
        a -= (a // sixs[idx-1]) * sixs[idx-1]

    while b > 0:
        idx = bisect.bisect_right(nines, b)
        cnt += b // nines[idx-1]
        b -= (b // nines[idx-1]) * nines[idx-1]

    ans = min(ans, cnt)

print(ans)

# Optimized solution
from bisect import bisect_right

def main():
    N = int(eval(input()))


    sixs = [1]
    i = 6
    while i <= N:
        sixs.append(i)
        i *= 6

    nines = [1]
    i = 9
    while i <= N:
        nines.append(i)
        i *= 9

    """
    i円分を6**xxx で、　残りを９**yyyと１*zzzで構成する。これらを全通り試す
    """

    ans = N
    for i in range(N+1):
        cnt = 0
        sum_six = i
        sum_nine = N-i
        while sum_six > 0:
            idx = bisect_right(sixs, sum_six) - 1
            tmp = sum_six // sixs[idx]
            cnt += tmp
            sum_six -= tmp * sixs[idx]

        while sum_nine > 0:
            idx = bisect_right(nines, sum_nine) - 1
            tmp = sum_nine // nines[idx]
            cnt += tmp
            sum_nine -= tmp * nines[idx]

        ans = min(ans, cnt)
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
n = int(eval(input()))
dp = [float('Inf')] * (100000 + 1)
dp[0] = 0
for i in range(1, n + 1):
    dp[i] = dp[i - 1] + 1
    cnt = 1
    for j in range(0, 7):
        cnt *= 6 
        if i - cnt >= 0:
            dp[i] = min(dp[i], dp[i - cnt] + 1)
    cnt = 1
    for j in range(0, 6):
        cnt *= 9
        if i - cnt >= 0:
            dp[i] = min(dp[i], dp[i - cnt] + 1)
print((dp[n]))

# Optimized solution
n = int(eval(input()))
dp = [float('Inf')] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
    j = 1
    while j <= i:
        dp[i] = min(dp[i], dp[i - j] + 1)
        j *= 6
    j = 1
    while j <= i:
        dp[i] = min(dp[i], dp[i - j] + 1)
        j *= 9
print((dp[n]))
# Original solution
from collections import deque
def main():
    N = int(eval(input()))
    divs = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1]
    dp = [0]*(10**6+1)
    def f(x):
        if x == 0:
            return 0
        if x < 0:
            return 10**6
        if dp[x]:
            return dp[x]
        else:
            res = 10**6
            for d in divs:
                res = min(res,f(x-d)+1)
            dp[x] = res
            return res
            

    print((f(N)))
main()

# Optimized solution
def main():
    N = int(eval(input()))
    amounts = [1]
    lim = 10**6
    mul6 = 6
    while mul6 < lim:
        amounts.append(mul6)
        mul6 *= 6
    mul9 = 9
    while mul9 < lim:
        amounts.append(mul9)
        mul9 *= 9
    
    dp = [lim] * (N+1)
    dp[0] = 0
    for i in range(N):
        for a in amounts:
            if i+a <= N:
                dp[i+a] = min(dp[i+a],dp[i]+1)
    print((dp[N]))

    



main()
# Original solution
n = int(eval(input()))

#引き出せる金額の列挙
Withdr = [1] + [6**i for i in range(1, 7)] + [9**i for i in range(1,6)]
INF = 100010
dp = [INF]*159050
dp[0] = 0

#全てを順に計算
for j in range(n + 1):
    #あるnに対し複数の計算方法があるため、それらの中で一番少ないものをdpに格納する
    for WithdrOrder in Withdr:
        #dp[j + WithdrOrder]は今までに計算した最小値とdp[j]から1回で引き出せる金額を足したものとの比較で算出
        dp[j + WithdrOrder] = min(dp[j + WithdrOrder], dp[j] + 1)
print((dp[n]))

# Optimized solution
N=int(eval(input()))
ans=N
for i in range(N+1):
    element_cnt=0
    t=i
    '''Nを6の累乗で処理する部分(i)と9の累乗で処理する部分(j)に分ける
    ある数(i)を6で割った余りは6のゼロ乗の要素がいくつ含まれるかを示す
    ある数の6の商をさらに6で割った余りは、ある数に6の1乗がいくつ含まれるかを示す
    ある数の6の商の6の商をさらに6で割った余りは、ある数に6の2乗がいくつ含まれるかを示す
    したがって、ある数を6で割って余りと商を順に処理すれば6の累乗の要素がどれだけあるかがわかる。
    最終的に商がゼロになるまで繰り返す
    '''
    while t>0:
        element_cnt+=t%6
        t//=6
    j=N-i
    while j>0:
        element_cnt+=j%9
        j//=9
    ans = min(ans,element_cnt)
print(ans)
# Original solution
N = int(eval(input()))
A=[]
A.append(1)
for i in range(1,7):
    A.append(6**i)
for i in range(1,6):
    A.append(9**i)
A.sort()
dp=[10*5+5]*(N+1)
dp[0]=0
for i in range(1,N+1):
    for s in range(len(A)):
        if i-A[s]>=0:
            dp[i]=min(dp[i-A[s]]+1,dp[i])
print((dp[N]))


# Optimized solution
N = int(eval(input()))
def shin(n,q):
    s = n
    a = 0
    while(s>0):
        t=s%q
        a +=t
        s=(s-t)//q
    return a
ans = N
for i in range(N+1):
    c=shin(i,6)+shin(N-i,9)
    ans = min(ans,c)
print(ans)
# Original solution
def solve():
    n = int(eval(input()))
    dp = [[False]*(n+1)]
    dp[0][0] = True
    way = [1] + [6**i for i in range(1,9) if 6**i <= n] + [9**i for i in range(1,7) if 9**i <= n]
    while True:
        newline = [False] * (n+1)
        for w in way:
            for i in range(0, n+1):
                if i - w < 0: continue
                if newline[i]: continue
                if dp[len(dp)-1][i-w]: newline[i] = True
                if newline[n]: return len(dp)
        dp.append(newline)

print((solve()))

# Optimized solution
def solve():
    n = int(eval(input()))
    dp = [True] + [False]*(n+1)
    way = [1] + [6**i for i in range(1,9) if 6**i <= n] + [9**i for i in range(1,7) if 9**i <= n]
    loop_counter = 0
    while True:
        loop_counter += 1
        newdp = [False] * (n+1)
        for w in way:
            for i in range(w, n+1):
                if newdp[i]: continue
                if dp[i-w]: newdp[i] = True
                if newdp[n]: 
                    return loop_counter
        dp = [x for x in newdp]

print((solve()))
# Original solution
# ABC099C - Strange Bank
# DP ver.
def main():
    n = int(eval(input()))
    dp = [i for i in range(n + 1)]  # initialize with using only ¥1
    cand = [6 ** i for i in range(1, 7) if 6 ** i <= n]
    cand += [9 ** i for i in range(1, 6) if 9 ** i <= n]
    for i in range(n + 1):
        for j in cand:
            if j > n:
                break
            if i - j >= 0:
                dp[i] = min(dp[i], dp[i - j] + 1)
    print((dp[-1]))


if __name__ == "__main__":
    main()

# Optimized solution
# ABC099C - Strange Bank
# DP ver.
def main():
    n = int(eval(input()))
    dp = [i for i in range(n + 1)]  # initialize with using only ¥1
    cand = [6 ** i for i in range(1, 7) if 6 ** i <= n]
    cand += [9 ** i for i in range(1, 6) if 9 ** i <= n]
    for i in range(n + 1):
        for j in cand:
            if i - j >= 0:
                dp[i] = min(dp[i], dp[i - j] + 1)
    print((dp[-1]))


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

input = sys.stdin.readline


def main():
    N = int(eval(input()))

    dp = [0] * (10 ** 5 + 1)
    dp[1] = 1

    x = [1]
    x.extend([6 ** x for x in range(1, 8)])
    x.extend([9 ** x for x in range(1, 6)])


    for i in range(2, 10 ** 5 + 1):
        tmp = []
        for v in x:
            if i >= v:
                tmp.append(dp[i - v] + 1)

        dp[i] = min(tmp)

    print((dp[N]))


if __name__ == '__main__':
    main()


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

dp = [0] * 100001

dp[1] = 1

for i in range(2, N + 1):
    dp[i] = dp[i - 1] + 1
    j = 1
    while 6 ** j <= i:
        dp[i] = min(dp[i], dp[i - (6 ** j)] + 1)
        j += 1
    j = 1
    while 9 ** j <= i:
        dp[i] = min(dp[i], dp[i - (9 ** j)] + 1)
        j += 1

print((dp[N]))
# Original solution
ans = [0] * 100100
ans[0] = 0

n = int(eval(input()))
#n = 127
for i in range(1,n+1):
    cand1 = ans[i-1]
    cand2 = 100001
    cand3 = 100001
    x = 6
    while (x<=i):
        cand2 = min(ans[i-x], cand2)
        x=6*x
    y = 9
    while (y<=i):
        cand3 = min(ans[i-y], cand3)
        y=9*y
    ans[i] = min(cand1,cand2,cand3) + 1


print((ans[n]))


# Optimized solution
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  6 17:34:40 2018

@author: Yuki
"""

ans = [0] * 100100
ans[0] = 0

n = int(eval(input()))
#n = 127
for i in range(1,n+1):
    cand1 = ans[i-1]
    cand2 = 100001
    cand3 = 100001
    x = 6
    while (x<=i):
        #cand2 = min(ans[i-x], cand2)
        cand2 = ans[i-x]
        x=6*x
    y = 9
    while (y<=i):
        #cand3 = min(ans[i-y], cand3)
        cand3 = ans[i-y]
        y=9*y
    ans[i] = min(cand1,cand2,cand3) + 1


print((ans[n]))
# Original solution
import math
n = int(eval(input()))
#n = 127
inf = float('inf')

dp = [inf]*(n+100000)
dp[0]=0
#print(dp)
#print(len(dp))

def motome(target):
    tmp = []
    for i in range(10):
        if 9**i <= n:
            tmp.append(9**i)
        if 6**i <= n:
            tmp.append(6**i)
    #print(tmp) 
    #[1, 6, 36, 1, 9, 81]
    #print(dp)
    for k in range(target+1):
        for t in sorted(list(set(tmp))):
            if k-t>=0:
                dp[k] = min(dp[k],1 + dp[k-t])
                #dp[k] = min([dp[k-t] for t in tmp])
                '''
                try:
                    dp[k] = min(dp[k],1 + min([dp[k-t] for t in tmp]))
                except:
                    print(len(dp),dp[k-t],dp[k])
                    pass
                '''
        #print(k)
        #print([t for t in tmp])
        #print([dp[k-t] for t in tmp])

motome(n)
#print(dp[n-1])
print((dp[n]))
#print(dp)
#print(dp[n+2])
#print(dp[n-1])
#print(dp[n])

# Optimized solution
n = int(eval(input()))
inf = float('inf')
dp = [inf]*(n+1)
dp[0] = 0
a = 1
b = 6
c = 9
for i in range(1,n+1):
    if i>=b*6:
        b*=6
    if i>=c*9:
        c*=9
    if i<6:
        dp[i] = i
    elif i<9:
        dp[i] = min(dp[i],dp[i-b]+1)
    else:
        dp[i] = min(dp[i],dp[i-b]+1,dp[i-c]+1)
print((dp[n]))
# Original solution
# 配るDP

N=int(eval(input()))

dp = [100000] * 200005;

g = [1]
a = 6
while a <= 100000:
  g.append(a)
  a *= 6
a = 9
while a <= 100000:
  g.append(a)
  a *= 9
g.sort(reverse=True)

dp[0] = 0
for i in range(N):
  for j in g:
    dp[i+j] = min( dp[i+j], dp[i]+1 )

print((dp[N]))



# Optimized solution
# BFS
from collections import deque

N=int(eval(input()))

dp = [-1] * 200005;
q = deque()

g = [1]
a = 6
while a <= 100000:
  g.append(a)
  a *= 6
a = 9
while a <= 100000:
  g.append(a)
  a *= 9
g.sort(reverse=True)

dp[0] = 0
q.append(0)

while len(q) > 0:
  v = q.popleft()
  if v == N: break
  for j in g:
    if dp[v+j] != -1: continue
    if v+j > N: continue
    dp[v+j] = dp[v] + 1
    q.append(v+j)

print((dp[N]))
# Original solution
N = int(eval(input()))
ans = 10**13
Y = [9**i for i in range(1, 10)] + [6**i for i in range(1, 10)]
Y = sorted(Y, reverse=True)
for i in range(N+1):
    tmp = 0
    n = N-i
    for j in range(10, -1, -1):
        if i // 9**j > 0:
            tmp += i // 9**j
            i %= 9**j
    for j in range(10, -1, -1):
        if n // 6**j > 0:
            tmp += n // 6**j
            n %= 6**j
        # print(ans, j)

        # print(ans, j, N)
    ans = min(ans, tmp)

print(ans)

# Optimized solution
# dp answer
N = int(eval(input()))
dp = [N for i in range(N+1)]
dp[0] = 0
Y = [1] + [6**i for i in range(1, 10) if 6**i <= N] + [9**i for i in range(1, 10) if 9**i <= N]
Y = sorted(Y)
for i in range(1, N+1):
    for y in Y:
        if i-y < 0:
            break
        dp[i] = min(dp[i], dp[i-y]+1)
print((dp[N]))
# Original solution
import math
N = int(eval(input()))
dp=[100000]*(100000+1)
dp[0]=0
dp[1:5]=list(range(1,6))
dp[6:9]=list(range(1,4))
for n in range(9,N+1):
  k9=0
  k6=0
  while n//(9**k9)>=9:
    k9+=1
  while n//(6**k6)>=6:
    k6+=1
  dp[n]=min(dp[n],dp[n-6**k6]+1,dp[n-9**k9]+1,dp[n-1]+1)
print((dp[N]))

# Optimized solution
import math
N = int(eval(input()))
dp=[100000]*(100000+1)
dp[0]=0
dp[1:5]=list(range(1,6))
dp[6:9]=list(range(1,4))
k9=0
k6=0
for n in range(9,N+1):
  while n//(9**k9)>=9:
    k9+=1
  while n//(6**k6)>=6:
    k6+=1
  dp[n]=min(dp[n],dp[n-6**k6]+1,dp[n-9**k9]+1,dp[n-1]+1)
print((dp[N]))
# Original solution
n=int(eval(input()))
import math
ansa=[0]
ansb=[0]
ans=[]
cnt=0
x=0
y=0
for i in range(1,n+1):
    while 9**(x+1)<=i:
        x+=1
    for j in range(x,-1,-1):
        z=(math.floor(i/(9**j))%9)
        i-=(9**j)*z
        cnt+=z
    ansa.append(cnt)
    cnt=0    
    
for i in range(1,n+1):
    while 6**(y+1)<=i:
        y+=1
    for j in range(y,-1,-1):
        z=(math.floor(i/(6**j))%6)
        i-=(6**j)*z
        cnt+=z
    ansb.append(cnt)
    cnt=0
    
for i in range(n+1):
    ans.append(ansa[i]+ansb[n-i])
    
print((min(ans)))   



# Optimized solution
n=int(eval(input()))
li6=[46656,7776,1296,216,36,6,1]
li9=[59049,6561,729,81,9,1]
dp6=[0]*(n+1)
dp9=[0]*(n+1)
dp6[1]=1
dp9[1]=1
ans=[]
for i in range(2,n+1):
    for j in range(7):
        if i>=li6[j]:
            k=i-li6[j]
            break
    dp6[i]=1+dp6[k] 
for i in range(2,n+1):
    for j in range(6):
        if i>=li9[j]:
            k=i-li9[j]
            break
    dp9[i]=1+dp9[k]  
for i in range(n+1):
    ans.append(dp6[i]+dp9[n-i])                    
print((min(ans)))
# Original solution
num = [i for i in range(0,100001)]

for j in range(1,100001):
        num[j] = j//6 + num[j%6]

for j in range(1,100001):
        num[j] = min(num[j],j//9+num[j%9])

for j in range(1,100001):
        num[j] = min(num[j],j//36+num[j%36])

for j in range(81,324):
        num[j] = min(num[j],1+num[j-81])
for j in range(162,324):
        num[j] = min(num[j],2+num[j-162])
for j in range(243,324):
        num[j] = min(num[j],3+num[j-243])
for j in range(324,100001):
        num[j] = (j//324)*4 + num[j%234]

for j in range(216,648):
        num[j] = min(num[j],1+num[j-216])
for j in range(432,648):
        num[j] = min(num[j],2+num[j-216*2])
for j in range(648,100001):
        num[j] = (j//648)*3 + num[j%648]

for i in range(1,8):
        for j in range(729*i,729*8):
                num[j] = min(num[j],i+num[j-729*i])
for j in range(729*8,100001):
        num[j] = (j//(729*8))*8+num[j%(729*8)]

MoneyYouCanGet = [6**4,9**4,6**5,6**6,9**5]

for i in MoneyYouCanGet:
        for j in range(1,100001):
                k = 1
                while k*i<j:
                        num[j] = min(num[j],k+num[j-k*i])
                        k += 1

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

# Optimized solution
SixNum = [i for i in range(0,100001)]
for i in range(1,7):
        for j in range(1,100001):
                SixNum[j] = min(SixNum[j],j//(6**i) + SixNum[j%(6**i)])

NineNum = [i for i in range(0,100001)]
for i in range(1,6):
        for j in range(1,100001):
                NineNum[j] = min(NineNum[j],j//(9**i) + NineNum[j%(9**i)])

N = int(eval(input()))
ans = 100001
for i in range(0,N+1):
        ans = min(ans,SixNum[i]+NineNum[N-i])
print(ans)
# Original solution
SixNum = [i for i in range(0,100001)]
for i in range(1,7):
        for j in range(1,100001):
                SixNum[j] = min(SixNum[j],j//(6**i) + SixNum[j%(6**i)])

NineNum = [i for i in range(0,100001)]
for i in range(1,6):
        for j in range(1,100001):
                NineNum[j] = min(NineNum[j],j//(9**i) + NineNum[j%(9**i)])

N = int(eval(input()))
ans = 100001
for i in range(0,N+1):
        ans = min(ans,SixNum[i]+NineNum[N-i])
print(ans)

# Optimized solution
n=int(eval(input()))
dp=[10**10]*(n+1)
dp[0]=0
for i in range(1,n+1):
  dp[i]=dp[i-1]+1
  x=0
  while 9**(x+1)<=i:
    x+=1
  dp[i]=min(dp[i],dp[i-9**x]+1)
  x=0
  while 6**(x+1)<=i:
    x+=1
  dp[i]=min(dp[i],dp[i-6**x]+1)
print((dp[n]))
# Original solution
n = int(eval(input()))
a = 6
b = [1]
b.append(a)
while a <= 100000:
	a *= 6
	b.append(a)
c = 9
b.append(c)
while c <= 100000:
	c *= 9
	b.append(c)
b.sort()
INF = 10**7
dp = [INF]*100001
dp[0] = 0
for i in range(100001):
	for j in b:
		if i + j > 100000:
			continue
		dp[i+j] = min(dp[i]+1, dp[i+j])
print((dp[n]))

# Optimized solution
n = int(eval(input()))
ary = [1]
x = 1
for i in range(6):
	x *= 6
	ary.append(x)
x = 1
for i in range(5):
	x *= 9
	ary.append(x)
ary.sort()

dp = [i for i in range(n+1)]

for i in range(1,n+1):
	for j in ary:
		if j > i:
			break
		dp[i] = min(dp[i],dp[i-j]+1)
print((dp[n]))
# Original solution
n = int(eval(input()))
ary = [1]
x = 1
for i in range(6):
	x *= 6
	ary.append(x)
x = 1
for i in range(5):
	x *= 9
	ary.append(x)
ary.sort()

dp = [i for i in range(n+1)]

for i in range(1,n+1):
	for j in ary:
		if j > i:
			break
		dp[i] = min(dp[i],dp[i-j]+1)
print((dp[n]))

# Optimized solution
n = int(eval(input()))
ary = [1]
x = 1
for i in range(6):
	x *= 6
	ary.append(x)
x = 1
for i in range(5):
	x *= 9
	ary.append(x)
ary.sort()

dp = [i for i in range(n+1)]

for i in range(1,n+1):
	for j in ary:
		if j > i:
			continue
		dp[i] = min(dp[i],dp[i-j]+1)
print((dp[n]))
# Original solution
def main():
    n = int(eval(input()))
    m = [10 ** 5 for _ in range(n + 1)]
    m[0] = 0
    for i in range(1, n + 1):
        j = 0
        while i - 6 ** j >= 0:
            m[i] = min(1 + m[i - 6 ** j], m[i])
            j += 1
        j = 0
        while i - 9 ** j >= 0:
            m[i] = min(1 + m[i - 9 ** j], m[i])
            j += 1

    print((m[-1]))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    import bisect
    n = int(eval(input()))
    m = [10 ** 5 for _ in range(n + 1)]
    m[0] = 0
    a = [6 ** i for i in range(7)]
    b = [9 ** i for i in range(6)]
    c = sorted(a + b)
    for i in range(1, n + 1):
        j = bisect.bisect_right(c, i)
        k = [1 + m[i - x] for l, x in enumerate(c) if l < j]
        m[i] = min(k)

    print((m[-1]))


if __name__ == '__main__':
    main()
# Original solution
# 配るDP

n = int(eval(input()))


max_n = 110000
dp = [n] * max_n

# 初期化
dp[0] = 0

for i in range(n):
    pow6, pow9 = 1, 1
    while i + pow6 <= n:
        dp[i + pow6] = min(dp[i + pow6], dp[i] + 1)
        pow6 *= 6
    while i + pow9 <= n:
        dp[i + pow9] = min(dp[i + pow9], dp[i] + 1)
        pow9 *= 9
print((dp[n]))


# Optimized solution
# 貰うDP

n = int(eval(input()))


max_n = 110000
dp = [n] * max_n

# 初期化
dp[0] = 0

for i in range(1, n + 1):
    pow6, pow9 = 1, 1
    while pow6 <= i:
        dp[i] = min(dp[i], dp[i - pow6] + 1)
        pow6 *= 6
    while pow9 <= i:
        dp[i] = min(dp[i], dp[i - pow9] + 1)
        pow9 *= 9

print((dp[n]))