CodeEff/ECCO huggingface dataset:
# Original solution
def solve():
    N = int(eval(input()))

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

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

        k = 0
        while 9 ** k + i <= N:
            dp[9 ** k + i] = min(dp[9**k+i], dp[i] + 1)
            k += 1
        
        dp[i+1] = min(dp[i+1], dp[i] + 1)

    print((dp[-1]))
if __name__ == '__main__':
    solve()


# Optimized solution
def solve():
    N = int(eval(input()))
    dp = [float('inf')] * (N+1)
    dp[0] = 0

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

        # 6 ** n yen
        n = 1
        while i - pow(6,n) >= 0:
            dp[i] = min(dp[i], dp[i-pow(6,n)]+1)
            n += 1

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

if __name__ == '__main__':
    solve()
# Original solution
from bisect import bisect_right

n = int(eval(input()))

l = [1]
i = 1
while 6 ** i <= 100000:
    l.append(6 ** i)
    i += 1
i = 1
while 9 ** i <= 100000:
    l.append(9 ** i)
    i += 1
l.sort()

dp = [float('inf')] * (n+1)
dp[0] = 0
for i in range(1, n+1):
    for v in l:
        if v > i:
            break
        dp[i] = min(dp[i], dp[i-v] + 1)

print((dp[n]))

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

l = [1]
i = 1
while 6 ** i <= 100000:
    l.append(6 ** i)
    i += 1
i = 1
while 9 ** i <= 100000:
    l.append(9 ** i)
    i += 1
l.sort()

dp = [float('inf')] * (n+1)
dp[0] = 0
for i in range(1, n+1):
    for v in l:
        if v > i:
            break
        dp[i] = min(dp[i], dp[i-v] + 1)

print((dp[n]))
# Original solution
lis = [1]
cou = 1
n = int(eval(input()))
while 6 ** cou <= n:
  lis.append(6**cou)
  cou += 1
cou = 1
while 9 ** cou <= n:
  lis.append(9**cou)
  cou += 1
lis.sort(reverse = True)

now = {0}

for i in range(n):
  gone = set()
  for num in now:
    for k in range(len(lis)):
      gone.add(num + lis[k])
  if n in gone:
    print((i+1))
    exit()
  now = gone

# Optimized solution
n = int(eval(input()))
lis = [1, 6, 36, 216, 1296, 7776, 46656,9, 81, 729, 6561, 59049]
lis.sort(reverse=True)
dp = [10 ** 10 for i in range(n+1)]
dp[0] = 0
for i in range(n):
  for num in lis:
    try:
      dp[i+num] = min(dp[i+num],dp[i]+1)
    except:
      continue
print((dp[n]))
# Original solution
n = int(eval(input()))
possible = [1]
i=1
while i <= 100000:
  i = i*6
  possible+=[i]
i=1
while i <= 100000:
  i = i*9
  possible+=[i]
possible.sort(reverse =1)
ans = 1
table = set(possible)
while True:
  if n in table:
    print(ans)
    break
  ans+=1
  nexttable = set()
  for num in table:
    for p in possible:
      if num + p <100000:
        nexttable.add(num+p)
  table = table | nexttable

# Optimized solution
n = int(eval(input()))
possible = [1]
i=1
while i <= 100000:
  i = i*6
  possible+=[i]
i=1
while i <= 100000:
  i = i*9
  possible+=[i]
possible.sort(reverse =1)
ans = 0
table = set([n])
while True:
  if 0 in table:
    print(ans)
    break
  ans+=1
  nexttable = set()
  for num in table:
    for p in possible:
      if num - p >= 0:
        nexttable.add(num-p)
  table =nexttable
# Original solution
n = int(eval(input()))
dp = [10**9]*30000000
dp[0] = 0
pow6 = [6**i for i in range(10) if 6**i <= n]
pow9 = [9**i for i in range(10) if 9**i <= n]

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


