# 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]))
# 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]))
# 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]))

# 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]))

-    while power <= N: +    while power <= i: -    while power <= N: +    while power <= i: