To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:
 - 1 yen (the currency of Japan)
 - 6 yen, 6^2(=36) yen, 6^3(=216) yen, ...
 - 9 yen, 9^2(=81) yen, 9^3(=729) yen, ...
At least how many operations are required to withdraw exactly N yen in total?
It is not allowed to re-deposit the money you withdrew.

-----Constraints-----
 - 1 \leq N \leq 100000
 - N is an integer.

-----Input-----
Input is given from Standard Input in the following format:
N

-----Output-----
If at least x operations are required to withdraw exactly N yen in total, print x.

-----Sample Input-----
127

-----Sample Output-----
4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Difficulty: introductory

Starter code:

Solutions:
N = int(input())
res = N
for i in range(N+1):
    cc = 0
    t = i
    while t > 0:
        cc += t%6
        t //= 6
    t = N - i
    while t > 0:
        cc += t%9
        t //= 9
    if res > cc:
        res = cc
print(res)


        
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()

    n = int(input())
    inf = 1 << 18
    dp = [inf]*(n+1)
    dp[0] = 0
    for i in range(n):
        dp[i+1] = min(dp[i+1], dp[i]+1)
        k = 1
        while i+6**k <= n:
            dp[i+6**k] = min(dp[i+6**k], dp[i]+1)
            k += 1
        k = 1
        while i+9**k <= n:
            dp[i+9**k] = min(dp[i+9**k], dp[i]+1)
            k += 1
    print(dp[n])


    




                
def __starting_point():
    main()
__starting_point()

# -*- coding: utf-8 -*-

def count(val1, val2):
    digit = 1
    while val1 >= (val2 ** digit):
        digit += 1

    res = 0
    while digit!=0:
        cnt = int(val1 // val2 ** digit)
        val1 -= (val2 ** digit)*cnt
        res += cnt
        digit -= 1

    return res,val1


N = int(input())
ans = N

for i in range(0,N+1):
    n = i
    m = N - i
    res = 0

    cnt, nn = count(n, 9)
    n = nn
    res += cnt

    cnt, mm = count(m, 6)
    m = mm
    res += cnt

    res += n + m
    ans = min(ans, res)

print(ans)


def sinsuuton(X, n): #10進数の Xをn進数に
    if (int(X/n)):
        return sinsuuton(int(X/n), n)+str(X%n)
    return str(X%n)
def sinsuutoo10(X,n): #n進数のX(str)を10進数に
    return sum([ X[-i-1]*n**i for i in range(len(X)) ])
n=int(input())
ans=float("inf")
for i in range(n+1):
    t=i
    now=0
    t6= sinsuuton(t, 6)
    now+= sum([int(i) for i in t6])
    t=n-i
    t9= sinsuuton(t, 9)
    now+= sum([int(i) for i in t9])
    ans=min(ans,now)

print(ans)


# -*- coding: utf-8 -*-

N = int(input())

ans = N
for i in range(N+1):
    n = i
    m = N - i
    res = 0

    nine = 1
    while nine*9 <= n:
        nine *= 9
    while nine != 1:
        res += n // nine
        n %= nine
        nine /= 9
    res += n

    six = 1
    while six*6 <= m:
        six *= 6
    while six != 1:
        res += m // six
        m %= six
        six /= 6
    res += m

    ans = min(ans, int(res))

print(ans)


N=int(input())
A=[1,6,36,6**3,6**4,6**5,6**6,9,81,9**3,9**4,9**5]
B=[0]+[N]*N
i=0
while i<=N:
    for a in A:
        if a+i<=N:
            B[a+i]=min(B[a+i],B[i]+1)
    i+=1
print(B[N])

n=int(input())

dp=[float('inf')]*(n+1)
dp[0]=0

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

print((dp[n]))



N=int(input())
cnt=[0 for _ in range(N+1)]
for i in range(1,N+1):
    cnt[i]=cnt[i-1]+1
    a=6
    while i-a>=0:
        cnt[i]=min(cnt[i],cnt[i-a]+1)
        a*=6
    a=9
    while i-a>=0:
        cnt[i]=min(cnt[i],cnt[i-a]+1)
        a*=9
print((cnt[N]))


N=int(input())
INF=10**10
dp=[INF]*(N+1)
dp[0]=0
for i in range(N+1):
    if i+1<=N: 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])

n = int(input())
dp = [float('inf')]*(n+1)
dp[0] = 0
for i in range(n+1):
    for j in [6, 9]:
        cnt = 1
        while i+cnt <= n:
            if dp[i+cnt] > dp[i]+1:
                dp[i+cnt] = dp[i]+1
            cnt *= j
print(dp[-1])

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N = int(readline())

    amt = [1]
    a = 6
    while a <= N:
        amt.append(a)
        a *= 6
    a = 9
    while a <= N:
        amt.append(a)
        a *= 9

    dp = [INF] * (N + 1)
    dp[0] = 0
    for i in range(1, N + 1):
        for a in amt:
            if i >= a and dp[i] > dp[i - a]:
                dp[i] = dp[i - a] + 1

    print((dp[N]))

    return


def __starting_point():
    main()

__starting_point()

import sys
sys.setrecursionlimit(10**8)

N = int(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))

# -*- coding: utf-8 -*-

