CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))

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


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

print((dp[n]))

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

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


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

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

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

for i in range(2,N+1):
  L=[M[i-1]+1]
  j=0
  while i-6**j>=0:
    L.append(M[i-6**j]+1)
    j+=1
  j=0
  while i-9**j>=0:
    L.append(M[i-9**j]+1)
    j+=1
  M[i]=min(L)
#print(M)

print((M[N]))

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

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

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

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

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

A = []
for i in range(1, N+1):
    if 6**i > N:
        break
    else:
        A.append(6**i)
    if 9**i <= N:
        A.append(9**i)
A.sort(reverse=True)

money = [i for i in range(N+1)]
for i in A:
    for j in range(i,N+1):
        money[j] = min(money[j], money[j-i]+1)
print((money[N]))


# Optimized solution
n = int(eval(input()))
ans = float('INF')
hand = []
tmp = 1
while tmp * 6 <= n:
    tmp *= 6
    hand.append(tmp)
tmp = 1
while tmp * 9 <= n:
    tmp *= 9
    hand.append(tmp)
 
hand.sort()
hand = hand[::-1]
dp = [i for i in range(n+1)]
for v in hand:
    for i in range(n+1-v):
        if dp[i] + 1 < dp[i+v]:
            dp[i+v] = dp[i] + 1
print((dp[n]))
# Original solution
def main():
    N = int(eval(input()))

    withdraws = []
    for i in range(N):
        if N < 9**i:
            break
        withdraws.append(9**i)
    for i in range(1, N):
        if N < 6**i:
            break
        withdraws.append(6**i)
    withdraws.sort(reverse=True)
    W = len(withdraws)

    dp = [[10**9 for i in range(N + 1)] for j in range(W + 1)]
    dp[0][0] = 0
    for i in range(1, W+1):
        for n in range(N + 1):
            if n - withdraws[i-1] >= 0:
                dp[i][n] = min(dp[i-1][n], dp[i][n],
                               dp[i][n - withdraws[i-1]] + 1)
            else:
                dp[i][n] = min(dp[i-1][n], dp[i][n])
    print((dp[W][N]))


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    N = int(eval(input()))

    withdraws = []
    for i in range(N):
        if N < 9**i:
            break
        withdraws.append(9**i)
    for i in range(1, N):
        if N < 6**i:
            break
        withdraws.append(6**i)
    withdraws.sort(reverse=True)
    W = len(withdraws)

    dp = [[10**9 for i in range(N + 1)] for j in range(W + 1)]
    dp[0][0] = 0
    for i in range(1, W+1):
        for n in range(N + 1):
            if n - withdraws[i-1] >= 0:
                dp[i][n] = min(dp[i-1][n], dp[i][n - withdraws[i-1]] + 1)
            else:
                dp[i][n] = dp[i-1][n]
    print((dp[W][N]))


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

from collections import deque
# q = deque(); q.append(x); q.appendleft(x); q.pop() -> x; q.popleft() -> x
from collections import defaultdict
# d = defaultdict(lambda: []); d[key] -> []
import heapq
# hq = []; heapq.heappush(hq, x); heapq.heappop(hq) -> x; heapq.heappushpop(hq, x) -> x
import bisect
# i = bisect.bisect_left([1,2,3,4], 1.5); i = bisect.bisect_right([4,3,2,1], 1.5)

#import numpy as np
#from scipy.misc import comb

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

# BFS方法
def main():
    N = i2n()
    coins = [1]
    d = 6
    while d <= N:
        coins.append(d)
        d *= 6
    d = 9
    while d <= N:
        coins.append(d)
        d *= 9
    q = deque()
    q.append((0, 0))
    s = set((0,))
    while len(q) > 0:
        i, n = q.popleft()
        for j in coins:
            k = i + j
            if k == N:
                print((n+1))
                return
            if k > N:
                continue
            if k in s:
                continue
            q.append((k, n+1))
            s.add(k)
    print((-1))

main()


# Optimized solution
import sys

