CodeEff/ECCO huggingface dataset:
# Original solution
def getN():
    return int(eval(input()))
def getNM():
    return list(map(int, input().split()))
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(eval(input())) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()

from collections import defaultdict, deque, Counter
from sys import exit
import heapq
import math
import fractions
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

N = getN()

num = [1]
sixsta = 6
while sixsta < 100000:
    num.append(sixsta)
    sixsta *= 6

ninesta = 9
while ninesta < 100000:
    num.append(ninesta)
    ninesta *= 9
num.sort()

dp = [float('inf')] * 100001
dp[0] = 0
for i in range(N + 1):
    for k in range(len(num)):
        if i >= num[k]:
            dp[i] = min(dp[i], dp[i - num[k]] + 1)
print((dp[N]))


# Optimized solution
def getN():
    return int(eval(input()))
def getNM():
    return list(map(int, input().split()))
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(eval(input())) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7

#############
# Main Code #
#############

N = getN()

coin = [1]
sixsta = 6
while sixsta < 100000:
    coin.append(sixsta)
    sixsta *= 6

ninesta = 9
while ninesta < 100000:
    coin.append(ninesta)
    ninesta *= 9
coin.sort()

# NlogNダイクストラ
# これも最短経路を求める問題
def dijkstra(n, start):
    dist = [float('inf')] * n
    pos = [(0, start)]
    heapify(pos)
    dist[start] = 0

    while len(pos):
        cost, u = heappop(pos)

        if dist[u] < cost:
            continue
        # エッジは探索のたびに生成していく
        for i in range(len(coin)):
            if u + coin[i] >= n:
                continue
            if dist[u] + 1 < dist[u + coin[i]]:
                dist[u + coin[i]] = dist[u] + 1
                heappush(pos, (dist[u] + 1, u + coin[i]))

    return dist

print((dijkstra(N + 1, 0)[N]))
# Original solution
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
import sys
# import numpy as np
# from scipy.special import comb

sys.setrecursionlimit(100000)
INF = float('inf')

N = int(eval(input()))
memo = [INF] * (N + 1)
memo[0] = 0


def f(n):

    # 0なら終了
    if n == 0:
        return 0

    # メモされてたら終了
    if not memo[n] == INF:
        return memo[n]

    ans = INF

    # 9
    for i in range(1, n):
        if 9 ** i > n:
            break
        ans = min(ans, f(n - 9**i) + 1)

    # 6
    for i in range(1, n):
        if 6 ** i > n:
            break
        ans = min(ans, f(n - 6**i) + 1)

    # 1
    ans = min(ans, f(n - 1) + 1)

    # メモ
    memo[n] = ans
    return ans


def main():
    print((f(N)))


if __name__ == '__main__':
    main()


# Optimized solution
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
import sys
# import numpy as np
# from scipy.special import comb

sys.setrecursionlimit(100000)
INF = 100100100

N = int(eval(input()))
memo = [-1] * (N + 1)
memo[0] = 0


def f(n):

    # 0なら終了
    if n == 0:
        return 0

    # メモされてたら終了
    if not memo[n] == -1:
        return memo[n]

    ans = INF

    # 9
    for i in range(1, n):
        if 9 ** i > n:
            break
        ans = min(ans, f(n - 9**i) + 1)

    # 6
    for i in range(1, n):
        if 6 ** i > n:
            break
        ans = min(ans, f(n - 6**i) + 1)

    # 1
    ans = min(ans, f(n - 1) + 1)

    # メモ
    memo[n] = ans
    return ans


def main():

    print((f(N)))


if __name__ == '__main__':
    main()
# Original solution
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
import sys
# import numpy as np
# from scipy.special import comb

sys.setrecursionlimit(100000)
INF = 100100100

N = int(eval(input()))
memo = [-1] * (N + 1)
memo[0] = 0


