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

def drawal(x):
  if(x <= 5):
    return x
  else:
    def drawal9(y):
      i = 0
      while 9** (i+ 1)<= y:
        i+= 1
      return drawal(y- 9** i)+ 1
    def drawal6(z):
      i = 0
      while 6** (i+ 1)<= z:
        i+= 1
      return drawal(z- 6** i)+ 1
    return min(drawal9(x), drawal6(x))

print((drawal(N)))

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

def drawal(x):
  if(x <= 5):
    return x
  elif(6<= x and x < 9):
    return x- 5
  elif(9<= x and x < 15):
    return x- 8
  else:
    def drawal9(y):
      i = 0
      while 9** (i+ 1)<= y:
        i+= 1
      return drawal(y- 9** i)+ 1
    def drawal6(z):
      i = 0
      while 6** (i+ 1)<= z:
        i+= 1
      return drawal(z- 6** i)+ 1
    return min(drawal9(x), drawal6(x))

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

h = [1]

t = []
i = 1
while True:
  t.append(6**i)
  i += 1
  if t[-1]>10000:
    break
h.extend(t)

t = []
i = 1
while True:
  t.append(9**i)
  i += 1
  if t[-1]>10000:
    break
h.extend(t)

h.sort()

DP = []
DP.append((0, 0))

hlen = len(h)
for i in range(1,N+1):
  m = 999999
  p = 0
  for j in range(hlen):
    if i-h[j]<0:
      break
    else:
      if m>DP[i-h[j]][0] + 1:
        m = DP[i-h[j]][0] + 1
        p = h[j] + DP[i-h[j]][0]
  DP.append((m, p))  
print((DP[N][0]))

# Optimized solution
N = int(eval(input()))
 
h = [1]
 
t = []
i = 1
while True:
  t.append(6**i)
  i += 1
  if t[-1]>10000:
    break
h.extend(t)
 
t = []
i = 1
while True:
  t.append(9**i)
  i += 1
  if t[-1]>10000:
    break
h.extend(t)
 
h.sort()
 
DP = []
DP.append(0)
 
hlen = len(h)
for i in range(1,N+1):
  m = 999999
  for j in range(hlen):
    if i-h[j]<0:
      break
    else:
      if m>DP[i-h[j]] + 1:
        m = DP[i-h[j]] + 1
  DP.append(m)  
print((DP[N]))
# Original solution
import sys
input = sys.stdin.readline

def main():
    N = int(eval(input()))
    ind = [1]
    tmp = 6
    while tmp <= N:
        ind.append(tmp)
        tmp *= 6
    tmp = 9
    while tmp <= N:
        ind.append(tmp)
        tmp *= 9
    ind = sorted(ind)
    INF = int(1e9)
    dp = [INF] * (N+1)
    dp[0] = 0
    for i in range(1, N+1):
        for t in ind:
            if i - t >= 0:
                dp[i] = min(dp[i], dp[i-t]+1)
            else:
                break
    # print(dp[:20])
    print((dp[N]))
    


if __name__ == "__main__":
    main()

# Optimized solution
import sys
input = sys.stdin.readline

def main():
    N = int(eval(input()))
    ans = N
    for i in range(N+1):# 6^pで何円引き出すか
        count = 0
        tmp = i
        while tmp > 0:
            count += tmp % 6
            tmp //= 6
        tmp = N - i
        while tmp > 0:
            count += tmp % 9
            tmp //= 9
        ans = min(ans, count)
    print(ans)

if __name__ == "__main__":
    main()
# Original solution
n = int(eval(input()))
L = [1]
for i in range(1, 6):
    L.append(pow(9, i))
for i in range(1, 7):
    L.append(pow(6, i))
L = sorted(L)
dp = [float("inf") for _ in range(n + 1)]
dp[0] = 0
for i in range(1, n + 1):
    for l in L:
        if i - l >= 0:
            dp[i] = min(dp[i], dp[i - l] + 1)
print((dp[n]))


# Optimized solution
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n = int(eval(input()))
    List = [1]

    for i in range(1, 7):
        List.append(pow(6, i))
        List.append(pow(9, i))

    List.sort()
    dp = [f_inf for _ in range(n + 1)]
    dp[0] = 0

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

    print((dp[n]))


if __name__ == '__main__':
    resolve()
