CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))
 
arr6 = []
for i in range(10):
    arr6.append(6**i)

arr9 = []
for i in range(10):
    arr9.append(9**i)

ans = float("inf")
for i in range(n+1):
    count = 0
    tmp = i
    while tmp > 0:
        t = 0
        while arr6[t] <= tmp:
            t+=1

        tmp -= arr6[t-1]
        count += 1

    tmp = n-i
    while tmp > 0:
        t = 0
        while arr9[t] <= tmp:
            t += 1
        tmp -= arr9[t-1]
        count += 1

    ans = min(ans,count)

print(ans)


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

dp = [float("inf") for _ in range(100010)]
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]))
# Original solution
def main():
    N = int(eval(input()))
    dp = [-1]*(N + 1)
    dp[0] = 0
    multiples = [1]
    n = 9
    while n <= N:
        multiples.append(n)
        n *= 9
    n = 6
    while n <= N:
        multiples.append(n)
        n *= 6
    multiples.sort(reverse=True)
    # print(multiples)
    for ratio in reversed(multiples):
        for i in range(N + 1):
            c = dp[i]
            if c == - 1:
                continue
            if i + ratio > N:
                continue
            if dp[i + ratio] == -1 or dp[i + ratio] > c + 1:
                dp[i + ratio] = c + 1
    # print(dp)
    print((dp[N]))

if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(input()))
    ans = 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
        ans = min(ans, cc)
    print(ans)

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

x = int(eval(input()))
six_list = [6 ** i for i in range(7)]
nine_list = [9 ** i for i in range(6)]


def six_nine(n):
    if n == 0:
        return 0
    else:
        six = six_list[bisect.bisect_right(six_list, n) - 1]
        nine = nine_list[bisect.bisect_right(nine_list, n) - 1]
        return min(n // six + six_nine(n % six), 1 + six_nine(n - nine))


print((six_nine(x)))


# Optimized solution
import bisect

x = int(eval(input()))
six_list = [6 ** i for i in range(7)]
nine_list = [9 ** i for i in range(6)]


def six_nine(n):
    if n < 6:
        return n
    else:
        six = six_list[bisect.bisect_right(six_list, n) - 1]
        nine = nine_list[bisect.bisect_right(nine_list, n) - 1]
        return min(n // six + six_nine(n % six), 1 + six_nine(n - nine))


print((six_nine(x)))
# Original solution
import bisect


def six_nine(n):
    if n < 6:
        return n
    else:
        six = six_list[bisect.bisect_right(six_list, n) - 1]
        nine = nine_list[bisect.bisect_right(nine_list, n) - 1]
        return min(n // six + six_nine(n % six), 1 + six_nine(n - nine))


if __name__ == '__main__':
    x = int(eval(input()))
    six_list = [6 ** i for i in range(7)]
    nine_list = [9 ** i for i in range(6)]
    print((six_nine(x)))

# Optimized solution
import bisect

six_list = [6 ** i for i in range(7)]
nine_list = [9 ** i for i in range(6)]


def six_nine(n):
    if n < 6:
        return n
    else:
        six = six_list[bisect.bisect_right(six_list, n) - 1]
        nine = nine_list[bisect.bisect_right(nine_list, n) - 1]
        return min(n // six + six_nine(n % six), 1 + six_nine(n - nine))


if __name__ == '__main__':
    x = int(eval(input()))
    print((six_nine(x)))
# Original solution
import itertools
N = int(eval(input()))

base = [1]
for i in range(1, 7):
    if 6 ** i <= N:
        base.append(6 ** i)
for i in range(1, 6):
    if 9 ** i <= N:
        base.append(9 ** i)
ans = N
dp = [0] * (N + 1)
for i in range(1, N + 1):
    dp[i] = min([dp[i - j] + 1 if i - j >= 0 else j for j in base])

print((dp[N]))

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

def base_n(x, n):
    results = 0
    while x > 0:
        results += x % n
        x //= n
    return results

print((min([base_n(i, 6) + base_n(N - i, 9) for i in range(N + 1)])))
# Original solution
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]))

# Optimized solution
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]))
# Original solution
def g(n, a):
    for i in range(1, 100):
        if a**i > n:
            return a**(i-1)
    return -1