# Optimized solution
from bisect import bisect_right
n = int(eval(input()))
MX = 100010
dp = [float('inf') for _ in range(MX)]
dp[0] = 0
pow_6, pow_9 = [6], [9]
while True:
    flg = False
    if pow_6[-1]*6 <= MX:
        pow_6.append(pow_6[-1]*6)
        flg = True
    if pow_9[-1]*9 <= MX:
        pow_9.append(pow_9[-1]*9)
        flg = True
    
    if flg == False:
        break

for i in range(1, MX):
    tmp = [1]
    if i >= 6:
        x = bisect_right(pow_6, i)
        tmp.append(pow_6[x-1])
    if i >= 9:
        y = bisect_right(pow_9, i)
        tmp.append(pow_9[y-1])
    for t in tmp:
        dp[i] = min(dp[i], dp[i-t]+1)
print((dp[n]))
# Original solution
n = int(eval(input()))
max_n = 10 ** 5

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

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

# Optimized solution
n = int(eval(input()))
dp = [int(i) for i in range(10 ** 5 + 1)]

for i in [6,9]:
    v = i
    while v < 10 ** 5:
        for j in range(len(dp)):
            if j+v <= 10 ** 5:
                dp[j+v] =min(dp[j]+1,dp[j+v])
        v *= i      
        
print((dp[n]))
# Original solution
l_6 = []
_ = 1
while 6**_ < 100000:
    l_6.append(6**_)
    _ += 1
l_6.sort(reverse = 1)

l_9= []
_ = 1
while 9**_ < 100000:
    l_9.append(9**_)
    _ += 1
l_9.sort(reverse = 1)

n = int(eval(input()))

w = 0
z = []
for i in range(n+1):
    x = i
    for j in range(len(l_6)):
        while l_6[j] <= x:
            x -= l_6[j]
            w += 1
    else:
        w += x
    
    y = n-i
    for k in range(len(l_9)):
        while l_9[k] <= y:
            y -= l_9[k]
            w += 1
    else:
        w += y

    z.append(w)
    w = 0

print((min(z)))


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

dp = [0] + [100000]*n

ptn = [1]
_ = 1
while 6**_ < 100000:
    ptn.append(6**_)
    _ += 1

_ = 1
while 9**_ < 100000:
    ptn.append(9**_)
    _ += 1

for i in range(1,n+1):
    for j in ptn:
        if i-j >= 0:
            dp[i] = min(dp[i], dp[i-j]+1)
print((dp[-1]))
# Original solution
n_max = 100000

bs = [(6 ** i, 6) for i in range (1, 10)]
bs.extend([(9 ** i, 9) for i in range (1, 10)])
bs = list(sorted([x for x in bs if x[0] <= n_max]))
bms = []
s = 5
for b, i in bs:
    s += b * (i - 1)
    bms.append((b, i, s))

#print(bs)
#print(bms, len(bms))

#m = 44852
m = int(eval(input()))

stack = [(m, 0, len(bms) - 1)]
#print("search start!")
result = 10000
while stack:
    m, n, bms_i = stack.pop()
    b, i, s = bms[bms_i]
    #print(m, n, bms_i, b, i, s)

    if n > result:
        #print("skip!", m, n, bms_i)
        continue
    if bms_i < 0:
        if m <= 6 and m + n < result:
            result = m + n
        #    print('get result!', result)
        continue

    for j in range(i):
        next_m = m - b * j
        if next_m >= 0 and n + j < result:
            stack.append((next_m, n + j, bms_i - 1))

print(result)


# Optimized solution
n_max = 100000

bs = [(6 ** i, 6) for i in range (1, 10)]
bs.extend([(9 ** i, 9) for i in range (1, 10)])
bs = list(sorted([x for x in bs if x[0] <= n_max]))
bms = []
s = 5
for b, i in bs:
    s += b * (i - 1)
    bms.append((b, i, s))

#print(bs)
#print(bms, len(bms))

#m = 44852
m = int(eval(input()))

stack = [(m, 0, len(bms) - 1)]
#print("search start!")
result = 10000
while stack:
    m, n, bms_i = stack.pop()
    b, i, s = bms[bms_i]
    #print(m, n, bms_i, b, i, s)

    if n > result:
        #print("skip!", m, n, bms_i)
        continue
    if m > s:
        continue
    if bms_i < 0:
        if m <= 6 and m + n < result:
            result = m + n
        #    print('get result!', result)
        continue

    for j in range(i):
        next_m = m - b * j
        if next_m >= 0 and n + j < result:
            stack.append((next_m, n + j, bms_i - 1))

print(result)
# Original solution
import bisect
n = int(eval(input()))
add_num = 1
draw_money = []
while add_num <= 10 ** 5:
    draw_money.append(add_num)
    add_num *= 6
    
add_num = 1
while add_num <= 10 ** 5:
    draw_money.append(add_num)
    add_num *= 9

#重複を削除
draw_money = list(set(draw_money))
ans = 0
draw_money.sort()
# dp[i] = i円引き出すのに必要な最小操作
dp = [10000000] * (n + 1)
dp[0] = 0
for i in range(1,n + 1):
    for j in draw_money:
        if  i - j >= 0:
            dp[i] = min(dp[i - 1] + 1, dp[i - j] + 1,dp[i])
        else:
            dp[i] = min(dp[i-1] + 1, dp[i])

print((dp[n]))





# Optimized solution
n = int(eval(input()))
inf = 10**9
dp = [inf for i in range(n+1)]
dp[0] = 0
for i in range(n):
    if dp[i] != inf:
        #1円引き出す場合
        dp[i+1] = min(dp[i] + 1, dp[i+1])
        num = 6
        while i + num <= n:
            dp[i + num] = min(dp[i] + 1, dp[i+num])
            num *= 6
        num = 9
        while i + num <= n:
            dp[i + num] = min(dp[i] + 1, dp[i+num])
            num *= 9

print((dp[n]))
# Original solution
INF=9999999

n=int(eval(input()))
dp=[INF for i in range(n+1)]
dp[-1]=0
for i in range(n,0,-1):
    e6=1
    while 6**e6<=i:
        dp[i-6**e6]=min(dp[i]+1,dp[i-6**e6])
        e6+=1
    e9=1
    while 9**e9<=i:
        dp[i-9**e9]=min(dp[i]+1,dp[i-9**e9])
        e9+=1
ans=INF
for i in range(n):
    if dp[i]==0:
        continue
    ans=min(ans,i+dp[i])
if ans==INF:
    ans=n
print(ans)


# Optimized solution
#補助関数
def convert(num,radix):
    res = 0
    while num > 0:
        res += num % radix
        num //= radix
    return res

#入力
INF = (1 << 31)
N   = int(eval(input()))
ans = INF

#計算
for i in range(N + 1):
    dec6 = i
    dec9 = N - i
    ans  = min(ans , convert(dec6,6) + convert(dec9,9))

#出力
print(ans)
# Original solution
N=int(eval(input()))


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


DP=[i for i in range(100001)]

def dpmin(i):
    Y=i
    for j in X:
      if i-j>=0:
        Y=min(Y,DP[i-j]+1)

    return Y

for i in range(100001):
    DP[i]=dpmin(i)

print((DP[N]))
    

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


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

ANS=1000000



i=4



for i_0 in range(max(N//X[0]-i,0),N//X[0]+1):
    N_0=N-X[0]*i_0
    for i_1 in range(max(N_0//X[1]-i,0),N_0//X[1]+1):
        N_1=N_0-X[1]*i_1
        for i_2 in range(max(N_1//X[2]-i,0),N_1//X[2]+1):
            N_2=N_1-X[2]*i_2
            for i_3 in range(max(N_2//X[3]-i,0),N_2//X[3]+1):
                N_3=N_2-X[3]*i_3
                for i_4 in range(max(N_3//X[4]-i,0),N_3//X[4]+1):
                    N_4=N_3-X[4]*i_4
                    for i_5 in range(max(N_4//X[5]-i,0),N_4//X[5]+1):
                        N_5=N_4-X[5]*i_5
                        for i_6 in range(max(N_5//X[6]-i,0),N_5//X[6]+1):
                            N_6=N_5-X[6]*i_6
                            for i_7 in range(max(N_6//X[7]-i,0),N_6//X[7]+1):
                                N_7=N_6-X[7]*i_7
                                for i_8 in range(max(N_7//X[8]-i,0),N_7//X[8]+1):
                                    N_8=N_7-X[8]*i_8
                                    for i_9 in range(max(N_8//X[9]-i,0),N_8//X[9]+1):
                                        N_9=N_8-X[9]*i_9
                                        i_10=N_9//6
                                        N_10=N_9-X[10]*i_10
                                        if ANS>i_0+i_1+i_2+i_3+i_4+i_5+i_6+i_7+i_8+i_9+i_10+N_10:
                                            ANS=i_0+i_1+i_2+i_3+i_4+i_5+i_6+i_7+i_8+i_9+i_10+N_10


                                            




print(ANS)
# Original solution
bank = [1]
N = int(eval(input()))
for i in range(20):
    if 100000 >= 6**i:
        bank.append(6**i)
    if 100000 >= 9**i:
        bank.append(9**i)
# print(bank)
dp = [[10**10]*(N+1) for _ in range(len(bank)+1)]

dp[0][0] = 0
# i番目の引き出し方法を使う
for i in range(len(bank)):
    for j in range(N+1):
        # 使わない場合
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])
        # 使う場合
        if j + bank[i] > N:  # Nを超えるならcontinue
            continue
        dp[i+1][j+bank[i]] = min(dp[i][j]+1, dp[i+1][j]+1)
print((dp[len(bank)][N]))


# Optimized solution
# from math import log
n = int(eval(input()))


# def make_list(n, x):
#     return [x ** i for i in range(1, int(log(n, x))+1)]
nums = [1]
# N = int(input())
for i in range(20):
    if 100000 >= 6**i:
        nums.append(6**i)
    if 100000 >= 9**i:
        nums.append(9**i)

# sixes = make_list(n, 6)
# nines = make_list(n, 9)
# nums = [1] + sixes + nines

dp = [float('inf')] * (n+1)
for num in nums:
    for i in range(1, n+1):  # 1~nまでのn円について調べる
        if i == num:
            dp[i] = 1
        elif i > num:
            dp[i] = min(dp[i], dp[i-num]+1)
print((dp[n]))
# Original solution
n=int(eval(input()))
l=[1]
for i in [6,9]:
    x=i
    while x<=n:
        l.append(x)
        x*=i
dp=[10**9]*(n+1)
dp[0]=0
for x in l:
    for i in range(n):
        if i+x<=n:dp[i+x]=min(dp[i+x],dp[i]+1)
print((dp[n]))



# Optimized solution
n=int(eval(input()))
l=[1]
for i in [6,9]:
    x=i
    while x<=n:
        l.append(x)
        x*=i
dp=[10**9]*(n+1)
dp[0]=0
for x in l:
    for i in range(n):
        if i+x<=n:dp[i+x]=min(dp[i+x],dp[i]+1)
        else:break
print((dp[n]))
# Original solution
N = int(eval(input()))

ans = N
for v9 in range(N + 1):
    cnt = 0
    t = v9
    w = 9 ** 5
    while t >= 9:
        if t >= w:
            cnt9, t = divmod(t, w)
            cnt += cnt9
        w //= 9
    t = N - (v9 - t)
    w = 6 ** 7
    while t >= 6:
        if t >= w:
            cnt6, t = divmod(t, w)
            cnt += cnt6
        w //= 6
    ans = min(ans, cnt + t)

print(ans)


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

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

print(ans)
# Original solution
def s(n,c):
  if n<6:return c+n
  a=b=1
  while a*6<=n:a*=6
  while b*9<=n:b*=9
  return min(s(n-a,c+1),s(n-b,c+1))
print((s(int(eval(input())),0)))

# Optimized solution
def s(n,p):
  while n>0:yield n%p;n//=p
c=lambda*a:sum(s(*a));n=int(eval(input()));print((min(c(i,6)+c(n-i,9)for i in range(n+1))))
# Original solution
n = int(eval(input()))
m = [1, 6, 9, 36, 81, 216, 729, 1296, 6561, 7776, 46656, 59049]
dp = [10**6 for i in range(100001)]
for mi in m:
    dp[mi] = 1
for i in range(1,100001):
    for mi in m:
        if i+mi <= 100000:
            dp[i+mi] = min(dp[i+mi],dp[i]+1)
        else:
            break
print((dp[n]))


# Optimized solution
N = int(eval(input()))
li = [1]

six = 1
while six <= N:
    six = six*6
    li.append(six)

nine = 1
while nine <= N:
    nine = nine*9
    li.append(nine)

INF = 10**10

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

for i in range(N):
    for num in li:
        if i+num <= N:
            dp[i+num] = min(dp[i+num], dp[i] + 1)
print((dp[N]))
# Original solution
N = int(eval(input()))

def six(n):
  count = 0
  while n > 5:
    x = n
    a = 0
    while x >= 1:
      x = x / 6
      a += 1
    a -= 1
    n -= 6 ** a
    count += 1
  count += n
  return count

def nine(n):
  count = 0
  while n > 8:
    x = n
    a = 0
    while x >= 1:
      x = x / 9
      a += 1
    a -= 1
    n -= 9 ** a
    count += 1
    #print(n)
  count += n
  return count

#print(six(41), nine(41))
ans = 10 ** 7
for i in range(N + 1):
  x = six(i) + nine(N - i)
  ans = min(ans, x)

print(ans)
  

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

six = []
nine = []

now = 6
while now < 100001:
  six.append(now)
  now *= 6
  
now = 9
while now < 100001:
  nine.append(now)
  now *= 9  

#print(six, nine)  
#[6, 36, 216, 1296, 7776, 46656] [9, 81, 729, 6561, 59049]

def coin(n):
  if n < 6:
    return n
  else:
    index_six = bisect.bisect_right(six, n)
    index_nine = bisect.bisect_right(nine, n)
    #print(n, index_six, index_nine, count)
    #print(n, n - six[index_six - 1], n - nine[index_nine - 1])
    if n - six[index_six - 1] >= 0:
      alpha = coin(n - six[index_six - 1]) + 1
    else:
      alpha = 10 ** 7
    if n - nine[index_nine - 1] >= 0:
      beta = coin(n - nine[index_nine - 1]) + 1
    else:
      beta = 10 ** 7
    return min(alpha, beta)
  
print((coin(N)))
# Original solution
N = int(eval(input()))
r = N

for i in range(N+1):
  c=0;t=i
  while t>0:c+=t%6;t//=6
  t=N-i
  while t>0:c+=t%9;t//=9
  r=min(r,c)
  
print(r)

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

for n in range(N+1):
  c = 0
  t = n
  while t>0:
    c+=t%6
    t//=6
  t = N-n
  while t>0:
    c+=t%9
    t//=9
  a = min(a,c)
  
print(a)
# Original solution
N = int(eval(input()))

s = set()
a = 1
while a <= 100000:
    s.add(a)
    a *= 6
a = 1
while a <= 100000:
    s.add(a)
    a *= 9
s = sorted(s)

INF = float('inf')
dp = [INF] * (N+1)
dp[0] = 0
for i in range(N):
    for a in s:
        if i+a > N: break
        dp[i+a] = min(dp[i+a], dp[i] + 1)
print((dp[N]))

# Optimized solution
import sys
sys.setrecursionlimit(10**8)

N = int(eval(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)))
# Original solution
def main():
    N = int(eval(input()))

    dp = [float('inf')] * (N + 10)
    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]))

main()

# Optimized solution
def main():
    N = int(eval(input()))
    cand = [1]
    dp = [float('inf')] * (N + 5)
    tmp = 6
    cnt = 1
    while tmp**cnt <= N:
        cand.append(tmp**cnt)
        cnt += 1
    tmp = 9
    cnt = 1
    while tmp**cnt <= N:
        cand.append(tmp**cnt)
        cnt += 1
    cand.sort()
    dp[0] = 0
    for i in range(N+1):
        for c in cand:
            if i + c <= N:
                dp[i+c] = min(dp[i+c], dp[i] + 1)
    print((dp[N]))

if __name__ == "__main__":
    main()
# Original solution
N = int(eval(input()))
# DPだと思ったけど、2次元じゃなくて1次元だった
# けんちょんさん曰く、1次元でできないときに2次元で考える
sixes = [6 ** i for i in range(1, 7)]
nines = [9 ** i for i in range(1, 6)]
coin = [1] + sixes + nines
INF = 10**6
dp = [INF] * 100010
dp[0] = 0  # dp[i]:i円引き出すのに必要な回数
for money in range(1, N + 1):
    dp_candidate = []
    for coin_ in coin:
        if money < coin_:
            continue
        else:
            dp_candidate.append(dp[money-coin_] + 1)
    dp[money] = min(dp_candidate)
print((dp[N]))


# Optimized solution
N = int(eval(input()))
sixes = [6 ** i for i in range(1, 7)]
nines = [9 ** i for i in range(1, 6)]
coin = [1] + sixes + nines
INF = 10**6
dp = [INF] * 100010
dp[0] = 0  # dp[i]:i円引き出すのに必要な回数
for money in range(1, N + 1):
    dp[money] = min([dp[money-i] + 1 for i in coin if i <= money])
print((dp[N]))
# Original solution
N = int(eval(input()))
A = [1, 6, 6**2, 6**3, 6**4, 6**5, 6**6, 9, 9**2, 9**3, 9**4, 9**5]
dp = [1000000] * 300000
dp[0] = 0

for i in range(N):
    for a in A:
        d = dp[i] + 1
        dp[i + a] = min(dp[i + a], d)

print((dp[N]))

# Optimized solution
N = int(eval(input()))
INF = 10 ** 9
dp = [INF] * (N + 1)
dp[0] = 0
M = set()
M.add(1)
i = 1
while True:
    if 6 ** i > N:
        break
    M.add(6 ** i)
    i += 1
i = 1
while True:
    if 9 ** i > N:
        break
    M.add(9 ** i)
    i += 1
for i in range(N):
    for m in M:
        if i + m > N:
            continue
        dp[i + m] = min(dp[i + m], dp[i] + 1)
print((dp[N]))
# Original solution
n = int(eval(input()))
INF = 10**5
answer = INF

for i in range(n+1):
    cost = i
    ans = 0
    for p in range(8):
        ans += (cost // 6**p) % 6

    cost = n-i
    for q in range(7):
        ans += (cost // 9**q) % 9
    if answer > ans:
        answer = ans
print(answer)


# Optimized solution
n = int(eval(input()))
INF = 10**5
answer = INF
for i in range(n+1):
    money = i
    cnt = 0
    while money > 0:
        cnt += money % 6
        money //= 6

    money = n-i
    while money > 0:
        cnt += money % 9
        money //= 9
    if answer > cnt:
        answer = cnt
print(answer)
# Original solution
N = int(eval(input()))


def main():
    ans = N

    for i in range(N + 1):
        num = 0
        cur = i

        while 0 < cur:
            num += cur % 6
            cur //= 6

        cur = N - i

        while 0 < cur:
            num += cur % 9
            cur //= 9

        if num < ans:
            ans = num

    print(ans)
    return


main()


# Optimized solution
def f(num, base):
    # calc max Pth Power
    ret = 1
    while ret <= num:
        ret *= base
    return ret // base


def main():
    N = int(eval(input()))
    st = {N}
    ans = 0
    while (all(st)):
        st = {x - f(x, 6) for x in st} | {x - f(x, 9) for x in st}
        ans += 1
    print(ans)
    return


main()
# Original solution
def f(num, base):
    # calc max Pth Power
    ret = 1
    while ret <= num:
        ret *= base
    return ret // base


def main():
    N = int(eval(input()))
    st = {N}
    ans = 0
    while (all(st)):
        st = {x - f(x, 6) for x in st} | {x - f(x, 9) for x in st}
        ans += 1
    print(ans)
    return


main()


# Optimized solution
def f(num, base):
    # calc max Pth Power
    ret = 1
    while ret <= num:
        ret *= base
    return ret // base


def main():
    N = {int(eval(input()))}
    ans = 0
    while (all(N)):
        N = {x - f(x, 6) for x in N} | {x - f(x, 9) for x in N}
        ans += 1
    print(ans)
    return


main()
# Original solution
from heapq import *

N = int(eval(input()))

result = 100000

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

h = []

xs = {}

heappush(h, (0, 0))

while 1:
    r, m = heappop(h)
    if m == N:
        result = r
        break
    for n in ns:
        s = r + 1
        x = m + n
        if x <= N and not x in xs:
            xs[x] = s
            heappush(h, (s, x))

print(result)



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

result = 100000000000

def calc(x, base):
    r = 0
    while x:
        x, m = divmod(x, base)
        r += m
    return r

for i in range(N + 1):
    result = min(result, calc(i, 6) + calc(N - i, 9))

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

result = 100000000000

def calc(x, base):
    r = 0
    while x:
        x, m = divmod(x, base)
        r += m
    return r

for i in range(N + 1):
    result = min(result, calc(i, 6) + calc(N - i, 9))

print(result)


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

def calc(x, base):
    r = 0
    while x:
        x, m = divmod(x, base)
        r += m
    return r

result = min(calc(i, 6) + calc(N - i, 9) for i in range(N + 1))

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

dp = [i for i in range(101010)]

#dp[i] = i円払うのに必要な最小値
#6ˆn円,0<=i<=Nにおいて、dp[i+6ˆn] = min(dp[i+6ˆn] ,dp[i]+1)  

#6の倍数
t = 10
while True:
    for i in range(101010):
        if 6**t+(i+1) < 101010:
            dp[6**t+(i)] = min(dp[6**t+(i)], dp[i] + 1) 
    t -= 1
    if t == 0:
        break

#9の倍数
t = 10
while True:
    for i in range(101010):
        if 9**t+(i+1) < 101010:
            dp[9**t+(i)] = min(dp[9**t+(i)], dp[i] + 1)
    t -= 1
    if t == 0:
        break

i = 0
while i+1 < 101010:
    dp[i+1] = min(dp[i+1],dp[i]+1)
    i += 1

#print(dp)
print((dp[n]))

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

dp = [i for i in range(101010)]

#dp[i] = i円払うのに必要な最小値
#6ˆn円,0<=i<=Nにおいて、dp[i+6ˆn] = min(dp[i+6ˆn] ,dp[i]+1)  


t = 7
while True:
    for i in range(101010):
        if 6**t+(i+1) < 101010:
            dp[6**t+(i)] = min(dp[6**t+(i)], dp[i] + 1) 
    t -= 1
    if t == 0:
        break


t = 7
while True:
    for i in range(101010):
        if 9**t+(i+1) < 101010:
            dp[9**t+(i)] = min(dp[9**t+(i)], dp[i] + 1)
    t -= 1
    if t == 0:
        break

i = 0
while i+1 < 101010:
    dp[i+1] = min(dp[i+1],dp[i]+1)
    i += 1

#print(dp)
print((dp[n]))