def f(n):

    # 0なら終了
    if n == 0:
        return 0

    # メモされてたら終了
    if not memo[n] == -1:
        return memo[n]

    ans = INF

    # 9
    for i in range(1, n):
        if 9 ** i > n:
            break
        ans = min(ans, f(n - 9**i) + 1)

    # 6
    for i in range(1, n):
        if 6 ** i > n:
            break
        ans = min(ans, f(n - 6**i) + 1)

    # 1
    ans = min(ans, f(n - 1) + 1)

    # メモ
    memo[n] = ans
    return ans


def main():

    print((f(N)))


if __name__ == '__main__':
    main()


# Optimized solution
# import math
# import itertools
# from collections import deque
# from collections import defaultdict
# import heapq
# import sys
# import numpy as np
# from scipy.special import comb

INF = 100100100


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

    memo = [INF] * (N + 1)
    memo[0] = 0

    for i in range(1, N + 1):
        for nine in range(1, i + 1):
            if 9**nine > i:
                break
            memo[i] = min(memo[i], memo[i - 9**nine] + 1)
        for six in range(1, i + 1):
            if 6**six > i:
                break
            memo[i] = min(memo[i], memo[i - 6**six] + 1)
        memo[i] = min(memo[i], memo[i - 1] + 1)

    print((memo[N]))


if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))

dp = [float('inf')]*100001
dp[0] = 0
for i in range(1,100001):
    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]))

# Optimized solution
n = int(eval(input()))
minC = n
for i in range(n+1):
    cnt = 0
    t = i
    while t > 0:
        cnt += t%6
        t //= 6
    t = n-i
    
    while t > 0:
        cnt += t%9
        t //= 9
        
    if minC > cnt:
        minC = cnt
print(minC)
# Original solution
# 引き出したいお金の額を入力する
N = int(eval(input()))

# 整数aの累乗のうち、整数N以下かつ最も大きいものを返す関数を定義
def func(N, a):
    index = 0
    while True:
        if a ** (index + 1) <= N:
            index += 1
        else:
            break
    return a ** index

# 引き出し回数を再帰的に求める関数を定義
def search_times_re(N):
    times = 0
    if N < 6:
        times = N
        return times
    elif N < 9:
        times = 1 + (N - 6)
        return times
    else:
        N1 = search_times_re(N - func(N, 6))
        N2 = search_times_re(N - func(N, 9))
        if N1 < N2:
            return 1 + N1
        else:
            return 1 + N2

print((search_times_re(N)))

# Optimized solution
N = int(eval(input()))
ans = N
for i in range(0, N + 1, 6):
    cnt = 0
    j = N - i
    while i > 0:
        cnt += i % 6
        i //= 6
    while j > 0:
        cnt += j % 9
        j //= 9
    ans = min(ans, cnt)
print(ans)
# Original solution
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict, Counter
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def i_inpl(): return int(eval(input()))
def s_inpl(): return list(map(int,input().split()))
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

N = i_inpl()
dp = [INF for _ in range(N+1)]

dp[0] = 0

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

print((dp[N]))

# Optimized solution
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict, Counter
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def i_inpl(): return int(eval(input()))
def s_inpl(): return list(map(int,input().split()))
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")

N = i_inpl()
dp = [INF for _ in range(N+1)]

dp[0] = 0

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

print((dp[N]))
# Original solution
N = int(eval(input()))
DP = [100000000] * (N+1)
DP[0] = 0
for i in range (N):
    DP[i + 1] = DP[i] + 1
    for j in range (100):
        if 6**j <= i + 1:
            DP[i + 1] = min(DP[i + 1],DP[i + 1 - 6**j] + 1)
    for j in range (100):
        if 9**j <= i + 1:
            DP[i + 1] = min(DP[i + 1],DP[i + 1 - 9**j] + 1)
print((DP[N]))

# Optimized solution
N = int(eval(input()))
DP = [100000000] * (N+1)
DP[0] = 0
for i in range (N):
    DP[i + 1] = DP[i] + 1
    for j in range (8):
        if 6**j <= i + 1:
            DP[i + 1] = min(DP[i + 1],DP[i + 1 - 6**j] + 1)
    for j in range (8):
        if 9**j <= i + 1:
            DP[i + 1] = min(DP[i + 1],DP[i + 1 - 9**j] + 1)