def count(val1, val2):
    num = 1
    while True:
        if val1 < (val2 ** num):
            break
        num += 1

    cnt = 0
    digit = num
    while True:
        if val1 >= val2 ** digit:
            val1 -= val2 ** digit
            cnt += 1
        else:
            digit -= 1
        
        if digit== 0:
            break

    return cnt,val1


N = int(input())
ans = N

for i in range(0,N+1):
    n = i
    m = N - i
    res = 0

    cnt, nn = count(n, 9)
    n = nn
    res += cnt

    cnt, mm = count(m, 6)
    m = mm
    res += cnt

    res += n + m
    ans = min(ans, res)

print(ans)


N = int(input())

lst = [1, 6, 9, 6**2, 9**2, 6**3, 9**3, 6**4, 9**4, 6**5, 6**6, 9**5]
dp = [N+1] * (N+1)
dp[0] = 0
for i in range(1, N+1):
    for j in lst:
        if j <= i:
            # print(i, j, i-j)
            dp[i] = min(dp[i], dp[i-j]+1)
print(dp[-1])

def cal(n,x):
    res=0
    while n>0:
        res+=n%x
        n//=x
    return res

N=int(input())

ans=N
for i in range(N+1):
    ans=min(ans,cal(i,6)+cal(N-i,9))
print(ans)

def main():
    N = int(input())
    INF = float('inf')
    dp = [INF] * (N+1)
    dp[0] = 0
    for n in range(1, N+1):
        dp[n] = 1 + dp[n-1]
        for k in range(1, 8):
            p = 6**k
            if n-p >= 0:
                dp[n] = min(dp[n], dp[n-p] + 1)
            p = 9**k
            if n-p >= 0:
                dp[n] = min(dp[n], dp[n-p] + 1)
    
    ans = dp[N]
    print(ans)


def __starting_point():
    main()

__starting_point()

def sub_cnt(c, b):
    cnt = 0
    for i in b:
        while c >= i:
            c -= i
            cnt += 1
    return cnt

n = int(input())

b6 = [6**i for i in range(6, -1, -1)]
b9 = [9**i for i in range(5, -1, -1)]

ans = 10**9
n9 = n // 9
for c9 in range(n9+1):
    cnt = 0
    cn = n - c9*9
    c6 = cn // 6
    c1 = cn % 6
    
    cnt += sub_cnt(c9, b9)
    cnt += sub_cnt(c6, b6)
    cnt += c1
    ans = min(ans, cnt)
print(ans)

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def f(n, a):
    ans = 0
    while n > 0:
        ans += n % a
        n //= a
    return ans


def main():
    N = int(readline())

    ans = INF
    for i in range(N + 1):
        ans = min(ans, f(i, 6) + f(N - i, 9))

    print(ans)
    return


def __starting_point():
    main()

__starting_point()

n=int(input())
def f(x):
    ans=0
    while x>0:
        ans+=x%6
        x=x//6
    return ans
def g(x):
    ans=0
    while x>0:
        ans+=x%9
        x=x//9
    return ans
ans=n
for i in range(n+1):
    ans=min(ans,f(i)+g(n-i))
print(ans)


N=int(input())
ans=N
for i in range(N+1):
    cnt=0
    t=i
    while t>0:
        cnt+=t%9
        t//=9
    j=N-i
    while j>0:
        cnt+=j%6
        j//=6
    ans = min(ans,cnt)
print(ans)

