import sys

readline = sys.stdin.readline

from math import floor, log



def main():

    N = int(readline())

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

    for i in range(N):

        dp[i+1] = min(dp[i+1], dp[i]+1)

        log9 = floor(log(N-i, 9))

        for e in range(1, log9+2):

            if i + 9**e <= N:

	            dp[i+9**e] = min(dp[i+9**e], dp[i]+1)

        log6 = floor(log(N-i, 6))

        for e in range(1, log6+2):

            if i + 6**e <= N:

            	dp[i+6**e] = min(dp[i+6**e], dp[i]+1)

    

    print((dp[N]))





if __name__ == '__main__':

    main()

import sys

readline = sys.stdin.readline



def main():

    N = int(readline())

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



    for i in range(N):

        dp[i+1] = min(dp[i+1], dp[i]+1)

        e = 9

        while i + e <= N:

            dp[i+e] = min(dp[i+e], dp[i]+1)

            e *= 9

        e = 6

        while i + e <= N:

            dp[i+e] = min(dp[i+e], dp[i]+1)

            e *= 6



    print((dp[N]))





if __name__ == '__main__':

    main()
import sys

readline = sys.stdin.readline
from math import floor, log


def main():
    N = int(readline())
    dp = [i for i in range(N + 1)]
    for i in range(N):
        dp[i + 1] = min(dp[i + 1], dp[i] + 1)
        log9 = floor(log(N - i, 9))
        for e in range(1, log9 + 2):
            if i + 9**e <= N:
                dp[i + 9**e] = min(dp[i + 9**e], dp[i] + 1)
        log6 = floor(log(N - i, 6))
        for e in range(1, log6 + 2):
            if i + 6**e <= N:
                dp[i + 6**e] = min(dp[i + 6**e], dp[i] + 1)
    print((dp[N]))


if __name__ == "__main__":
    main()

import sys

readline = sys.stdin.readline


def main():
    N = int(readline())
    dp = [i for i in range(N + 1)]
    for i in range(N):
        dp[i + 1] = min(dp[i + 1], dp[i] + 1)
        e = 9
        while i + e <= N:
            dp[i + e] = min(dp[i + e], dp[i] + 1)
            e *= 9
        e = 6
        while i + e <= N:
            dp[i + e] = min(dp[i + e], dp[i] + 1)
            e *= 6
    print((dp[N]))


if __name__ == "__main__":
    main()

-from math import floor, log -        log9 = floor(log(N - i, 9)) -        for e in range(1, log9 + 2): -            if i + 9**e <= N: -                dp[i + 9**e] = min(dp[i + 9**e], dp[i] + 1) -        log6 = floor(log(N - i, 6)) -        for e in range(1, log6 + 2): -            if i + 6**e <= N: -                dp[i + 6**e] = min(dp[i + 6**e], dp[i] + 1) +        e = 9 +        while i + e <= N: +            dp[i + e] = min(dp[i + e], dp[i] + 1) +            e *= 9 +        e = 6 +        while i + e <= N: +            dp[i + e] = min(dp[i + e], dp[i] + 1) +            e *= 6