print((DP[N]))
# Original solution
n = int(eval(input()))
dp = [1000000] * 100010
dp[0] = 0
for i in range(n):
    a = 1
    while i + a <= 100000:
        dp[i + a] = min(dp[i + a], dp[i] + 1)
        a *= 6
    b = 1
    while i + b <= 100000:
        dp[i + b] = min(dp[i + b], dp[i] + 1)
        b *= 9
print((dp[n]))


# Optimized solution
n = int(eval(input()))
dp = [1000000] * 100010
dp[0] = 0
for i in range(1, n + 1):
    a = 1
    while a <= i:
        dp[i] = min(dp[i], dp[i - a] + 1)
        a *= 6
    b = 1
    while b <= i:
        dp[i] = min(dp[i], dp[i - b] + 1)
        b *= 9
print((dp[n]))
# Original solution
N = int(eval(input()))

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

def f(amount):
    if amount <= 5:
        return amount
    s = Base10toN(amount, 6)
    fs = f(amount - 6 ** (len(s) - 1))
    n = Base10toN(amount, 9)
    fn = f(amount - 9 ** (len(n) - 1))
    if (fs < fn):
        return fs + 1
    else:
        return fn + 1

print((f(N)))

# Optimized solution
N= int(eval(input()))
memo = [-1] * 100001

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

def f(amount):
    if memo[amount] != -1:
        return memo[amount]
    if amount <= 5:
        memo[amount] = amount
        return amount
    s = Base10toN(amount, 6)
    fs = f(amount - 6 ** (len(s) - 1))
    n = Base10toN(amount, 9)
    fn = f(amount - 9 ** (len(n) - 1))
    if (fs < fn):
        memo[amount] = fs + 1
        return fs + 1
    else:
        memo[amount] = fn + 1
        return fn + 1

print((f(N)))
# Original solution
def main():
    n = int(eval(input()))
    money = sorted([6 ** i for i in range(7)] + [9 ** i for i in range(1, 6)], reverse=True)
    dp = [0] + [1000000] * n
    for i in range(1, n + 1):
        dp[i] = min(dp[i], *[dp[i - j] + 1 for j in money if i - j >= 0])
    print((dp[n]))

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    n = int(eval(input()))
    money = sorted([6 ** i for i in range(7)] + [9 ** i for i in range(1, 6)], reverse=True)
    dp = [0] * (n + 1)
    for i in range(1, n + 1):
        dp[i] = min(dp[i - j] + 1 for j in money if i - j >= 0)
    print((dp[n]))

if __name__ == '__main__':
    main()
# Original solution
N=int(eval(input()))
dp=[10**7]*3000000
dp[0]=0

pow6=[6**j for j in range(10) if 6**j <= N]
pow9=[9**j for j in range(10) if 9**j <= 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
N = int(eval(input()))

ans = 10**10
for i in range(N+1):
    cc = 0

    t = i
    while t > 0:
        cc += t % 6
        t //= 6

    t = N-i
    while t > 0:
        cc += t % 9
        t //= 9

    if ans > cc:
        ans = cc
print(ans)
# Original solution
# -*- coding: utf-8 -*-
# AtCoder Beginner Contest
# Problem C


if __name__ == '__main__':
    n = int(eval(input()))
    dp = [0] + [100001 for _ in range(n + 1)]

    # See:
    # https://www.youtube.com/watch?v=I-8BnRkq6Ow
    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
# -*- coding: utf-8 -*-


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

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

        six = 6

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

        nine = 9

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

    print((dp[n]))


if __name__ == '__main__':
    main()
# Original solution
N=int(eval(input()))
dp=[float('inf') for i in range(100001)]
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()))
dp=[float('inf') for i in range(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]))
# Original solution
n = int(eval(input()))
mn = 1e10

for i in range(n+1):
    ans = 0
    a, b = i, n-i

    while a>=6:
        a, mo = divmod(a, 6)
        ans += mo
    ans += a

    while b>=9:
        b, mo = divmod(b, 9)
        ans += mo
    ans += b

    mn = min(mn, ans)
print(mn)