def main3():
    import sys
    sys.setrecursionlimit(100000000)

    N=int(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(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(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)
        
def __starting_point():
    print(main3())
__starting_point()

N=int(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])

N = int(input())
Cash = sorted([1]+[6**T for T in range(1,7)]+[9**T for T in range(1,6)])
DPList = [0]*(N+1)
for TN in range(1,N+1):
    DPList[TN] = min(DPList[TN-TC]+1 if TN>=TC else 1000000 for TC in Cash)
print(DPList[N])

n = int(input())

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

dp = [0] * (n + 1)
for i in range(1, n + 1):
    cand = []
    for j in l:
        if i>=j:
            cand.append(dp[i-j] + 1)
    dp[i] = min(cand)

print(dp[n])

import math
power_6 = [6 ** i for i in range(int(math.log(100000, 6)) + 1)]
power_9 = [9 ** i for i in range(int(math.log(100000, 9)) + 1)]
p = list(set(power_6 + power_9))
p.sort()
p.append(100001)

N = int(input())
 
idx = 0
cnt = [0]
i = 1
while i < N + 1:
  if p[idx] <= i < p[idx + 1]:
    c = 0
    if idx == 0:
      cnt.append(i)
    else:
      c = [cnt[i - p[j]] + 1 for j in range(idx + 1)]
      cnt.append(min(c))
    i += 1
  else:
    idx += 1

print(cnt[N])

import sys
import math
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def get_num_draw(n, x):
    res = 0
    while n > 0:
        res += n % x
        n = n // x
    return res


def main():
    N = NI()
    ans = 10**10
    for i in range(0, N+1, 6):
        ans = min(get_num_draw(N-i, 9) + get_num_draw(i//6, 6), ans)
    print(ans)


def __starting_point():
    main()
__starting_point()

import math

n = int(input())

dp = [n] * (n+1)

dp[0] = 0
dp[1] = 1

p = int(math.log(n, 6))
q = int(math.log(n, 9))

for i in range(1, n+1):
    dp[i] = min(dp[i-1]+1, dp[i])
    for x in range(1, p+10):
        y = pow(6, x)
        if i - y >= 0:
            dp[i] = min(dp[i-y] + 1, dp[i])
        else:
            break
    for x in range(1, q+10):
        y = pow(9, x)
        if i - y >= 0:
            dp[i] = min(dp[i-y] + 1, dp[i])
        else:
            break

print(dp[n])

n = int(input())

dp = [100000] * (n+1)
dp[0] = 0

for i in range(1, n+1):
    for j in range(1, 7):
        ind = i-(6**j)
        if ind >= 0:
            dp[i] = min(dp[i], dp[ind]+1)
        else:
            break

    for j in range(1, 6):
        ind = i-(9**j)
        if ind >= 0:
            dp[i] = min(dp[i], dp[ind]+1)
        else:
            break

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

print(dp[n])

import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N = I()
    L = [1]
    temp = 1
    while temp <= N:
        temp *= 6
        L.append(temp)
    temp = 1
    while temp <= N:
        temp *= 9
        L.append(temp)
    L.sort()
    D = [i for i in range(N+1)]
    for i in L[1:]:
        for j in range(i, N+1):
            D[j] = min(D[j], D[j-i]+1)
    print(D[N])

def __starting_point():
    main()
__starting_point()

N = int(input())
dp = [i for i in range(N+1)]

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

# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def run():
    N = int(input())
    nines = [9 ** i for i in range(1, 10) if 9 ** i <= N]
    sixes = [6 ** i for i in range(1, 10) if 6 ** i <= N][::-1]
    L = len(nines)
    #print(nines, sixes)
    min_ans = INF
    for K in product(list(range(9)), repeat = L):
        #print(K, '----------------')
        ans = 0
        s = 0
        for k, x in zip(K, nines):
            if k:
                ans += k
                s += x * k
        #print(ans, s)

        if s > N:continue
        resid = N - s
        #print(resid)
        for six in sixes:
            #print(resid)
            tmp = resid // six
            if tmp:
                ans += tmp
                resid -= six * tmp
        #print(resid, ans)
        #if resid >= 6:
        #    print(resid)
        ans += resid
        min_ans = min(min_ans,ans)

    print(min_ans)

def __starting_point():
    run()

__starting_point()

import numpy as np

N = int(input())


#@njit
def main(n):
    dp = []
    for i in range(10**5+1):
        dp.append(i)
    dp = np.array(dp)
    powers = [1]
    i = 1
    while True:
        if 6**i>10**5:
            break
        powers.append(6**i)
        if 9**i<=10**5:
            powers.append(9**i)
        i += 1
    powers.sort()
    for i in range(1,10**5+1):
        for j in range(len(powers)):
            if powers[j]>i:
                break
            dp[i] = min(dp[i],dp[i-powers[j]]+1)

    print(dp[n])            

def __starting_point():
    main(N)
__starting_point()

#!/usr/bin/env python3
# import math
n = int(input())

ans = [-1]*(n+1)

ans[0] = 0
for i in range(1, n+1):
    num = i
    data = ans[i-1]+1
    for j in range(8):
        index = i-6**j
        if index < 0:
            break
        data = min(data, ans[index]+1)
    for j in range(8):
        index = i-9**j
        if index < 0:
            break
        data = min(data, ans[index]+1)
    ans[i] = data
print((ans[-1]))


def getval():
    n = int(input())
    return n

def main(n):
    dp = [0]
    arr = [1]
    a1 = 6
    a2 = 9
    for i in range(1,100):
        if a1**i<=n:
            arr.append(a1**i)
        else:
            break
    for i in range(1,100):
        if a2**i<=n:
            arr.append(a2**i)
        else:
            break
    arr.sort()
    
    for i in range(1,n+1):
        temp = i 
        for j in arr:
            if j>i:
                break
            temp = min(temp,dp[i-j]+1)
        dp.append(temp)

    print(dp[n])

def __starting_point():
    n = getval()
    main(n)
__starting_point()

def fn(n, a):
    res = 0
    while n > 0:
        res += n % a
        n //= a

    return res


def solve(N):

    ans = 10 ** 5
    for i in range(N+1):
        tmp = fn(i, 6) + fn(N-i, 9)
        ans = min(ans, tmp)

    return ans


def __starting_point():
    N = int(input())
    print((solve(N)))

__starting_point()

# -*- coding: utf-8 -*-

def count(val1, val2):
    digit = 1
    while digit*val2 <= val1:
        digit *= val2

    res = 0
    while digit!=1:
        cnt = int(val1 // digit)
        val1 -= digit*cnt
        res += cnt
        digit /= val2

    return res,val1


N = int(input())
ans = N

for i in range(0,N+1):
    n = i
    m = N - i
    res = 0

    cnt, nn = count(n, 9)
    n = nn
    res += cnt

    cnt, mm = count(m, 6)
    m = mm
    res += cnt

    res += n + m
    ans = min(ans, int(res))

print(ans)


N = int(input())

INF = 10 ** 9


def base(x, y):
    ret = 0
    while x >= y:
        ret += x % y
        x //= y
    ret += x
    return ret
        

def main():
    ans = INF
    for i in range(N + 1):
        v = base(i, 6) + base(N - i, 9)
        ans = min(ans, v)
    print(ans)


def __starting_point():
    main()

__starting_point()

N = int(input())

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

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

#print(pow6)
#print(pow9)
dp[0] = 0
for i in range(N+1):
    dp[i] = min(dp[i], dp[i-1]+1)
    for j in pow6:
        if i-j>=0:
            dp[i] = min(dp[i], dp[i-j] + 1)
    for j in pow9:
        if i-j>=0:
            dp[i] = min(dp[i], dp[i-j] + 1)

#print(dp)
print((dp[-1]))


#解説見ながら

n = int(input())

ans = n

for i in range(n+1) :
    c = 0
    a = n-i
    while a>0 :
        c += a%6
        a = a//6
    a = i
    while a>0 :
        c += a%9
        a = a//9
    if c<ans :
        ans = c

print(ans)

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

# In[1]:


N = int(input())


# In[12]:


ans = N
for i in range(N+1):
    x = 0
    y = i
    while y:
        x += y%6
        y //= 6
    y = N-i
    while y:
        x += y%9
        y //= 9
    ans = min(x, ans)
print(ans)


# In[ ]:






N = int(input())
dp = [0] * 10 ** 6
for i in range(1, N + 1):
    power = 6
    dp[i] = i
    while power <= i:
        dp[i] = min(dp[i], dp[i - power] + 1)
        power *= 6
    power = 9
    while power <= i:
        dp[i] = min(dp[i], dp[i - power] + 1)
        power *= 9
print(dp[N])

n=int(input())
import bisect
l=[1,6,9,36,81,216,729,1296,6561,7776,46656,59049]
def draw(x,y):
    ind=bisect.bisect_right(l,x)-1
    if ind==0:
        return y+x
    else:
        y += 1
        x1=x-l[ind]
        x2=x-l[ind-1]
        if x1==0 or x2==0:
            return y
        else:
            return(min(draw(x1,y),draw(x2,y)))
print(draw(n,0))

import numpy as np

def chmin(dp, i, a):
    if dp[i] < a:
        return
    else:
        dp[i] = a
        return

N = int(input())

dp = np.array([i for i in range(N+1)]) # 1円ずつ引き出したときの回数で初期化

for i in range(N+1):
    x = 1
    while i + 6**x <= N:
        chmin(dp, i + 6**x, dp[i]+1)
        if i + 9**x <= N:
            chmin(dp, i + 9**x, dp[i]+1)
        x += 1

print(dp[N])

# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N):
    import math
    coins = [1] + \
            [6 ** i for i in range(1, int(math.log(N, 6)) + 2)] + \
            [9 ** i for i in range(1, int(math.log(N, 9)) + 2)]
    coins.sort()
    dp = [0] + [math.inf] * N
    for i in range(1, N + 1):
        for c in coins:
            if i - c < 0:
                break
            dp[i] = min(dp[i], dp[i - c] + 1)
    print((dp[N]))


def __starting_point():
    N = int(input())
    solve(N)

    # # test
    # from random import randint
    # from func import random_str
    # for i in range(1, 100 + 1):
    #     solve(i)

__starting_point()

from functools import lru_cache

def main():
    n=int(input())
    dp=[float("inf")]*(n+1)
    dp[0]=0
    for i in range(n):
        dp[i+1] = min(dp[i+1], dp[i]+1)
        j,k = 6,9
        while i+j <= n:
            dp[i+j] = min(dp[i+j], dp[i]+1)
            j *= 6
        while i+k <= n:
            dp[i+k] = min(dp[i+k], dp[i]+1)
            k *= 9
    print(dp[-1])

def __starting_point():
    main()
__starting_point()

def f(a,x):
    l=[]
    while x!=0:
        l.append(x%a)
        x//=a
    return sum(l)

def main():
    N=int(input())
    MIN=100000
    for k in range(N+1):
        MIN=min(MIN,f(9,k)+f(6,(N-k)))
    return MIN

def __starting_point():
    print(main())
__starting_point()

import sys

N = int(sys.stdin.readline())
s_max = N // 6
n_max = N // 9

s_n = 6
s_c = 0
while s_n < N:
    s_n *= 6
    s_c += 1
# print(6**s_c)

n_n = 9
n_c = 0
while n_n < N:
    n_n *= 9
    n_c += 1
# print(9**n_c)

ans = float("inf")
for i in range(s_max+1):
    # print("#####", i, "#####")
    j = (N - 6*i) // 9
    o = N - 6*i - 9*j
    if o < 0:
        continue
    tmp_ans = o + i + j
    ans = min(ans, tmp_ans)
    # i, jをx乗で差し替えて最小の操作回数を求める
    tmp = 6 ** s_c
    while 0 < tmp:
        r = i // tmp
        i -= r*tmp
        # print("i", i)
        tmp_ans += r - r*tmp
        tmp //= 6

    tmp = 9 ** n_c
    while 0 < tmp:
        r = j // tmp
        j -= r*tmp
        # print("j", j)
        tmp_ans += r - r*tmp
        tmp //= 9

    # print(tmp_ans)
    ans = min(ans, tmp_ans)

print(ans)

import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()

    # 引き出せる金額リスト
    l = [1]
    a = 6
    b = 9
    while a <= N or b <= N:
        if a < b:
            l.append(a)
            a *= 6
        else:
            l.append(b)
            b *= 9

    # ナップザック
    dp = [INF] * (N + 1)
    dp[0] = 0
    for i in range(1, N + 1):
        for j in l:
            if i - j >= 0:
                dp[i] = min(dp[i - j] + 1, dp[i])

    print((dp[-1]))

def __starting_point():
    resolve()

__starting_point()

import math
N=int(input())
dp=[math.inf for _ in range(N+1)]
dp[0]=0
for i in range(1,N+1):
	p=1
	q=1
	while p<=i:
		dp[i]=min(dp[i],dp[i-p]+1)
		p*=6
	while q<=i:
		dp[i]=min(dp[i],dp[i-q]+1)
		q*=9
print(dp[N])

def solve():
    n = int(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())

# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(100000)  # RecursionError対策

def func(n):
    if n==0:
        return 0 # 終端条件
    if memo[n] != -1:
        return memo[n] # 探索済み

    res = n

    for i in range(1,n+1):
        pow6 = 6 ** i
        if pow6 > n:
            break
        res = min(res, func(n-pow6)+1)

    for i in range(1,n+1):
        pow9 = 9 ** i
        if pow9 > n:
            break
        res = min(res, func(n-pow9)+1)

    memo[n] = res
    return res


N = int(input())

max_n = 110000
memo = [-1] * max_n
ans = func(N)
print(ans)


n=int(input())
def f(x):
    ans=0
    while x>0:
        ans+=x%6
        x=x//6
    return ans
def g(x):
    ans=0
    while x>0:
        ans+=x%9
        x=x//9
    return ans
ans=n
for i in range(n+1): 
  ans=min(ans,f(i)+g(n-i))
print(ans)

n = int(input())

dp = [1000000 for _ in range(n+1)]
dp[0] = 0

for i in range(0,n):
  if i+1 <= n:
    dp[i+1] = min(dp[i+1], dp[i] + 1)
  for j in range(1,7):
    a = 6**j
    if i+a <= n:
      dp[i+a] = min(dp[i+a], dp[i] + 1)
  for k in range(1,6):
    a = 9**k
    if i+a <= n:
      dp[i+a] = min(dp[i+a], dp[i] + 1)

print(dp[n])

N = int(input())
ans = [0] * 100001
for i in range(6):
    ans[i] = i
s = 6
n = 9
for i in range(6,N+1):
    p = i%3
    if p != 0:
        ans[i] =  ans[i-p] + p
    else:
        if i == s*6:
            s *= 6
        if i == n*9:
            n *= 9
        ans[i] = min(ans[i-s]+1,ans[i-n]+1)
print(ans[N])

N=int(input())
dp=[0]*(100000+1)
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-a]+1,dp[i-b]+1)
  else:
    dp[i]=min([dp[i-a]+1,dp[i-b]+1,dp[i-c]+1])
print((dp[N]))


#coding: utf-8

N = int(input())

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

dp[0] = 0

for i in range(N+1):
    for amount in (6, 9):
        a = amount 
        while i + a < len(dp):
            dp[i + a] = min(dp[i + a], dp[i] + 1)
            a *= amount

print((dp[N]))



N = int(input())
dp = [i for i in range(N + 1)]

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

    index = 1
    while 9 ** index <= i:
        dp[i] = min(dp[i], dp[i - (9 ** index)] + 1)
        index += 1

print(dp[N])

n = int(input())

dp=[0]
#dp[n]を求める

for i in range(1,n+1):
    c = []
    c.append(dp[i-1]+1)
    p6=1
    while i >=6**p6:
        c.append(dp[i-6**p6]+1)
        p6 +=1

    p9=1
    while i >=9**p9:
        c.append(dp[i-9**p9]+1)
        p9 +=1

    dp.append(min(c))

print(dp[n])

N=int(input())
ans=N
for i in range(N+1):
  cnt=0
  t=i
  while t>0:
    cnt+=t%6
    t//=6
  j=N-i
  while j>0:
    cnt+=j%9
    j//=9
  ans=min(ans,cnt)
print(ans)


N = int(input())
dp = [0] * 100500

import math
dp[1] = 1
for i in range(2, N+1):
    dp[i] = dp[i-1] + 1
    for j in range(math.floor(math.log(i, 6)) + 1):
        dp[i] = min(dp[i], dp[i - 6 ** j] + 1)
    for k in range(math.floor(math.log(i, 9) + 0.00000001) + 1):
        dp[i] = min(dp[i], dp[i - 9 ** k] + 1)


print((dp[N]))


N = int(input())

patterns = []
patterns.append(1)

f = 6
a = 6
while a <= N:
    patterns.append(a)
    a *= f

f = 9
a = 9
while a <= N:
    patterns.append(a)
    a *= f

dp = [float('inf')] * (N + 1)
dp[0] = 0
for n in range(1, N + 1):
    for p in patterns:
        if n - p < 0:
            continue

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


print((dp[-1]))


n = int(input())

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

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


import numpy as np

n = int(input())

dp = np.full((n + 1), n + 1, dtype = int)
dp[0] = 0

loop = 0
while dp[n] > n:
    tmp1 = np.full(n + 1, n + 1, dtype = int)
    tmp6 = np.full(n + 1, n + 1, dtype = int)
    tmp9 = np.full(n + 1, n + 1, dtype = int)    
    tmp1[1:] = dp[: -1] + 1
    j = 1
    while 6 ** j <= n:
        tmp6_pre = tmp6
        tmp6[6 ** j:] = dp[:-6 ** j] + 1
        tmp6 = np.minimum(tmp6_pre, tmp6)
        j += 1
    k = 1
    while 9 ** k <= n:
        tmp9_pre = tmp9
        tmp9[9 ** k:] = dp[: -9 ** k] + 1
        tmp9 = np.minimum(tmp9_pre, tmp9)
        k += 1
    tmp1 = np.minimum(tmp1, tmp6)
    tmp1 = np.minimum(tmp1, tmp9)
    dp = np.minimum(dp, tmp1)
    loop += 1
print(dp[n])

n = int(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])

N = int(input())

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

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

    power = 9
    while power <= i:
        dp[i] = min(dp[i], dp[i-power] + 1)
        power *= 9

print((dp[-1]))


# -*- coding: utf-8 -*-


def main():
    n = int(input())
    dp = [float('inf') for _ in range(n + 100)]
    dp[0] = 0

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

        six = 6

        while six <= i:
            dp[i] = min(dp[i - six] + 1, dp[i])
            six *= 6

        nine = 9

        while nine <= i:
            dp[i] = min(dp[i - nine] + 1, dp[i])
            nine *= 9

    print((dp[n]))


def __starting_point():
    main()

__starting_point()

n=int(input())

a=[1,6,36,216,1296,7776,46656,9,81,729,6561,59049]

inf=float('inf')
dp=[inf for _ in range(100000+1)]
dp[0]=0
dp[1]=1

for x in range(1,100001):
  for b in a:
    if b<=x:
      dp[x]=min(dp[x],dp[x-b]+1)
      
print((dp[n]))


n = int(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])