def solve(n):
    if n < 6:
        return n
    a = g(n, 6)
    b = g(n, 9)
    return min(solve(n-a)+1, solve(n-b)+1)

# assert solve(127) == 4
# assert solve(3) == 3
# assert solve(44852) == 16

print(solve(eval(input())))

# Optimized solution
def g(n, a):
    for i in range(1, 100):
        if a**i > n:
            return a**(i-1)
    return -1

dp = {}
def solve(n):
    if n < 6:
        return n
    if not n in dp:
        a = g(n, 6)
        b = g(n, 9)
        dp[n] = min(solve(n-a)+1, solve(n-b)+1)
    return dp[n]

# assert solve(127) == 4
# assert solve(3) == 3
# assert solve(44852) == 16

print(solve(eval(input())))
# Original solution
N = int(eval(input()))
B6 = [6**i for i in range(1,7)]
B9 = [9**i for i in range(1,6)]
L = B6 + B9
L = sorted(list(set(L)))
dp = [i for i in range(N+1)]
for i in L :
    for j in range(i,(N+1)) :
        dp[j] = min(dp[j-i]+1,dp[j])
ans = dp[N]
print(ans)


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

dp = [i for i in range(N+1)]
B6 = [6**i for i in range(1,8)]
B9 = [9**i for i in range(1,7)]
L = B6 + B9
L = sorted(list(set(L)))

for i in L :
    for j in range(i, N+1) :
        dp[j] = min(dp[j-i]+1, dp[j])
print((dp[-1]))
# Original solution
N = int(eval(input()))
dp = [0 for i in range(N+1)]
dp[1] = 1
roku = 0
kyu = 0
for i in range(2, N+1):
    if i == 6**(roku+1):
        roku += 1
    if i == 9**(kyu+1):
        kyu += 1
    kouho = [1]
    for j in range(roku):
        kouho.append(6**(j+1))
    for j in range(kyu):
        kouho.append(9**(j+1))
    size = len(kouho)

    small = 1000000
    for j in range(size):
        if dp[i - kouho[j]] + 1 < small:
            small = dp[i - kouho[j]] + 1
    dp[i] = small
print((str(dp[N])))

# Optimized solution
N = int(eval(input()))
dp = [0 for i in range(N+1)]
dp[1] = 1
kazu = [1]
i = 6
while i <= N:
    kazu.append(i)
    i*=6
i = 9
while i <= N:
    kazu.append(i)
    i*=9
kazu.sort()
kazulazor = len(kazu)
temp = [1]
lentemp = 1
index = 1
for i in range(2, N+1):
    kouho = []
    if index < kazulazor and i == kazu[index]:
        temp.append(i)
        index += 1
        lentemp += 1
    for j in range(lentemp):
        kouho.append(dp[i-temp[j]]+1)
    dp[i] = min(kouho)
print((str(dp[N])))
# Original solution
#公式解法見て書いた
iN = int(eval(input()))

a6 = [0]*7
for i in range(7):
    a6[i] = 6 ** i
a6.reverse()
a9 = [0]*6
for i in range(6):
    a9[i] = 9 ** i
a9.reverse()

