import sys

sys.setrecursionlimit(10 ** 9)

from functools import lru_cache

def input(): return sys.stdin.readline().rstrip()



def main():

    N = int(eval(input()))

    yen = [6 ** i for i in range(1, 7)]

    yen += [9 ** i for i in range(1, 6)]

    yen = [i for i in yen if i <= N]

    yen.sort(reverse=True)



    def MIN(a, b):

        if a > b:

            return b

        else:

            return a



    @lru_cache(maxsize=None)

    def dfs(x, n, m):

        n += 1

        for y in yen:

            if n >= m:

                return n

            if y > x:

                continue

            if x - y < 6:

                return n + x - y

            m = MIN(dfs(x - y, n, m), m)

        return m



    print((dfs(N, 0, N)))



if __name__ == '__main__':

    main()

import sys

def input(): return sys.stdin.readline().rstrip()



def MIN(a, b):

    if a > b:

        return b

    else:

        return a



def main():

    N = int(eval(input()))

    yen6 = [6 ** i for i in range(1, 7) if 6 ** i <= N]

    yen9 = [9 ** i for i in range(1, 6) if 9 ** i <= N]



    if N < 6:

        print(N)

        exit()



    dp = [N] * (N + 1)

    for i in range(6):

        dp[i] = i

    for i in range(1, N + 1):

        for j in yen6:

            if i - j < 0:

                break

            dp[i] = MIN(dp[i], dp[i - j] + 1)

        for j in yen9:

            if i - j < 0:

                break

            dp[i] = MIN(dp[i], dp[i - j] + 1)

    print((dp[N]))



if __name__ == '__main__':

    main()

import sys

sys.setrecursionlimit(10**9)
from functools import lru_cache


def input():
    return sys.stdin.readline().rstrip()


def main():
    N = int(eval(input()))
    yen = [6**i for i in range(1, 7)]
    yen += [9**i for i in range(1, 6)]
    yen = [i for i in yen if i <= N]
    yen.sort(reverse=True)

    def MIN(a, b):
        if a > b:
            return b
        else:
            return a

    @lru_cache(maxsize=None)
    def dfs(x, n, m):
        n += 1
        for y in yen:
            if n >= m:
                return n
            if y > x:
                continue
            if x - y < 6:
                return n + x - y
            m = MIN(dfs(x - y, n, m), m)
        return m

    print((dfs(N, 0, N)))


if __name__ == "__main__":
    main()

import sys


def input():
    return sys.stdin.readline().rstrip()


def MIN(a, b):
    if a > b:
        return b
    else:
        return a


def main():
    N = int(eval(input()))
    yen6 = [6**i for i in range(1, 7) if 6**i <= N]
    yen9 = [9**i for i in range(1, 6) if 9**i <= N]
    if N < 6:
        print(N)
        exit()
    dp = [N] * (N + 1)
    for i in range(6):
        dp[i] = i
    for i in range(1, N + 1):
        for j in yen6:
            if i - j < 0:
                break
            dp[i] = MIN(dp[i], dp[i - j] + 1)
        for j in yen9:
            if i - j < 0:
                break
            dp[i] = MIN(dp[i], dp[i - j] + 1)
    print((dp[N]))


if __name__ == "__main__":
    main()

- -sys.setrecursionlimit(10**9) -from functools import lru_cache +def MIN(a, b): +    if a > b: +        return b +    else: +        return a + + -    yen = [6**i for i in range(1, 7)] -    yen += [9**i for i in range(1, 6)] -    yen = [i for i in yen if i <= N] -    yen.sort(reverse=True) - -    def MIN(a, b): -        if a > b: -            return b -        else: -            return a - -    @lru_cache(maxsize=None) -    def dfs(x, n, m): -        n += 1 -        for y in yen: -            if n >= m: -                return n -            if y > x: -                continue -            if x - y < 6: -                return n + x - y -            m = MIN(dfs(x - y, n, m), m) -        return m - -    print((dfs(N, 0, N))) +    yen6 = [6**i for i in range(1, 7) if 6**i <= N] +    yen9 = [9**i for i in range(1, 6) if 9**i <= N] +    if N < 6: +        print(N) +        exit() +    dp = [N] * (N + 1) +    for i in range(6): +        dp[i] = i +    for i in range(1, N + 1): +        for j in yen6: +            if i - j < 0: +                break +            dp[i] = MIN(dp[i], dp[i - j] + 1) +        for j in yen9: +            if i - j < 0: +                break +            dp[i] = MIN(dp[i], dp[i - j] + 1) +    print((dp[N]))