n = int(input())
index6, index9, num6, num9 = 0, 0, 0, 0
INF = float('inf')    
a = [INF for _ in range(n+1)]
a[n] = 0

for i in range(n, -1, -1):
    if a[i] == INF:
        continue
    j = 1
    while i-9**j >= 0:
        a[i-9**j] = min(a[i]+1, a[i-9**j])
        j += 1
    j = 1
    while i-6**j >= 0:
        a[i-6**j] = min(a[i]+1, a[i-6**j])
        j += 1
    if i-1 >= 0:
        a[i-1] = min(a[i]+1, a[i-1])
print((a[0]))


N = int(input())
dp = [i for i in range(N+1)]
 
for i in range(1, 7):
    for j in range(9**i, N+1):
        dp[j] = min(dp[j], dp[j-9**i]+1)
for i in range(1, 7):
    for j in range(6**i, N+1):
        dp[j] = min(dp[j], dp[j-6**i]+1)
print(dp[N])

import sys
readline = sys.stdin.readline

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

for i in range(10,0,-1):
  for j in range(N + 1):
    if dp[j] != INF:
      if j + 6 ** i <= N:
        dp[j + 6 ** i] = min(dp[j + 6 ** i], dp[j] + 1)
      if j + 9 ** i <= N:
        dp[j + 9 ** i] = min(dp[j + 9 ** i], dp[j] + 1)

