import math

N = int(eval(input()))

 

def Nsinpou(x, base):

    #配列を返す Nsin[0]が上位桁 ex. 56(base=6) => [1,3,2]

    if x == 0: return [0] #log_n(0)の回避

    Nsin = []

    for i in range((int(round(math.log(x,base))//1)+1)):

        ithdigit = x%(base**(i+1)) // (base ** i)

        Nsin.append(ithdigit)

    Nsin.reverse()

    return Nsin

 

ans = N #最大でも1円玉N枚

for i in range(N+1): #分け方全探索

    coins = sum(Nsinpou(i,6)) + sum(Nsinpou(N-i,9))

    if coins < ans:

        ans = coins

    

print(ans)
MAX_N = 10**5



N = int(eval(input()))



dp = [MAX_N]*(N+1)

dp[0] = 0

dp[1] = 1



MONEY_LIST = [6**i for i in range(7)]

MONEY_LIST.extend([9**i for i in range(1,6)])

MONEY_LIST.sort()



for i in range(N+1):

    for m in MONEY_LIST:

        if m>i:

            break

        

        dp[i] = min(dp[i],dp[i-m]+1)

        

print((dp[N]))
import math

N = int(eval(input()))


def Nsinpou(x, base):
    # 配列を返す Nsin[0]が上位桁 ex. 56(base=6) => [1,3,2]
    if x == 0:
        return [0]  # log_n(0)の回避
    Nsin = []
    for i in range((int(round(math.log(x, base)) // 1) + 1)):
        ithdigit = x % (base ** (i + 1)) // (base**i)
        Nsin.append(ithdigit)
    Nsin.reverse()
    return Nsin


ans = N  # 最大でも1円玉N枚
for i in range(N + 1):  # 分け方全探索
    coins = sum(Nsinpou(i, 6)) + sum(Nsinpou(N - i, 9))
    if coins < ans:
        ans = coins
print(ans)

MAX_N = 10**5
N = int(eval(input()))
dp = [MAX_N] * (N + 1)
dp[0] = 0
dp[1] = 1
MONEY_LIST = [6**i for i in range(7)]
MONEY_LIST.extend([9**i for i in range(1, 6)])
MONEY_LIST.sort()
for i in range(N + 1):
    for m in MONEY_LIST:
        if m > i:
            break
        dp[i] = min(dp[i], dp[i - m] + 1)
print((dp[N]))

-import math - +MAX_N = 10**5 - - -def Nsinpou(x, base): -    # 配列を返す Nsin[0]が上位桁 ex. 56(base=6) => [1,3,2] -    if x == 0: -        return [0]  # log_n(0)の回避 -    Nsin = [] -    for i in range((int(round(math.log(x, base)) // 1) + 1)): -        ithdigit = x % (base ** (i + 1)) // (base**i) -        Nsin.append(ithdigit) -    Nsin.reverse() -    return Nsin - - -ans = N  # 最大でも1円玉N枚 -for i in range(N + 1):  # 分け方全探索 -    coins = sum(Nsinpou(i, 6)) + sum(Nsinpou(N - i, 9)) -    if coins < ans: -        ans = coins -print(ans) +dp = [MAX_N] * (N + 1) +dp[0] = 0 +dp[1] = 1 +MONEY_LIST = [6**i for i in range(7)] +MONEY_LIST.extend([9**i for i in range(1, 6)]) +MONEY_LIST.sort() +for i in range(N + 1): +    for m in MONEY_LIST: +        if m > i: +            break +        dp[i] = min(dp[i], dp[i - m] + 1) +print((dp[N]))