# Optimized solution
def rec(n, x):
        d, m = divmod(n, x)
        if d: return m + rec(d, x)
        else: return m

ans = n = int(eval(input()))
for i in range(n+1):
        ans = min(ans, rec(i, 6)+rec(n-i, 9))
print(ans)
# Original solution
import sys
sys.setrecursionlimit(10 ** 9)
from functools import lru_cache
def input(): return sys.stdin.readline().rstrip()

def main():
    N = int(eval(input()))
    yen = [6 ** i for i in range(1, 7)]
    yen += [9 ** i for i in range(1, 6)]
    yen = [i for i in yen if i <= N]
    yen.sort(reverse=True)

    def MIN(a, b):
        if a > b:
            return b
        else:
            return a

    @lru_cache(maxsize=None)
    def dfs(x, n, m):
        n += 1
        for y in yen:
            if n >= m:
                return n
            if y > x:
                continue
            if x - y < 6:
                return n + x - y
            m = MIN(dfs(x - y, n, m), m)
        return m

    print((dfs(N, 0, N)))

if __name__ == '__main__':
    main()


# Optimized solution
import sys
def input(): return sys.stdin.readline().rstrip()

def MIN(a, b):
    if a > b:
        return b
    else:
        return a

def main():
    N = int(eval(input()))
    yen6 = [6 ** i for i in range(1, 7) if 6 ** i <= N]
    yen9 = [9 ** i for i in range(1, 6) if 9 ** i <= N]

    if N < 6:
        print(N)
        exit()

    dp = [N] * (N + 1)
    for i in range(6):
        dp[i] = i
    for i in range(1, N + 1):
        for j in yen6:
            if i - j < 0:
                break
            dp[i] = MIN(dp[i], dp[i - j] + 1)
        for j in yen9:
            if i - j < 0:
                break
            dp[i] = MIN(dp[i], dp[i - j] + 1)
    print((dp[N]))

if __name__ == '__main__':
    main()
# Original solution
import sys
from math import log, floor

n = int(sys.stdin.readline().rstrip())

def main():
    cand = set()
    for i in range(floor(log(n, 6)) + 1):
        if 6 ** i <= n:
            cand.add(6 ** i)
        if 9 ** i <= n:
            cand.add(9 ** i)
    cand = sorted(cand)

    res = set([0])
    cnt = 0
    while True:
        nex = set()
        for val in res:
            for c in cand:
                if val + c <= n:
                    nex.add(val + c)
                else:
                    break
        cnt += 1
        if n in nex:
            return cnt
        res = nex
    
if __name__ == '__main__':
    ans = main()
    print(ans)

# Optimized solution
import sys

n = int(sys.stdin.readline().rstrip())

def main():
    bit6 = [0] * 8
    bit9 = [0] * 7
    b = n
    for i in range(5, -1, -1):
        q, r = divmod(b, 9 ** i)
        bit9[i] = q
        b = r

    res = sum(bit6) + sum(bit9)

    for _ in range(n):
        i = 0
        while True:
            bit6[i] += 1
            if bit6[i] == 6:
                bit6[i] = 0
                i += 1
            else:
                break

        i = 0
        while True:
            bit9[i] -= 1
            if bit9[i] == -1:
                bit9[i] = 8
                i += 1
            else:
                break        

        res = min(res, sum(bit6) + sum(bit9))

    return res

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

def count(n, b):
    res = 0
    while n:
        n, r = divmod(n, b)
        res += r
    return res

n = int(sys.stdin.readline().rstrip())

def main():
    res = float('inf')
    for i in range(n + 1):
        res = min(res, count(i, 6) + count(n - i, 9))
    print(res)

if __name__ ==  '__main__':
    main()

# Optimized solution
import sys

def count(n, b):
    res = 0
    while n:
        res += n % b
        n //= b
    return res

n = int(sys.stdin.readline().rstrip())

def main():
    res = float('inf')
    for i in range(n + 1):
        res = min(res, count(i, 6) + count(n - i, 9))
    print(res)

if __name__ ==  '__main__':
    main()
# Original solution
#DP　動的計画法