ans = INF
for i in range(len(dp) - 1, -1, -1):
  money = dp[i] + N - i
  if ans > money:
    ans = money
        
print(ans)

N = int(input())
#初期化
dp=[0]*N
for i in range(10):
  if 6**i-1 < N:
    dp[6**i-1] = 1
for i in range(10):
  if 9**i-1 < N:
    dp[9**i-1] = 1

#dp
for k in range(1,N):
  if dp[k] != 1:
    dp_list = []
    for j in range(10):
      if k >= 6**j:
        dp_list.append(dp[k-6**j])
      if k >= 9**j:
        dp_list.append(dp[k-9**j])
  
    dp[k] = 1+min(dp_list)

print(dp[N-1]%1000000007)

N = int(input())
dp = [N] * 110000
dp[0] = 0

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

print(dp[N])

n = int(input())
dp = [float('inf')]*100100
dp[0] = 0

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

    power = 1
    while power <= n:
        dp[i] = min(dp[i], dp[i-power]+1)
        power *= 9

print(dp[n])

N = int(input())

ans = N
for i in range(N+1):
    x = 0
    y = i
    while y:
        x += y%6
        y //= 6
    y = N-i
    while y:
        x += y%9
        y //= 9
    ans = min(x, ans)

print(ans)

#!/usr/bin/env python3
def main():
    import numpy as np

    N = int(input())

    can = [1]
    limit = 100
    for i in range(1, limit):
        six = 6 ** i
        if six <= N:
            can.append(six)
        nine = 9 ** i
        if nine <= N:
            can.append(nine)
        if six > N and nine > N:
            break

    INF = 10 ** 9
    dp = np.full(N + 1, INF, dtype=np.int32)
    dp[0] = 0
    for c in can:
        for _ in range(9):
            dp[c:] = np.minimum(dp[c:], dp[:-c] + 1)
    print((dp[-1]))


