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

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

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

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

-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 +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 -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)) +max_n = 110000 +memo = [-1] * max_n +ans = func(N)