iAns = iN
for i in range(iN+1):
    iD = iN - i
    iC = 0
    for j in range(7):
        if i:
            iG = a6[j]
            if i >= iG:
                iC += (i // iG)
                i %=iG
    for j in range(6):
        if iD :
            iG = a9[j]
            if iD >= iG:
                iC += (iD // iG)
                iD %= iG
    if iAns > iC:
        iAns = iC
print(iAns)


# Optimized solution
#公式解法見て書いた
iN = int(eval(input()))

a6 = [0]*7
for i in range(7):
    a6[i] = 6 ** i
a6.reverse()
a9 = [0]*6
for i in range(6):
    a9[i] = 9 ** i
a9.reverse()

iAns = iN
for i in range(iN+1):
    iD = iN - i
    iC = 0
    for j in range(7):
        if i:
            iG = a6[j]
            if i >= iG:
                iC += (i // iG)
                i %=iG
    if iAns <= iC:
        continue
    for j in range(6):
        if iD :
            iG = a9[j]
            if iD >= iG:
                iC += (iD // iG)
                iD %= iG
    if iAns > iC:
        iAns = iC
print(iAns)
# Original solution
import math

N=int(eval(input()))
number_of_withdrawal=[100000]

# def floor(float_number):
#     integer = 0
#     while integer < float_number:
#         integer+=1
#     if integer == float_number:
#         return integer
#     else:
#         integer-=1
#         return integer

for money_6 in range(0, N+1):
    money_9 = N-money_6

    i=0
    count_6_pow_number=0
    while pow(6,i)<=money_6:
        count_6_pow_number += (math.floor(money_6/pow(6,i)))%6
        i+=1
    j=0
    count_9_pow_number=0
    while pow(9,j)<=money_9:
        count_9_pow_number += (math.floor(money_9/pow(9,j)))%9
        j+=1

    # print(money_6, money_9, sum(count_6_pow_number), sum(count_9_pow_number))
    number_of_withdrawal.append(count_6_pow_number+count_9_pow_number)

print((min(number_of_withdrawal)))

# Optimized solution
N=int(eval(input()))
money_list=[1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
dp=[0]
for i in range(1,N+1):
    dp.append(100000)

for i in range(N):
    for money in money_list:
        if i+money <= N:
            dp[i+money]=min(dp[i+money],dp[i]+1)
print((dp[N]))
# Original solution
N=int(eval(input()))
money_list=[1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
dp=[0]
for i in range(1,N+1):
    dp.append(100000)

for i in range(N):
    for money in money_list:
        if i+money <= N:
            dp[i+money]=min(dp[i+money],dp[i]+1)
print((dp[N]))

# Optimized solution
N=int(eval(input()))
money_list=[1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
dp=[0]
for i in range(1,N+1):
    dp.append(100000)

for i in range(1,N+1):
    for money in money_list:
        if i-money >= 0:
            dp[i]=min(dp[i-money]+1,dp[i])
print((dp[N]))
# Original solution
N = int(eval(input()))

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

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

sixs = [6**i for i in range(1,7)]
nines = [9**i for i in range(1,6)]
tmp = {}

def dfs(n):
  if(n < 6):
    return n

  if(n in tmp):
    return tmp[n]

  m = 10**9+7
  if(n >= 9):
    max_nine = max(nine for nine in nines if nine <= n)
    if(n%max_nine == 0):
      m = min(m, n // max_nine)
    else:
      m = min(m, dfs(n - max_nine) + 1)

  max_six = max(six for six in sixs if six <= n)
  if(n%max_six == 0):
    m = min(m, n // max_six)

  tmp[n] = min(m, dfs(n - max_six) + 1)
  return tmp[n]

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

sixs = [6**i for i in range(1,7)]
nines = [9**i for i in range(1,6)]
tmp = {}

def dfs(n):
  if(n < 6):
    return n

  if(n in tmp):
    return tmp[n]

  m = 10**9+7
  if(n >= 9):
    max_nine = max(nine for nine in nines if nine <= n)
    if(n%max_nine == 0):
      m = min(m, n // max_nine)
    else:
      m = min(m, dfs(n - max_nine) + 1)

  max_six = max(six for six in sixs if six <= n)
  if(n%max_six == 0):
    m = min(m, n // max_six)

  tmp[n] = min(m, dfs(n - max_six) + 1)
  return tmp[n]

print((dfs(N)))

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

nine = [9**i for i in range(7, 0, -1)]
six = [6**i for i in range(8, 0, -1)]
tmp = {}

def f(n):
  if(n < 6):
    return n
  if(n in tmp):
    return tmp[n]
  
  m = 1e9+7
  if(n >= 9):
    mn = max(x for x in nine if x <= n)
    if(n%mn == 0):
      m = min(m, n//mn)
    else:
      m = min(m, f(n-mn)+1)
  ms = max(x for x in six if x <= n)
  if(n%ms == 0):
    m = min(m, n//ms)
  tmp[n] = min(m, f(n-ms)+1)
  return tmp[n]
print((f(N)))
# Original solution
N = int(eval(input()))

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

# Optimized solution
from itertools import count, takewhile
N = int(eval(input()))

def dfs(n, d={}):
  if(n < 6):
    return n
  if(n in d):
    return d[n]
  mn = max(takewhile(lambda x: x<=n, (9**i for i in count())))
  ms = max(takewhile(lambda x: x<=n, (6**i for i in count())))
  d[n] = min(dfs(n-mn), dfs(n-ms))+1
  return d[n]

print((dfs(N)))
# Original solution
N = int(eval(input()))
dp = dict()

dp[0] = 0
# 貰う DP --- dp[n] に遷移を集める
for n in range(1, N+1):
  dp[n] = dp[n-1] + 1
  
  # メモ化
  x = 6
  while x <= n:
    dp[n] = min(dp[n], dp[n-x]+1)
    x *= 6
  
  x = 9
  while x <= n:
    dp[n] = min(dp[n], dp[n-x]+1)
    x *= 9
    
  # dp[n] = ans
  
print((dp[N]))

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

max_N = 1000000
memo = [False]*max_N
import sys
import threading
sys.setrecursionlimit(100000)
threading.stack_size(128*1024*1024)


# 貰う DPではなく、再帰を使ったメモ化再帰
from functools import lru_cache

lru_cache(maxsize=100000)
def f(n):
	if n < 6:
		return n
	if memo[n]:
		# print(f"at n: {n}, memo[n]:", memo[n])
		return memo[n]
	
	ans = n
	# メモ化
	x = 6
	while x <= n:
		ans = min(ans, f(n-x)+1)
		x *= 6
	
	x = 9
	while x <= n:
		ans = min(ans, f(n-x)+1)
		x *= 9
	
	memo[n] = ans
	return ans

def main():
	print((f(n)))
	
threading.Thread(target=main).start()
# Original solution
N = int(eval(input()))
memo = [N]*(N+1)
unit = [1]
for b in [6,9]:
    u = b
    while u <= N:
        unit.append(u)
        u *= b
unit.sort()
cur = set({0})
for i in range(1,N+1):
    prev = cur
    cur = set()
    for p in prev:
        for u in unit:
            c = p + u
            if c == N:
                print(i)
                exit()
            elif c > N:
                break
            elif memo[c] > i:
                cur.add(c)
                memo[c] = i

# Optimized solution
from bisect import bisect_right
N = int(eval(input()))
unit = [1]
for b in [6,9]:
    u = b
    while u <= N:
        unit.append(u)
        u *= b
unit.sort()
Nu = len(unit)

ans = N
state_pool = [(0, 0, Nu-1)]
while state_pool:
    n, i, pk = state_pool.pop()
    if N - n >= (ans-i)*unit[pk]:
        continue
    sk = bisect_right(unit, N-n, 0, pk+1)
    for k in range(sk):
        u = unit[k]
        c = n + u
        if c == N:
            if i + 1 < ans:
                ans = i + 1
        else:
            state_pool.append((c, i+1, k))
print(ans)
# Original solution
n=int(eval(input()))
dp=[float("INF")]*(n+1)
dp[0]=0
temp=6
cnt=0
while temp<=n:
  cnt+=1
  dp[temp]=1
  temp*=6
l=[0,0]
l[0]=cnt
temp=9
cnt=0
while temp<=n:
  cnt+=1
  dp[temp]=1
  temp*=9
l[1]=cnt
for j,v in zip(l,[6,9]):
  for i in range(j+1):
    h=pow(v,i)
    for d in range(h,n+1):
      dp[d]=min(dp[d],dp[d-h]+1)
print((dp[-1]))

# Optimized solution
n=int(eval(input()))
ans=float("INF")
for i in range(n+1):
  d=0
  temp=i
  while (temp>0):
    d+=temp%6
    temp//=6
  temp=n-i
  while(temp>0):
    d+=temp%9
    temp//=9
  ans=min(d,ans)
print(ans)
# Original solution
N = int(eval(input()))
dp = [N]*(N+1)
dp[0] = 0

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

print((dp[-1]))

# Optimized solution
N = int(eval(input()))
dp = [N]*(N+1)
dp[0] = 0

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

print((dp[-1]))
# Original solution
n=int(eval(input()))
inf=float("inf")
DP=[inf]*(n+1)
DP[0]=0

for i in range(1,n+1):
    DP[i]=DP[i-1]+1
    for point in (6,9):
        cnt=1
        while point**cnt<=i:
            DP[i]=min(DP[i-1]+1,DP[i-point**cnt]+1,DP[i])
            cnt +=1
            
print((DP[n]))

# Optimized solution
n=int(eval(input()))
inf=float("inf")
DP=[inf]*(n+1)
DP[0]=0

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

print((DP[n]))
# Original solution
n=int(eval(input()))
inf=float("inf")
DP=[inf]*(n+1)
DP[0]=0

for i in range(1,n+1):
    DP[i]=DP[i-1]+1
    for P in (6,9):
        cnt=1
        while P**cnt<=i:
            DP[i]=min(DP[i-1]+1,DP[i-P**cnt]+1,DP[i])
            cnt +=1
            
print((DP[n]))

# Optimized solution
inf=float("inf")
n=int(eval(input()))
DP=[inf]*(n+1)
DP[0]=0

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

print((DP[n]))
# Original solution
n=int(eval(input()))
s_6=[6**(6-i) for  i in range(6)] 
s_9=[9**(5-i) for  i in range(5)]
amari=[0,1,2,3,4,5,1,2,3,1,2,3,2,3]

best=float("inf")
for i in range(n+1):
    count_6=0
    n_6=i
    for j in range(len(s_6)):
        count_6+=n_6//s_6[j]
        n_6=n_6%s_6[j]

    n_9=n-i
    count_9=0
    for j in range(len(s_9)):
        count_9+=n_9//s_9[j]
        n_9=n_9%s_9[j]

    best=min(best, count_6+count_9+ amari[n_6+n_9])

print(best)

# Optimized solution
n = int(eval(input()))
ans = float("inf")
for n_nine in range(0, n//9 + 1):
    s = n - 9 * n_nine
    n_six = s // 6
    n_one = s - 6 * n_six
    n_nine2 = 0
    n_six2 = 0
    pow_nine = 5
    pow_six = 6
    
    while n_nine >= 9:
        if n_nine >= 9 ** (pow_nine - 1):
            n_nine -= 9 ** (pow_nine - 1)
            n_nine2 += 1
        else:
          pow_nine -= 1

    while n_six >= 6:
        if n_six >= 6 ** (pow_six - 1):
            n_six -= 6 ** (pow_six - 1)
            n_six2 += 1
        else:
          pow_six -= 1          
          
    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)
          
print(ans)
# Original solution
n = int(eval(input()))
ans = float("inf")
for n_nine in range(0, n//9 + 1):
    s = n - 9 * n_nine
    n_six = s // 6
    n_one = s - 6 * n_six
    n_nine2 = 0
    n_six2 = 0
    pow_nine = 5
    pow_six = 6
    
    while n_nine >= 9:
        if n_nine >= 9 ** (pow_nine - 1):
            n_nine -= 9 ** (pow_nine - 1)
            n_nine2 += 1
        else:
          pow_nine -= 1

    while n_six >= 6:
        if n_six >= 6 ** (pow_six - 1):
            n_six -= 6 ** (pow_six - 1)
            n_six2 += 1
        else:
          pow_six -= 1          
          
    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)
          
print(ans)

# Optimized solution
def counting(num, power, base):
    num2 = 0
    while num >= base:
        if num >= base ** (power - 1):
            num -= base ** (power - 1)
            num2 += 1
        else:
            power -= 1
    return num + num2

def main():
    n = int(eval(input()))
    ans = float("inf")
    for n_nine in range(0, n// 9 + 1):
        s = n - 9 * n_nine
        n_six = s // 6
        n_1 = s - 6 * n_six
        pow_nine = 5
        pow_six = 6
        n_9 = counting(n_nine, pow_nine, 9)
        n_6 = counting(n_six, pow_six, 6)
        ans = min(ans, n_1 + n_6 + n_9)

    print(ans)

main()
# Original solution
s=eval(input())
a=int(s)
list=[]
list.append(1)
list.append(6)
list.append(9)
list.append(36)
list.append(81)
list.append(216)
list.append(729)
list.append(1296)
list.append(6561)
list.append(7776)
list.append(59049)
list.append(46656)
pp=[50 for l in range(50)]
def money(x,y):
  for i in range(12):
    b=x//list[i]
    if b==0:
      continue
    if i%2==1:
      if 0<b<6:
        if x%list[i]==0:
          pp[y+b]=y+b
        else:
          money(x-list[i],y+1)
    elif i%2==0:
      if i!=0:
        if 0<b<9:
          if x%list[i]==0:
            pp[y+b]=y+b
          else:
            money(x-list[i],y+1)
      else:
        if 0<b<6:
          pp[y+b]=y+b
money(a,0)
print((min(pp)))

# Optimized solution
s=eval(input())
a=int(s)
list=[]
list.append(1)
list.append(6)
list.append(9)
list.append(36)
list.append(81)
list.append(216)
list.append(729)
list.append(1296)
list.append(6561)
list.append(7776)
list.append(59049)
list.append(46656)
pp=[]
def money(x,y):
  for i in range(12):
    b=x//list[i]
    if b==0:
      continue
    if i%2==1:
      if 0<b<6:
        if x%list[i]==0:
          pp.append(y+b)
        else:
          if y>20:
            break
          money(x-list[i],y+1)
    elif i%2==0:
      if i!=0:
        if 0<b<9:
          if x%list[i]==0:
            pp.append(y+b)
          else:
            if y>20:
              break
            money(x-list[i],y+1)
      else:
        if 0<b<6:
          pp.append(y+b)
money(a,0)
print((min(pp)))
# Original solution
N = int(eval(input()))
dp = [0]*100001
L = [1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
for i in range(100001):
    for j in range(len(L)):
        if 1 <= i+L[j] <= 100000:
            if dp[i+L[j]] == 0:
                dp[i+L[j]] = dp[i]+1
            else:
                dp[i+L[j]] = min(dp[i]+1,dp[i+L[j]]) 
print((dp[N]))

# Optimized solution
N = int(eval(input()))
dp = [float('inf')]*100001
dp[0],dp[1] = 0,1
L = [1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
for i in range(2,100001):
  for j in range(len(L)):
    if i-L[j] >= 0:
      dp[i] = min(dp[i-L[j]]+1,dp[i])
print((dp[N]))
# Original solution
import sys

input = sys.stdin.readline


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

    A = []
    i = 1
    while True:
        if 6**i <= 100000:
            A.append(6**i)
            i += 1
        else:
            break
    A.sort(reverse=True)
    B = []
    i = 1
    while True:
        if 9**i <= 100000:
            B.append(9**i)
            i += 1
        else:
            break
    B.sort(reverse=True)

    ans = float("inf")
    for i in range(N+1):
        n_6, n_9 = i, N-i
        c_6 = 0
        for a in A:
            while True:
                if a <= n_6:
                    n_6 -= a
                    c_6 += 1
                else:
                    break
        c_6 += n_6
        c_9 = 0
        for b in B:
            while True:
                if b <= n_9:
                    n_9 -= b
                    c_9 += 1
                else:
                    break
        c_9 += n_9
        c = c_6 + c_9
        if c < ans:
            ans = c
    print(ans)


if __name__ == "__main__":
    main()


# Optimized solution
import sys

input = sys.stdin.readline


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

    A = []
    i = 1
    while True:
        if 6**i <= 100000:
            A.append(6**i)
            i += 1
        else:
            break
    A.sort(reverse=True)
    B = []
    i = 1
    while True:
        if 9**i <= 100000:
            B.append(9**i)
            i += 1
        else:
            break
    B.sort(reverse=True)

    ans = float("inf")
    for i in range(N+1):
        n_6, n_9 = i, N-i
        c_6 = 0
        for a in A:
            q, _, = divmod(n_6, a)
            c_6 += q
            n_6 -= a * q
        c_6 += n_6
        c_9 = 0
        for b in B:
            q, _, = divmod(n_9, b)
            c_9 += q
            n_9 -= b * q
        c_9 += n_9
        c = c_6 + c_9
        if c < ans:
            ans = c
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
# dp
# N円を作るのに最も小さい操作回数

import sys
readline = sys.stdin.readline

N = int(readline())

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

for i in range(len(dp)):
  n = 6
  while i + n < len(dp):
    if dp[i + n] > dp[i] + 1:
      dp[i + n] = dp[i] + 1
    n *= 6
  n = 9
  while i + n < len(dp):
    if dp[i + n] > dp[i] + 1:
      dp[i + n] = dp[i] + 1
    n *= 9
print((dp[N]))


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