def __starting_point():
    main()

__starting_point()

n = int(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])

N = int(input())
kouho = [1]
for i in range(1, 100):
    t = 6 ** i
    if t <= 10 ** 5:
        kouho.append(t)
    else:
        break
for i in range(1, 100):
    t = 9 ** i
    if t <= 10 ** 5:
        kouho.append(t)
    else:
        break
# print(kouho)

kouho.sort(reverse=True)
kouho_cnt = len(kouho)
INF = 10 ** 9
dp = [INF] * ((10 ** 5) + 1)
dp[0] = 0
for i in range(1, N + 1):
    for k in kouho:
        if i - k >= 0:
            dp[i] = min(dp[i], dp[i - k] + 1)
#print(dp)
print((dp[N]))
# cur = 0
# ans = 0
# while N > 0:
#     if N >= kouho[cur]:
#         N -= kouho[cur]
#         ans += 1
#     else:
#         cur += 1
# print(ans)


from collections import deque
n=int(input())
table=[-1]*100001
table[0]=0
d=deque()
d.append(0)
can=[]
can.append(1)
for i in range(1,50):
    if 6**i<=100000:
        can.append(6**i)
    else:
        break
    if 9**i<=100000:
        can.append(9**i)
while d:
    w=d.popleft()
    for i in can:
        if w+i>100000:
            continue
        if not table[w+i]==-1:
            continue
        table[w+i]=table[w]+1
        d.append(w+i)