N = int(eval(input()))

INF = 10 ** 9
dp = [INF] * (N+1) #初期化配列

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

#print (dp)
print((dp[N]))

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

INF = 10 ** 9

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

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

# print (dp)
print((dp[N]))
# Original solution
# coding:utf-8

n = int(input().rstrip())

dic = [None for i in range(n + 1)]
def get_ans(amount:int):
    if dic[amount] is not None:
        return dic[amount]

    if amount <= 0:
        return 0

    ans = amount

    for i in reversed(list(range(6))):
        num = pow(9, i)
        if num > amount:
            continue
        ret = get_ans(amount - num) + 1
        ans = min(ans, ret)

    for i in reversed(list(range(7))):
        num = pow(6, i)
        if num > amount:
            continue
        ret = get_ans(amount - num) + 1
        ans = min(ans, ret)

    dic[amount] = ans
    return ans

print((get_ans(n)))

# Optimized solution
# coding: utf-8

def get_pow(target: int, base: int):
    ans = 0
    current = base
    for i in range(1, target + 1):
        if current > target:
            break
        ans = i
        current *= base
    return ans


dic = {}


def get_ans(remain: int):
    if remain in dic:
        return dic[remain]
    if remain < 6:
        return max(0, remain)
    ans = remain

    pos9 = get_pow(remain, 9)
    for i in reversed(list(range(1, pos9 + 1))):
        ret = get_ans(remain - pow(9, i)) + 1
        ans = min(ans, ret)

    pow6 = get_pow(remain, 6)
    for i in reversed(list(range(1, pow6 + 1))):
        ret = get_ans(remain - pow(6, i)) + 1
        ans = min(ans, ret)

    dic[remain] = ans
    return ans


n = int(input().rstrip())
print((get_ans(n)))
# Original solution
N = int(eval(input()))

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

coin6 = [6**n for n in range(1,10) if 6**n <= N]
coin9 = [9**n for n in range(1,10) if 9**n <= N]

for i in range(N):
    dp[i+1] = min(dp[i+1], dp[i]+1)
    for j in coin6:
        if j+i <= N:
            dp[i+j]=min(dp[i+j],dp[i]+1)
    for j in coin9:
        if j+i <= N:
            dp[i+j]=min(dp[i+j],dp[i]+1)
print((dp[N]))

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

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

coin6 = [6**n for n in range(1,10) if 6**n <= N]
coin9 = [9**n for n in range(1,10) if 9**n <= N]

for i in range(N+1):
    dp[i] = min(dp[i], dp[i-1]+1)
    for j in coin6:
        if i-j >= 0:
            dp[i]=min(dp[i],dp[i-j]+1)
    for j in coin9:
        if i+j >= 0:
            dp[i]=min(dp[i],dp[i-j]+1)
print((dp[N]))
# Original solution
n = int(eval(input()))
dp = [i for i in range(n+1)]  # dp table 最初は全て1円で払う
for j in range(1, n+1):
    power = 1
    while power <= j:
        dp[j] = min(dp[j], dp[j - power] + 1)
        power *= 6
    power = 1
    while power <= j:
        dp[j] = min(dp[j], dp[j - power] + 1)
        power *= 9
print((dp[n]))


# Optimized solution
n = int(eval(input()))
dp = [i for i in range(n + 1)]
for i in range(1, n+1):
    pow = 6
    while i - pow > -1:
        dp[i] = min(dp[i], dp[i - pow] + 1)
        pow *= 6
    pow = 9
    while i - pow > -1:
        dp[i] = min(dp[i], dp[i - pow] + 1)
        pow *= 9
# print(dp)
print((dp[n]))
# Original solution
n = int(eval(input()))
dp = [float("inf")]*(n+1)
dp[0] = 0
for i in range(n):
	j = 0
	while i+6**j <= n:
		dp[i+6**j] = min(dp[i+6**j],dp[i]+1)
		j += 1
	k = 0
	while i+9**k <= n:
		dp[i+9**k] = min(dp[i+9**k],dp[i]+1)
		k += 1
print((dp[n]))

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