from collections import deque
# q = deque(); q.append(x); q.appendleft(x); q.pop() -> x; q.popleft() -> x
from collections import defaultdict
# d = defaultdict(lambda: []); d[key] -> []
import heapq
# hq = []; heapq.heappush(hq, x); heapq.heappop(hq) -> x; heapq.heappushpop(hq, x) -> x
import bisect
# i = bisect.bisect_left([1,2,3,4], 1.5); i = bisect.bisect_right([4,3,2,1], 1.5)

#import numpy as np
#from scipy.misc import comb

s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))

# BFS方法
def main():
    N = i2n()
    coins = [1]
    d = 6
    while d <= N:
        coins.append(d)
        d *= 6
    d = 9
    while d <= N:
        coins.append(d)
        d *= 9
    q = deque()
    q.append((0, 0))
    s = set((0,))
    while q:
        i, n = q.popleft()
        for j in coins:
            k = i + j
            if k == N:
                print((n+1))
                return
            if k > N:
                continue
            if k in s:
                continue
            q.append((k, n+1))
            s.add(k)
    print((-1))

main()
# Original solution
N = int(eval(input()))
dp = [float('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)
    for j in range(1,N+1):
        if i + 6**j > N:
            break
        dp[i+6**j] = min(dp[i+6**j],dp[i]+1)
    for k in range(1,N+1):
        if i + 9**k > N:
            break
        dp[i+9**k] = min(dp[i+9**k],dp[i]+1)
print((dp[N]))

# Optimized solution
N = int(eval(input()))
dp = [float('inf')]*(N+1)
dp[0] = 0
for i in range(N+1):
    if i >= 1:
        dp[i] = min(dp[i-1]+1,dp[i])
    for j in range(1,N+1):
        if i < 6**j:
            break
        dp[i] = min(dp[i-6**j]+1,dp[i])
    for k in range(1,N+1):
        if i < 9**k:
            break
        dp[i] = min(dp[i-9**k]+1,dp[i])
print((dp[N]))
# Original solution
from itertools import count

n = int(eval(input()))

dp = [0]*(n+1)

def it(n):
  yield 1
  m = 6
  while m <= n:
    yield m
    m *= 6
  m = 9
  while m <= n:
    yield m
    m *= 9

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

print((dp[n]))



# Optimized solution
def it(n,p):
  while n > 0:
    yield n%p
    n //= p
cost = lambda *a: sum(it(*a))

n = int(eval(input()))
print((min(cost(x,6)+cost(n-x,9) for x in range(n+1))))
# Original solution
def fx(n):
  if n == 0:
    return 0
  if n == 1:
    return 1
  arr = []
  cnt = 0
  
  if n < 6:
    b = fx(n-1)+1
    arr.append(b)
  while n-6**cnt >= 0:
    cnt += 1
  if cnt > 1:
    
    x = n-(6**(cnt-1))
    b = fx(x)+1
    arr.append(b)

  cnt = 0
  while n-9**cnt >= 0:
    cnt += 1
  if cnt > 1:
    x = n-(9**(cnt-1))
    b = fx(x)+1
    arr.append(b)
  
  return min(arr)
  
N = int(eval(input()))
print((fx(N)))



# Optimized solution
from functools import lru_cache

@lru_cache(maxsize=1000000)
def fx(n):
  if n == 0:
    return 0
  if n == 1:
    return 1
  arr = []
  cnt = 0
  
  if n < 6:
    b = fx(n-1)+1
    arr.append(b)
  while n-6**cnt >= 0:
    cnt += 1
  if cnt > 1:
    
    x = n-(6**(cnt-1))
    b = fx(x)+1
    arr.append(b)

  cnt = 0
  while n-9**cnt >= 0:
    cnt += 1
  if cnt > 1:
    x = n-(9**(cnt-1))
    b = fx(x)+1
    arr.append(b)
  
  return min(arr)
  
N = int(eval(input()))
print((fx(N)))
# Original solution
N = int(eval(input()))
t = list(range(N+1))
for i in range(2, N+1):
    x = t[i-1] + 1
    p6, p9 = 6, 9
    while i-p6 >= 0:
        x = min(x, t[i-p6] + 1)
        p6 *= 6
    while i-p9 >= 0:
        x = min(x, t[i-p9] + 1)
        p9 *= 9
    t[i] = x
print((t[N]))


# Optimized solution
N=int(eval(input()));t=[0]+[N]*N
for i in range(1,N+1):
 x,a,b=t[i-1],6,9
 while a<=i:x=min(x,t[i-a],t[i-b]if b<=i else N);a*=6;b*=9
 t[i]=x+1
print((t[N]))
# Original solution
# 貰うDP
n = int(eval(input()))
a = [1]
i = 6
while i <= n:
  a.append(i)
  i *= 6
i = 9
while i <= n:
  a.append(i)
  i *= 9
t = [float("inf")] * (n + 1)
t[0] = 0
for i in range(1, n + 1):
  k = float("inf")
  for j in a:
    if i - j >= 0 and k > t[i - j] + 1:
      k = t[i - j] + 1
  t[i] = k
print((t[n]))


# Optimized solution
# 貰うDP
n = int(eval(input()))
a = [1]
i = 6
while i <= n:
  a.append(i)
  i *= 6
i = 9
while i <= n:
  a.append(i)
  i *= 9
t = [0] * (n + 1)
for i in range(1, n + 1):
  k = float("inf")
  for j in a:
    if i - j >= 0 and k > t[i - j] + 1:
      k = t[i - j] + 1
  t[i] = k
print((t[n]))
# Original solution
# 貰うDP
n = int(eval(input()))
a = [1]
i = 6
while i <= n:
  a.append(i)
  i *= 6
i = 9
while i <= n:
  a.append(i)
  i *= 9
t = [0] * (n + 1)
for i in range(1, n + 1):
  k = float("inf")
  for j in a:
    if i - j >= 0 and k > t[i - j] + 1:
      k = t[i - j] + 1
  t[i] = k
print((t[n]))


# Optimized solution
# 貰うDP
INF = float('inf')
n = int(eval(input()))
a = [1]
i = 6
while i <= n:
  a.append(i)
  i *= 6
i = 9
while i <= n:
  a.append(i)
  i *= 9
t = [0] * (n + 1)
for i in range(1, n + 1):
  k = INF
  for j in a:
    if i - j >= 0 and k > t[i - j] + 1:
      k = t[i - j] + 1
  t[i] = k
print((t[n]))
# Original solution
# DP(配るDP)
INF = float('inf')

N = int(eval(input()))

c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9

dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    for j in c:
        if i + j <= N:
            dp[i + j] = min(dp[i + j], dp[i] + 1)
        if i + j == N:
            break
print((dp[N]))


# Optimized solution
# DP(配るDP)
INF = float('inf')

N = int(eval(input()))

c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9

dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    for j in c:
        t = i + j
        if t <= N and dp[t] > dp[i] + 1:
            dp[t] = dp[i] + 1
        if t == N:
            break
print((dp[N]))
# Original solution
# DP(配るDP)
INF = float('inf')

N = int(eval(input()))

c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9

dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    for j in c:
        t = i + j
        if t <= N and dp[t] > dp[i] + 1:
            dp[t] = dp[i] + 1
        if t == N:
            break
print((dp[N]))


# Optimized solution
# DP(配るDP)
INF = float('inf')

N = int(eval(input()))

c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9

dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    t0 = dp[i]
    for j in c:
        t1 = i + j
        if t1 <= N and dp[t1] > t0 + 1:
            dp[t1] = t0 + 1
        if t1 == N:
            break
print((dp[N]))
# Original solution
# DP(配るDP)
INF = float('inf')

N = int(eval(input()))

c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9

dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    t0 = dp[i] + 1
    for j in c:
        t1 = i + j
        if t1 <= N and dp[t1] > t0:
            dp[t1] = t0
        if t1 == N:
            break
print((dp[N]))


# Optimized solution
# DP(配るDP)
INF = float('inf')

N = int(eval(input()))

c = [1]
t = 6
while t <= N:
    c.append(t)
    t *= 6
t = 9
while t <= N:
    c.append(t)
    t *= 9

dp = [INF] * (N + 1)
dp[0] = 0
for i in range(N):
    t0 = dp[i] + 1
    for j in c:
        t1 = i + j
        if t1 <= N and dp[t1] > t0:
            dp[t1] = t0
print((dp[N]))
# Original solution
n=int(eval(input()))

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

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

print((dp[n]))

# Optimized solution
n=int(eval(input()))
ans=10**100
for t in range(n+1):
    p=t
    q=n-p
    c=0
    while p>0:
        c+=p%6
        p=p//6
    while q>0:
        c+=q%9
        q=q//9
    ans=min(ans,c)
print(ans)
# Original solution
sixexp=[6**i for i in range(1,7)]
nineexp=[9**i for i in range(1,6)]
lis=[1]+sixexp+nineexp

n=int(eval(input()))

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

for i in range(n):
    for j in lis:
        if i+j<=n:
            dp[i+j]=min(dp[i+j],dp[i]+1)

print((dp[n]))

# Optimized solution
sixexp=[6**i for i in range(1,7)]
nineexp=[9**i for i in range(1,6)]
lis=[1]+sixexp+nineexp

n=int(eval(input()))

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

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

print((dp[n]))
# Original solution
import bisect as bi

li6 = [6 ** i for i in range(7)]
li9 = [9 ** i for i in range(6)]

def s69(n):
  if n < 6:
    return n
  else:
    s6 = li6[bi.bisect(li6, n)-1]
    s9 = li9[bi.bisect(li9, n)-1]
    return min(n // s6 + s69(n % s6), 1 + s69(n-s9), n // s9 + s69(n % s9))
  
print((s69(int(eval(input())))))

# Optimized solution
import bisect as bi

li6 = [6 ** i for i in range(7)]
li9 = [9 ** i for i in range(6)]

def s69(n):
  if n < 6:
    return n
  else:
    s6 = li6[bi.bisect(li6, n)-1]
    s9 = li9[bi.bisect(li9, n)-1]
    return min(n // s6 + s69(n % s6), 1 + s69(n-s9))
  
print((s69(int(eval(input())))))
# Original solution
n = int(eval(input()))
max_n = 100000
dp = [10000 for i in range(max_n+1)]
dp[0] = 0

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

print((dp[n]))

# Optimized solution
from collections import deque
n = int(eval(input()))
max_n = 100000
dp = [10000 for i in range(max_n+1)]
dp[0] = 0
que = deque()
que.append(0)

while len(que) > 0:
  v = que.popleft()
  k = 1
  while v + k <= n:
    if dp[v+k] == 10000:
      dp[v+k]=dp[v]+1
      que.append(v+k)
    k *= 6
  k = 1
  while v + k <= n:
    if dp[v+k] == 10000:
      dp[v+k]=dp[v]+1
      que.append(v+k)
    k *= 9
print((dp[n]))
# Original solution
n=int(eval(input()))

ls=set([1])
i=1
while 6**i<=100000:
  ls.add(6**i)
  i+=1
i=1
while 9**i<=100000:
  ls.add(9**i)
  i+=1

dp = [float("inf") for i in range(n+2)]
dp[0] = 0
ls=list(ls)
#円の総合計
for i in range(n+2):
  #額面のコインの種類
  for j in range(len(ls)):
    if i+ls[j]<=n:
      dp[i+ls[j]] = min(dp[i]+1, dp[i+ls[j]-1]+1,dp[i+ls[j]])
print((dp[n]))

# Optimized solution
n=int(eval(input()))
ls=[1]

for i in range(1,7):
  ls.append(6**i)
  ls.append(9**i)
dp = [float('inf') for i in range(n+2)]
dp[0] = 0

#円の総合計
for i in range(1,n+1):
  for j in ls:
    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]))
# Original solution
N = int(eval(input()))
dp = [N] * (N+1)
dp[0] = 0

for i in range(1, N+1):
    pow6 = []
    pow9 = []
    for j in range(i):
        if 6**j > i:
            break
        pow6.append(6**j)
    for k in range(i):
        if 9**k > i:
            break
        pow9.append(9**k)
    L = set(pow6 + pow9)
    
    for l in L:
        if i-l < 0:
            break
        dp[i] = min(dp[i], dp[i-l]+1)

print((dp[N]))

# Optimized solution
N = int(eval(input()))
INF = 10**9
dp = [INF]*(N+1)
dp[0] = 0
num6 = [6**i for i in range(1, 10)]
num9 = [9**i for i in range(1, 7)]
S = sorted([1] + num6 + num9)

for i in range(1, N+1):
    tmp = INF
    for j in S:
        if j > i:
            break
        else:
            dp[i] = min(tmp, dp[i-j]+1)
            tmp = dp[i]

print((dp[N]))
# Original solution
N = int(eval(input()))
INF = 10**9
dp = [INF]*(N+1)
dp[0] = 0
num6 = [6**i for i in range(1, 10)]
num9 = [9**i for i in range(1, 7)]
S = sorted([1] + num6 + num9)

for i in range(1, N+1):
    tmp = INF
    for j in S:
        if j > i:
            break
        else:
            dp[i] = min(tmp, dp[i-j]+1)
            tmp = dp[i]

print((dp[N]))

# Optimized solution
N = int(eval(input()))
INF = 10**9
dp = [INF]*(N+1)
dp[0] = 0
num6 = [6**i for i in range(1, 10)]
num9 = [9**i for i in range(1, 7)]
S = sorted([1] + num6 + num9)

for i in range(1, N+1):
    for j in S:
        if j > i:
            break
        else:
            dp[i] = min(dp[i], dp[i-j]+1)

print((dp[N]))
# Original solution
N = int(eval(input()))
x = N
for i in range(N + 1):
	count = 0
	t = i
	while t > 0:
		count += int(t % 9)
		t = int(t / 9)
	t = N - i
	while t > 0 :
		count += int(t % 6)
		t = int(t / 6)
	if x > count:
		x = count
print(x)

# Optimized solution
N = int(eval(input()))
ans = N
for i in range(N + 1):
	count = 0
	t = i
	while t > 0:
		count += int(t % 9)
		t = int(t / 9)
	t = N - i
	while t > 0:
		count += int(t % 6)
		t = int(t / 6)
	if count < ans:
		ans = count
print(ans)
# Original solution
N = int(eval(input()))

def f(n):
    if n<6:
        return n
    i,j = 0,0
    a,b = n,n
    while a >= 6:
        i += 1
        a //= 6
    while b >= 9:
        j += 1
        b //= 9
    return min(1+f(n-6**i),1+f(n-9**j))

print((f(N)))

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

memo = {i:i for i in range(6)}
def f(n):
    tmp = memo.get(n,None)
    if tmp != None:
        return tmp
    i,j = 0,0
    a,b = n,n
    while a >= 6:
        i += 1
        a //= 6
    while b >= 9:
        j += 1
        b //= 9
    ret = min(1+f(n-6**i),1+f(n-9**j))
    memo[n] = ret
    return ret

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

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

for i in range(N, -1, -1) :
    if i - 1 >= 0 :
        dp[i - 1] = min(dp[i - 1], dp[i] + 1)
    dis = 6
    while i - dis >= 0 :
        dp[i - dis] = min(dp[i - dis], dp[i] + 1)
        dis *= 6
    dis = 9
    while i - dis >= 0 :
        dp[i - dis] = min(dp[i - dis], dp[i] + 1)
        dis *= 9

print((dp[0]))

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

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

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

ans = 10**18
for i, c in enumerate(dp):
    ans = min(ans, N - i + c)
print(ans)
# Original solution
N = int(eval(input()))

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

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

ans = 10**18
for i, c in enumerate(dp):
    ans = min(ans, N - i + c)
print(ans)


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

def digit(x, base):
    if x == 0:
        return 0
    return digit(x // base, base) + x % base

def calc(six, nine):
    return digit(six, 6) + digit(nine, 9)

ans = 10**18
for six in range(N + 1):
    ans = min(ans, calc(six, N - six))
print(ans)
# Original solution
l=[100000 for i in range(100001)]
l[0]=0
dif=[1,6,9,36,81,216,729,1296,6561,7776,46656,59049]
for i in range(1,100001):
	for j in dif:
		if j>i:
			break
		l[i]=min(l[i],l[i-j]+1)
n=int(eval(input()))
print((l[n]))

# Optimized solution
dp=[100000 for i in range(100001)]
dp[0]=0
l=[1]
i=6
while i<=100000:
    l.append(i)
    i*=6
i=9
while i<=100000:
    l.append(i)
    i*=9
for i in range(1,100001):
    for j in l:
        if j<=i and dp[i-j]+1<dp[i]:
            dp[i]=dp[i-j]+1
print((dp[int(eval(input()))]))
# Original solution
def nsin(X,n):
    if(int(X/n)):
        return nsin(int(X/n),n)+str(X%n)
    return str(X)

N = int(eval(input()))
score = N
for i in range(N+1):
    score = min(score,sum(map(int,nsin(i,6)+nsin(N-i,9))))
print(score)

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

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

print(score)
# Original solution
N = int(eval(input()))
dp = [10 ** 6] * (N + 1)
dp[0] = 0

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

print((dp[N]))

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

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

print((dp[N]))
# Original solution
six=[6**i for i in range(1,7)][::-1]
nine=[9**i for i in range(1,6)][::-1]
N=int(eval(input()))

ans=[]
def find(n,a):
    if n<6: ans.append(n+a)
    for s in six:
        if s<=n:
            find(n-s,a+1)
            break
    for s in nine:
        if s<=n:
            find(n-s,a+1)
            break
find(N,0)
print((min(ans)))

# Optimized solution
six=[6**i for i in range(1,7)][::-1]
nine=[9**i for i in range(1,6)][::-1]
N=int(eval(input()))

ans=[]
def find(n,a):
    if n<6:
        ans.append(n+a)
        return
    for s in six:
        if s<=n:
            find(n-s,a+1)
            break
    for s in nine:
        if s<=n:
            find(n-s,a+1)
            break
find(N,0)
print((min(ans)))
# Original solution
N=int(eval(input()))
weights=[1]
pow6=6
while N>=pow6:
    weights.append(pow6)
    pow6*=6
pow9=9
while N>=pow9:
    weights.append(pow9)
    pow9*=9
dp=[1<<28]*(N+1)
dp[0]=0
for i in range(len(weights)):
    for j in range(weights[i],N+1):
        dp[j]=min(dp[j],dp[j-weights[i]]+1)
print((dp[N]))

# Optimized solution
N=int(eval(input()))
res=N
for i in range(N+1):
    cc=0
    tmp=i
    while tmp>0:
        cc+=tmp%6
        tmp=tmp//6
    tmp=N-i
    while tmp>0:
        cc+=tmp%9
        tmp=tmp//9
    res=min(res,cc)
print(res)
# Original solution
import sys
imput=sys.stdin.readline
n=int(eval(input()))
dp=[float('inf')]*(n+1)
dp[1]=1
i=0
while 9**i<=n:
    dp[9**i]=1
    i+=1
j=0
while 6**j<=n:
    dp[6**j]=1
    j+=1
#print(dp[1],dp[9],dp[36],dp[127])
for i in range(2,n+1):
    for j in range(1,8):
        if i-6**j>=1 and i-9**j>=1:
            dp[i]=min(dp[i],min(dp[i-1],dp[i-6**j],dp[i-9**j])+1)
        elif i-6**j>=1:
            dp[i]=min(dp[i],min(dp[i-1],dp[i-6**j])+1)
        else:
            dp[i]=min(dp[i],dp[i-1]+1)
#print(dp)
#int(dp[1],dp[9],dp[36],dp[127])
print((dp[n]))

# Optimized solution
n=int(eval(input()))
dp=[float('inf')]*(n+1)
dp[1]=1
i=0
while 9**i<=n:
    dp[9**i]=1
    i+=1
j=0
while 6**j<=n:
    dp[6**j]=1
    j+=1
for i in range(2,n+1):
    for j in range(1,8):
        if i-6**j>=1 and i-9**j>=1:
            dp[i]=min(dp[i],min(dp[i-1],dp[i-6**j],dp[i-9**j])+1)
        elif i-6**j>=1:
            dp[i]=min(dp[i],min(dp[i-1],dp[i-6**j])+1)
        else:
            dp[i]=min(dp[i],dp[i-1]+1)
print((dp[n]))