print(table[n])

n=int(input())
dp=[0]*(n+1)
for i in range(n+1):
  t=i
  count=0
  while t>0:
    count+=t%6
    t//=6
  t=n-i
  while t>0:
    count+=t%9
    t//=9
  dp[i]=count
print(min(dp))

n = int(input())

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

lst = [0] * 1000000
for i in range(n+1):
    can = i
    for m in money:
        if i-m >= 0:
            can = min(can, lst[i-m] + 1)
        else:
            break
    lst[i] = can

print(lst[n])

n=int(input())
tab=[10**8 for i in range(n+1)]
tab[0]=0

for i in range(n):
    now=1
    while i+now<=n:
        tab[i+now]=min(tab[i+now],tab[i]+1)
        now*=6
    now=9
    while i+now<=n:
        tab[i+now]=min(tab[i+now],tab[i]+1)
        now*=9

print(tab[-1])

n = int(input())

dp = [0]*(n+10)

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

for i in range(1,n+1):
    k = 6
    while k <=n:
        if i-k>=0:
            dp[i] = min(dp[i],dp[i-k]+1)
            #print(dp[i-1],dp[i-k])
        k *=6

        #print(dp)
    k = 9
    while k <=n:
        if i-k>=0:
            dp[i] = min(dp[i],dp[i-k]+1)
           
        k *=9

print(dp[n])

n = int(input())
a_6 = [int(6**i) for i in range(1,10) if 6**i <= 10**5]
a_9 = [int(9**i) for i in range(1,10) if 9**i <= 10**5]
a = [1]
for i in a_6:
    a.append(i)
for i in a_9:
    a.append(i)
a.sort(reverse = True)
dp = [i for i in range(n+1)]
dp[0] = 0
for i in range(n+1):
    for j in a:
        if i+j <= n:
            dp[i+j] = min(dp[i+j],dp[i]+1)
print(dp[n])

n = int(input())

dp = [float('inf')] * (n+1)

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

N = int(input())

dp = [N] * (N+1)

dp[0] = 0

for i in range(1,N+1):
    dp[i] = dp[i-1] + 1
    for k in range(1,10):
        if i < 6**k:
            break
        else:
            dp[i] = min(dp[i],dp[i-6**k]+1)
            
    for k in range(1,10):
        if i < 9**k:
            break
        else:
            dp[i] = min(dp[i],dp[i-9**k]+1)

print(dp[N])

from collections import deque

N = int(input())
units = set()
i = 0
while 6 ** i <= N:
    units.add(6 ** i)
    i += 1
i = 0
while 9 ** i <= N:
    units.add(9 ** i)
    i += 1
units = sorted(units)

mins = list(range(N + 1))
for i in range(1, len(units)):
    unit = units[i]
    for j in range(N):
        if j + unit < len(mins) and mins[j] + 1 < mins[j + unit]:
            mins[j + unit] = mins[j] + 1
print(mins[N])

from math import log

N=int(input())

dp = [0]*(N+1)

e6 = 0
e9 = 0
for i in range(1,N+1):
  for j in range(e6,12):
    if 6**j>i:
      e6 = j-1
      break
  for j in range(e9,12):
    if 9**j>i:
      e9 = j-1
      break
  dp[i] = min(dp[i-1],dp[i-int(6**e6)],dp[i-int(9**e9)])+1
  
print(dp[-1])

N = int(input())
ans = 100000
for A in range(N+1):
    num = 0
    B = N-A
    while A != 0:
        num  += A % 6
        A = A//6
    while B != 0:
        num  += B % 9
        B = B//9
    ans = min(ans,num)
print(ans)

