import sys

six = [6,36,216,1296,7776,46656]

nine = [9,81,729,6561,59049]



def dfs(n):

    if n < 6:

        return n

    six_max, nine_max = (1, 1)



    for i in six:

        if n < i:

            break

        six_max = i

    

    for i in nine:

        if n < i:

            break

        nine_max = i

    

    ans = 123

    

    if nine_max != 1:

        ans = dfs(n - nine_max) + 1

    if six_max != 1:

        ans = min(ans, dfs(n - six_max) + 1)

    return ans



print((dfs(int(eval(input())))))

def dfs(n):

    if n < 6:

        return n

    elif n < 9:

        return 1 + dfs(n - 6)

    

    six_max = 1

    nine_max = 1

    while six_max * 6 <= n:

        six_max *= 6

    

    while nine_max * 9 <= n:

        nine_max *= 9

    

    return min(dfs(n - nine_max), dfs(n - six_max)) + 1



print((dfs(int(eval(input())))))

import sys

six = [6, 36, 216, 1296, 7776, 46656]
nine = [9, 81, 729, 6561, 59049]


def dfs(n):
    if n < 6:
        return n
    six_max, nine_max = (1, 1)
    for i in six:
        if n < i:
            break
        six_max = i
    for i in nine:
        if n < i:
            break
        nine_max = i
    ans = 123
    if nine_max != 1:
        ans = dfs(n - nine_max) + 1
    if six_max != 1:
        ans = min(ans, dfs(n - six_max) + 1)
    return ans


print((dfs(int(eval(input())))))

def dfs(n):
    if n < 6:
        return n
    elif n < 9:
        return 1 + dfs(n - 6)
    six_max = 1
    nine_max = 1
    while six_max * 6 <= n:
        six_max *= 6
    while nine_max * 9 <= n:
        nine_max *= 9
    return min(dfs(n - nine_max), dfs(n - six_max)) + 1


print((dfs(int(eval(input())))))

-import sys - -six = [6, 36, 216, 1296, 7776, 46656] -nine = [9, 81, 729, 6561, 59049] - - -    six_max, nine_max = (1, 1) -    for i in six: -        if n < i: -            break -        six_max = i -    for i in nine: -        if n < i: -            break -        nine_max = i -    ans = 123 -    if nine_max != 1: -        ans = dfs(n - nine_max) + 1 -    if six_max != 1: -        ans = min(ans, dfs(n - six_max) + 1) -    return ans +    elif n < 9: +        return 1 + dfs(n - 6) +    six_max = 1 +    nine_max = 1 +    while six_max * 6 <= n: +        six_max *= 6 +    while nine_max * 9 <= n: +        nine_max *= 9 +    return min(dfs(n - nine_max), dfs(n - six_max)) + 1