# Original solution
n = int(eval(input()))
dp = [200000] * (n+10)
dp[0] = 0
# dp[1] = 1
for i in range(n):
    dp[i+1] = min(dp[i+1], dp[i]+1)
    six = 6
    while i + six <= n:
        dp[i+six] = min(dp[i+six], dp[i]+1)
        six *= 6 

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

print((dp[n]))

# Optimized solution
n = int(eval(input()))
dp = [200000] * (n+10)
dp[0] = 0
for i in range(1,n+1):
    dp[i] = min(dp[i-1]+1, dp[i])
    
    six = 6
    while i - six >= 0:
        dp[i] = min(dp[i-six]+1, dp[i])
        six *= 6 

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

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

INF = N
dp = [0, 1, 2, 3, 4, 5] + [INF for _ in range(N-5)]

for amount in range(6, N+1):
    dp[amount] = amount

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

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

print((dp[N]))

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

dp = list(range(N+1))

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

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

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

dp = list(range(N+1))

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

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

print((dp[N]))

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

dp = list(range(N+1))

def calc(unit_base):
    unit = unit_base
    while unit <= amount:
        dp[amount] = min(dp[amount], dp[amount-unit]+1)
        unit *= unit_base

for amount in range(6, N+1):
    calc(6)
    calc(9)

print((dp[N]))
# Original solution
N=int(eval(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)


# Optimized solution
N=int(eval(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]))
# 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 = [int(i) for i in range(N+1)]

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

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

dp = [int(i) for i in range(N+1)]
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)))

for i in L:
    for j in range(1, N+1):
        if j - i >= 0:
            dp[j] = min(dp[j], dp[j-i] + 1)
print((dp[N]))
# Original solution
n=int(eval(input()))
l6=[]
l9=[]
num6=6
num9=9
while num6<=n:
  l6.append(num6)
  num6*=6
while num9<=n:
  l9.append(num9)
  num9*=9
count=[0,1,2,3,4,5,1,2,3,1,2,3]
for i in range(12,n+1):
  min=100000
  for j in l6:
    if i-j<0:
      pass
    else:
      if min>count[i-j]+1:
        min=count[i-j]+1
  for j in l9:
    if i-j<0:
      pass
    else:
      if min>count[i-j]+1:
        min=count[i-j]+1
  count.append(min)
print((count[n]))

# Optimized solution
n=int(eval(input()))
num6=6
num9=9
l=[]
while num6<=n:
  l.append(num6)
  num6*=6
while num9<=n:
  l.append(num9)
  num9*=9
count=[0,1,2,3,4,5,1,2,3,1,2,3]
for i in range(12,n+1):
  min=100000
  for j in l:
    if i-j<0:
      pass
    else:
      if min>count[i-j]+1:
        min=count[i-j]+1
  count.append(min)
print((count[n]))
# Original solution
coin = [1,
6,6**2,6**3,6**4,6**5,6**6,
9,9**2,9**3,9**4,9**5]
n = int(eval(input()))
inf = 10**9
dp = [inf]*(n+1)
dp[0] = 0
for c in coin:
    for yen in range(c, n+1):
        dp[yen] = min(dp[yen], dp[yen-c] + 1)
print((dp[n]))


# Optimized solution
n = int(eval(input()))
dp = list(i for i in range(n+1))

def f(yen, p):
    c = p
    while yen >= c:
        dp[yen] = min(dp[yen], dp[yen-c] + 1)
        c *= p

for yen in range(6, n+1):
    f(yen, 6)
    f(yen, 9)
print((dp[n]))
# Original solution
n = int(eval(input()))
dp = list(i for i in range(n+1))

def f(yen, p):
    c = p
    while yen >= c:
        dp[yen] = min(dp[yen], dp[yen-c] + 1)
        c *= p

for yen in range(6, n+1):
    f(yen, 6)
    f(yen, 9)
print((dp[n]))


# Optimized solution
def f(yen, coin, cnt=0):
    while yen > 0: cnt += yen % coin; yen //= coin
    return cnt

ans = n = int(eval(input()))
for yen6 in range(0, n+1):
    ans = min(ans, f(n-yen6, 9, f(yen6, 6)))
print(ans)
# Original solution
N = int(eval(input()))
list6 = [6**x for x in range(0,7) ]
list9 = [9**x for x in range(0,6) ]

def f(x):
    if x < 6:
        return x
    else:
        max6 = max(y for y in list6 if y <= x)
        max9 = max(y for y in list9 if y <= x)
        x6 = x - max6
        x9 = x - max9
        return min(f(x6)+ 1,f(x9)+ 1)

print((f(N)))

# Optimized solution
N = int(eval(input()))
list6 = [6**x for x in range(0,7) ]
list9 = [9**x for x in range(0,6) ]
result = {}

def f(x):
    if x < 6:
        return x
    elif x in result:
        return result[x] 
    else:
        max6 = max(y for y in list6 if y <= x)
        max9 = max(y for y in list9 if y <= x)
        x6 = x - max6
        x9 = x - max9
        result[x] = min(f(x6)+ 1,f(x9)+ 1)
        return result[x]

print((f(N)))
# Original solution
# -*- coding: utf-8 -*-

def count(val1, val2):
    num = 1
    while True:
        if val1 < (val2 ** num):
            res = 0
            if num > 1:
                res = val2 ** (num-1)

            break  
        num += 1

    return res


N = int(eval(input()))
ans = N

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

    while True:
        num = count(n, 9)
        if num == 0:
            break
        n -= num
        res += 1

    while True:
        num = count(m, 6)
        if num == 0:
            break
        m -= num
        res += 1

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


print(ans)


# Optimized solution
# -*- 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(eval(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)
# Original solution
# -*- 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(eval(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)


# Optimized solution
# -*- coding: utf-8 -*-