import math
n = int(input())

dp = [math.inf] * (n+1)

for i in range(n+1):
    dp[i] = min(dp[i],i)
    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)
print((dp[n]))


n = int(input())


def n_base_digit_sum(v, a):
    # 10baseの自然数vをn進数で表した時の桁和
    res = 0
    while v != 0:
        res += v % a
        v = v//a
    return res


ans = n
for i in range(n+1):
    ans = min(ans, n_base_digit_sum(i, 6)+n_base_digit_sum(n-i, 9))

print(ans)


from collections import deque
n = int(input())
cnt = 0
d = {n:0}
q = deque([n])
while q[0] > -5:
    t = q.popleft()
    cnt = d[t]
    # print(t)
    a = 1
    b = 1
    while 6**a <= t:
        a += 1
    while 9**b <= t:
        b += 1
    a -= 1
    b -= 1
    # print(t,a,b)
    if t-6**a in d:
        if d[t-6**a] > cnt+1:
            d[t-6**a] = cnt+1
            q.append(t-6**a)
    else:
        d[t-6**a] = cnt+1
        q.append(t-6**a)
    if t-9**b in d:
        if d[t-9**b] > cnt+1:
            d[t-9**b] = cnt+1
            q.append(t-9**b)
    else:
        d[t-9**b] = cnt+1
        q.append(t-9**b)
    # print(q)
print((d[0]))
# print(d)


import sys;      from decimal import Decimal
import math;     from itertools import combinations, product
import bisect;   from collections import Counter, deque, defaultdict

# sys.setrecursionlimit(10 ** 6)
MOD = 10 ** 9 + 7
INF = 10 ** 9
PI = 3.14159265358979323846

def read_str():      return sys.stdin.readline().strip()
def read_int():      return int(sys.stdin.readline().strip())
def read_ints():     return map(int, sys.stdin.readline().strip().split())
def read_str_list(): return list(sys.stdin.readline().strip().split())
def read_int_list(): return list(map(int, sys.stdin.readline().strip().split()))
def lcm(a: int, b: int) -> int: return (a * b) // math.gcd(a, b)

import numpy as np
from numba import njit, i8

@njit(i8(i8))
def dp(N):
    dp = np.full(100005, INF, dtype=np.int64)
    dp[0] = 0
    
    for n in range(1, N + 1):
        pow6 = 1
        while pow6 <= n:
            dp[n] = min(dp[n], dp[n - pow6] + 1)
            pow6 *= 6
        pow9 = 1
        while pow9 <= n:
            dp[n] = min(dp[n], dp[n - pow9] + 1)
            pow9 *= 9
    
    return dp[n]

def Main():
    n = read_int()
    print(dp(n))    

if __name__ ==  '__main__':
    Main()

# 6**6 < 10**5 < 6**7
# 9**5 < 10**5 < 9**6

def solve(n):
    if n < 6:
        return n
    elif n < 9:
        return n - 5
    else:
        now_6 = 6
        while now_6 * 6 <= n:
            now_6 *= 6

        now_9 = 9
        while now_9 * 9 <= n:
            now_9 *= 9
        
        ans = float('inf')
        for i in range(n // now_6 + 1):
            cnt = i + (n - now_6*i) // now_9 + solve((n - now_6*i) % now_9)
            if cnt < ans:
                ans = cnt
        
        return ans

print(solve(int(input())))

def Ten_to_n(X, n):
    if (int(X/n)):
        return Ten_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

n=int(input())
wit=[1]
res=n
for i in range(n+1):
    cnt=0
    j=n-i
    si=Ten_to_n(i,6)
    for i in si:
        cnt+=int(i)
    ni=Ten_to_n(j,9)
    for i in ni:
        cnt+=int(i)
    res=min(res,cnt)
print(res)

N = int(input())

# dp[i] := i円引き出すのに必要な最小操作回数
dp = [float('inf')] * (N + 1)
dp[0] = 0
operates = sorted([1, 6, 36, 216, 1296, 7776, 46656, 9, 81, 729, 6561, 59049])
for i in range(N):
    for ope in operates:
        if i + ope > N:
            break
        dp[i + ope] = min(dp[i + ope], dp[i] + 1)

print((dp[N]))


# C - Strange Bank
import math
N = int(input())

INF = 10**10

dp = [0]*(N+1)

for i in range(N+1):
    dp[i] = INF

dp[0] = 0
W = [1,6,9]

# dp{N} に最小値を記録する
# 今の金額
for v in range(1,N+1):
    for w in range(3):
        for x in range(int(math.log(N+1,6))+1):
            # 選ぶ数
            n = W[w]**x
            if v-n < 0:
                continue
            dp[v] = min(dp[v],dp[v-n] + 1)
print(dp[N])

import math

n = int(input())
t = 100000 + 10

dp = [10**18] * (t+10)
dp[0] = 0

for i in range(1, t+1):
    dp[i] = dp[i-1] + 1

for i in range(t-1):
    s = t-i
    for j in range(int(math.log(s, 9)+1)):
        dp[i+9**j] = min(dp[i+9**j], dp[i]+1)
    for k in range(int(math.log(s, 6)+1)):
        dp[i+6**k] = min(dp[i+6**k], dp[i]+1)


print(dp[n])