INF=9999999



n=int(eval(input()))

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

dp[-1]=0

for i in range(n,0,-1):

    e6=1

    while 6**e6<=i:

        dp[i-6**e6]=min(dp[i]+1,dp[i-6**e6])

        e6+=1

    e9=1

    while 9**e9<=i:

        dp[i-9**e9]=min(dp[i]+1,dp[i-9**e9])

        e9+=1

ans=INF

for i in range(n):

    if dp[i]==0:

        continue

    ans=min(ans,i+dp[i])

if ans==INF:

    ans=n

print(ans)

#補助関数

def convert(num,radix):

    res = 0

    while num > 0:

        res += num % radix

        num //= radix

    return res



#入力

INF = (1 << 31)

N   = int(eval(input()))

ans = INF



#計算

for i in range(N + 1):

    dec6 = i

    dec9 = N - i

    ans  = min(ans , convert(dec6,6) + convert(dec9,9))



#出力

print(ans)

INF = 9999999
n = int(eval(input()))
dp = [INF for i in range(n + 1)]
dp[-1] = 0
for i in range(n, 0, -1):
    e6 = 1
    while 6**e6 <= i:
        dp[i - 6**e6] = min(dp[i] + 1, dp[i - 6**e6])
        e6 += 1
    e9 = 1
    while 9**e9 <= i:
        dp[i - 9**e9] = min(dp[i] + 1, dp[i - 9**e9])
        e9 += 1
ans = INF
for i in range(n):
    if dp[i] == 0:
        continue
    ans = min(ans, i + dp[i])
if ans == INF:
    ans = n
print(ans)

# 補助関数
def convert(num, radix):
    res = 0
    while num > 0:
        res += num % radix
        num //= radix
    return res


# 入力
INF = 1 << 31
N = int(eval(input()))
ans = INF
# 計算
for i in range(N + 1):
    dec6 = i
    dec9 = N - i
    ans = min(ans, convert(dec6, 6) + convert(dec9, 9))
# 出力
print(ans)

-INF = 9999999 -n = int(eval(input())) -dp = [INF for i in range(n + 1)] -dp[-1] = 0 -for i in range(n, 0, -1): -    e6 = 1 -    while 6**e6 <= i: -        dp[i - 6**e6] = min(dp[i] + 1, dp[i - 6**e6]) -        e6 += 1 -    e9 = 1 -    while 9**e9 <= i: -        dp[i - 9**e9] = min(dp[i] + 1, dp[i - 9**e9]) -        e9 += 1 +# 補助関数 +def convert(num, radix): +    res = 0 +    while num > 0: +        res += num % radix +        num //= radix +    return res + + +# 入力 +INF = 1 << 31 +N = int(eval(input())) -for i in range(n): -    if dp[i] == 0: -        continue -    ans = min(ans, i + dp[i]) -if ans == INF: -    ans = n +# 計算 +for i in range(N + 1): +    dec6 = i +    dec9 = N - i +    ans = min(ans, convert(dec6, 6) + convert(dec9, 9)) +# 出力