N = int(eval(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)
# Original solution
# -*- 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(eval(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)


# Optimized solution
# -*- 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(eval(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)
# Original solution
# -*- 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(eval(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)


# Optimized solution
# -*- 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(eval(input()))

max_n = 110000
memo = [-1] * max_n
ans = func(N)
print(ans)
# Original solution
N = int(eval(input()))
n6 = 1
n9 = 1
li = []
while 9**n9 <= N:
    li.append(9**n9)
    n9 += 1
while 6**n6 <= N:
    li.append(6**n6)
    n6 += 1

li.sort()
INF = 10**10
# 1円の場合はね
dp = [[i for i in range(N+1)] for i in range(len(li) + 1)]
dp = [i for i in range(N+1)]


for i,l in enumerate(li):
    for j in range(N+1):
        if j+l > N : continue
        dp[j+l] = min(dp[j+l], dp[j]+1)
print((dp[N]))


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

array = []
cnt6 = 0
while 6**cnt6 <= N:
    array.append(6**cnt6)
    cnt6 += 1
    
cnt9 = 0
while 9**cnt9 <= N:
    array.append(9**cnt9)
    cnt9 += 1

a = sorted(list(set(array)))
# 最小
dp = [i for i in range(N+1)]
# print(a)
n = 6
for n in a:
    for i in range(N+1):
        if i+n <= N:
            dp[i+n] = min(dp[i]+1, dp[i+n])
print((dp[N]))
# Original solution
n = int(eval(input()))
v = [1,6,36,216,1296,7776,46656,9,81,729,6561,59049]
dp = [float("inf") for i in range(n+1)]
dp[0] = 0
for i in range(n):
	for j in v:
		if i + j <= n:
			dp[i+j] = min(dp[i+j], dp[i]+1)
print(("{}".format(dp[n])))


# Optimized solution
def ds(a, b):
	return a if a<b else ds(a//b,b)+a%b
ans = float('inf')
n = int(eval(input()))
for i in range(n+1):
	ans = min(ans,ds(i,6)+ds(n-i,9))
print(ans)
# Original solution
def ds(a, b):
	return a if a<b else ds(a//b,b)+a%b
ans = float('inf')
n = int(eval(input()))
for i in range(n+1):
	ans = min(ans,ds(i,6)+ds(n-i,9))
print(ans)

# Optimized solution
def ds(a, b):
	return a if a<b else ds(a//b,b)+a%b
n = int(eval(input()))
print((min([ds(i,6)+ds(n-i,9) for i in range(n+1)])))
# Original solution
N = int(eval(input()))
dp = [1e10]*(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]))

# Optimized solution
N = int(eval(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)
# Original solution
def main():
    n=int(eval(input()))
    mon6=[6]
    mon9=[1]
    for i in range(5):
        mon6.append(6*mon6[i])
        mon9.append(9*mon9[i])
    mon=mon6+mon9
    mon.sort()
    
    
    inf=10**9
    dp=[[inf]*(n+1) for i in range(13)]
    
    for i in range(13):
      dp[i][0]=0
    for i in range(12):
        for j in range(1,n+1):
            if j-mon[i]>=0:
                dp[i+1][j]=min(dp[i+1][j-mon[i]]+1,dp[i][j])
            else:
                dp[i+1][j]=dp[i][j]
    print((dp[-1][-1])) 
if __name__=='__main__':
    main()

# Optimized solution
import sys
sys.setrecursionlimit(10000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

def main():
    N = int(eval(input()))
    money = [1]
    for i in range(10):
        money.append(money[-1]*6)
    money.append(9)
    for i in range(9):
        money.append(money[-1]*9)
    
    dp = [INF] * (1 + N)
    dp[0] = 0
    for m in money:
        for j in range(m,N + 1):
            dp[j] = min(dp[j],dp[j - m] + 1)
    ans = dp[-1]
    print(ans)
if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
dp = [float('inf')] * 100010
dp[0] = 0
for i in range(1, 100000 + 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]))

# Optimized solution
def funcDP(n):
    dp = [float('inf')] * (n + 10)
    dp[0] = 0
    for i in range(1, n + 1):
        for base in [6, 9]:
            powered = 1
            while powered <= i:
                dp[i] = min(dp[i], dp[i - powered] + 1)
                powered *= base
    return dp[n]

if __name__ == '__main__':
    print((funcDP(int(eval(input())))))
# Original solution
def funcDP(n):
    dp = [float('inf')] * (n + 10)
    dp[0] = 0
    for i in range(1, n + 1):
        for base in [6, 9]:
            powered = 1
            while powered <= i:
                dp[i] = min(dp[i], dp[i - powered] + 1)
                powered *= base
    return dp[n]

if __name__ == '__main__':
    print((funcDP(int(eval(input())))))

# Optimized solution
def main():
    inf = 1 << 30

    N = int(eval(input()))

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

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

    print((dp[N]))


if __name__ == '__main__':
    main()
# Original solution
from math import log
N = int(eval(input()))

hiki9 = []
hiki6 = []
n = int(log(N) / log(9) + 1)  # log9の計算に誤差があるので一つ多めに取っておく
for i in range(n+1):
    hiki9.append(9**i)
n = int(log(N) / log(6) + 1)
for i in range(n+1):
    hiki6.append(6**i)
hiki9.sort(reverse=True)
hiki6.sort(reverse=True)
x = []
for i in range(N+1):
    n9 = i
    tmpx = 0
    for h in hiki9:
        while h <= n9:
            n9 -= h
            tmpx += 1
    n6 = N - i
    for h in hiki6:
        while h <= n6:
            n6 -= h
            tmpx += 1
    x.append(tmpx)
x.sort()
print((x[0]))

# Optimized solution
N = int(eval(input()))
koho = [1]
i=1
while True:
    if i > 1e6:
        break
    i *= 6
    koho.append(i)
i=1
while True:
    if i > 1e6:
        break
    i *= 9
    koho.append(i)

dp = [0] * (N+1)
for i in range(1, N+1):
    tmp = []
    for k in koho:
        if k > i:
            continue
        tmp.append(dp[i - k] + 1)
    dp[i] = min(tmp)
print((dp[N]))
# Original solution
n=int(eval(input()))

n9=9**5
n6=6**6

k9=5+1
k6=6+1
n9=9**k9
smin=n
for i9 in range(n9):
    ii9=i9
    s=0
    m=0
    astr=""
    for ii in range(1,k9+1):
        r9=ii9%9
        ii9=(ii9-r9)//9    
        s=s*9+r9
        m=m+r9
        astr=astr+str(r9)
#    print("i9",i9,"astr",astr,"s",s,"m",m)
    
    nr=n-s
    nr1=nr
#    print("nr",nr,"m",m)
    
    if nr==0:
        snra=m
#        print(nr,m)
    elif nr>0:
        snra=m
        for jj in range(k6,-1,-1):
            nra=nr1//(6**jj)
            nr1=nr1%(6**jj)
            snra=snra+nra
#        print("jj",jj,"nra",nra,"nr1",nr1)
    if smin>snra:
        smin=snra
print(smin)

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

k9=5
k6=6
n9=9**k9
smin=n
for i9 in range(n9):
    ii9=i9
    s=0
    m=0
    astr=""
    for ii in range(1,k9+2):
        r9=ii9%9
        ii9=(ii9-r9)//9    
        s=s*9+r9
        m=m+r9
        astr=astr+str(r9)
#    print("i9",i9,"astr",astr,"s",s,"m",m)
    
    nr=n-s
    nr1=nr
#    print("nr",nr,"m",m)
    
    if nr==0:
        snra=m
#        print(nr,m)
    elif nr>0:
        snra=m
        for jj in range(k6,-1,-1):
            nra=nr1//(6**jj)
            nr1=nr1%(6**jj)
            snra=snra+nra
#        print("jj",jj,"nra",nra,"nr1",nr1)
    if smin>snra:
        smin=snra
print(smin)
# Original solution
n=int(eval(input()))

k9=5
k6=6
n9=9**k9
smin=n
for i9 in range(n9):
    ii9=i9
    s=0
    m=0
    astr=""
    for ii in range(1,k9+2):
        r9=ii9%9
        ii9=(ii9-r9)//9    
        s=s*9+r9
        m=m+r9
        astr=astr+str(r9)
#    print("i9",i9,"astr",astr,"s",s,"m",m)
    
    nr=n-s
    nr1=nr
#    print("nr",nr,"m",m)
    
    if nr==0:
        snra=m
#        print(nr,m)
    elif nr>0:
        snra=m
        for jj in range(k6,-1,-1):
            nra=nr1//(6**jj)
            nr1=nr1%(6**jj)
            snra=snra+nra
#        print("jj",jj,"nra",nra,"nr1",nr1)
    if smin>snra:
        smin=snra
print(smin)


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

res=n
for i in range(n+1):
    cc=0
    t=i
    while t>0:
        cc=cc+t%6
        t=t//6
    t=n-i
    while t>0:
        cc=cc+t%9
        t=t//9
    if res>cc:
        res=cc

print(res)
# Original solution
import itertools,sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
if N<=5:
    print(N)
    exit()
dp = [float('INF')]*(N+1)
dp[0] = 0
num6,num9 = 6,9
d6d9 = set()
while num6<=N:
    d6d9.add(num6)
    num6 *= 6
while num9<=N:
    d6d9.add(num9)
    num9 *= 9
for i,x in itertools.product(list(range(N+1)),d6d9):
    if i+x>=N+1:
        continue
    dp[i+1] = min(dp[i+1],dp[i]+1)
    dp[i+x] = min(dp[i+x],dp[i]+1)
print((dp[-1]))


# Optimized solution
import itertools,sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
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)
    num6,num9 = 6,9
    while i+num6<=N:
        dp[i+num6] = min(dp[i+num6],dp[i]+1)
        num6 *= 6
    while i+num9<=N:
        dp[i+num9] = min(dp[i+num9],dp[i]+1)
        num9 *=9
print((dp[-1]))
# Original solution
import sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
dp = [float('INF')]*(N+1) #1_indexed
dp[0] = 0
for i in range(N+1):
    if i+1<=N:
        dp[i+1] = min(dp[i+1],dp[i+1],dp[i]+1)
    num6,num9 = 6,9
    while i+num6<=N:
        dp[i+num6] = min(dp[i+num6],dp[i]+1)
        num6 *= 6
    while i+num9<=N:
        dp[i+num9] = min(dp[i+num9],dp[i]+1)
        num9 *= 9
print((dp[-1]))


# Optimized solution
import sys
def I(): return int(sys.stdin.readline().rstrip())
N = I()
dp = [float('INF')]*(N+1) #1_indexed
dp[0] = 0
for i in range(N+1):
    if i-1>=0:
        dp[i] = min(dp[i],dp[i-1]+1)
    num6,num9 = 6,9
    while i-num6>=0:
        dp[i] = min(dp[i],dp[i-num6]+1)
        num6 *= 6
    while i-num9>=0:
        dp[i] = min(dp[i],dp[i-num9]+1)
        num9 *= 9
print((dp[-1]))