import sys





def solve():

    input = sys.stdin.readline

    N = int(eval(input()))

    DP = [int(i) for i in range(N + 1)]

    ni = 9

    for i in range(N):

        if ni > N: break

        for j in range(N - ni + 1):

            DP[j + ni] = min(DP[j + ni], DP[j] + 1)

        ni *= 9

    six = 6

    for i in range(N):

        if six > N: break

        for j in range(N - six + 1):

            DP[j + six] = min(DP[j + six], DP[j] + 1)

        six *= 6

    print((DP[N]))

    

    return 0



if __name__ == "__main__":

    solve()
import sys



def solve():

    input = sys.stdin.readline

    N = int(eval(input()))

    M = [1]

    for i in range(1, 12):

        if pow(6, i) > N: break

        M.append(pow(6, i))

    for i in range(1, 10):

        if pow(9, i) > N: break

        M.append(pow(9, i))

    M.sort()



    DP = [100000] * (N + 1)

    DP[N] = 0

    for i in reversed(list(range(N + 1))):

        if DP[i] < 100000:

            for m in M:

                if i - m >= 0: DP[i-m] = min(DP[i-m], DP[i] + 1)

    print((DP[0]))



    return 0



if __name__ == "__main__":

    solve()
import sys


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    DP = [int(i) for i in range(N + 1)]
    ni = 9
    for i in range(N):
        if ni > N:
            break
        for j in range(N - ni + 1):
            DP[j + ni] = min(DP[j + ni], DP[j] + 1)
        ni *= 9
    six = 6
    for i in range(N):
        if six > N:
            break
        for j in range(N - six + 1):
            DP[j + six] = min(DP[j + six], DP[j] + 1)
        six *= 6
    print((DP[N]))
    return 0


if __name__ == "__main__":
    solve()

import sys


def solve():
    input = sys.stdin.readline
    N = int(eval(input()))
    M = [1]
    for i in range(1, 12):
        if pow(6, i) > N:
            break
        M.append(pow(6, i))
    for i in range(1, 10):
        if pow(9, i) > N:
            break
        M.append(pow(9, i))
    M.sort()
    DP = [100000] * (N + 1)
    DP[N] = 0
    for i in reversed(list(range(N + 1))):
        if DP[i] < 100000:
            for m in M:
                if i - m >= 0:
                    DP[i - m] = min(DP[i - m], DP[i] + 1)
    print((DP[0]))
    return 0


if __name__ == "__main__":
    solve()

-    DP = [int(i) for i in range(N + 1)] -    ni = 9 -    for i in range(N): -        if ni > N: +    M = [1] +    for i in range(1, 12): +        if pow(6, i) > N: -        for j in range(N - ni + 1): -            DP[j + ni] = min(DP[j + ni], DP[j] + 1) -        ni *= 9 -    six = 6 -    for i in range(N): -        if six > N: +        M.append(pow(6, i)) +    for i in range(1, 10): +        if pow(9, i) > N: -        for j in range(N - six + 1): -            DP[j + six] = min(DP[j + six], DP[j] + 1) -        six *= 6 -    print((DP[N])) +        M.append(pow(9, i)) +    M.sort() +    DP = [100000] * (N + 1) +    DP[N] = 0 +    for i in reversed(list(range(N + 1))): +        if DP[i] < 100000: +            for m in M: +                if i - m >= 0: +                    DP[i - m] = min(DP[i - m], DP[i] + 1) +    print((DP[0]))