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

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

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

-# 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 +# 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 +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())