import sys

input = sys.stdin.readline



def main():

    N = int(eval(input()))

    ind = [1]

    tmp = 6

    while tmp <= N:

        ind.append(tmp)

        tmp *= 6

    tmp = 9

    while tmp <= N:

        ind.append(tmp)

        tmp *= 9

    ind = sorted(ind)

    INF = int(1e9)

    dp = [INF] * (N+1)

    dp[0] = 0

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

        for t in ind:

            if i - t >= 0:

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

            else:

                break

    # print(dp[:20])

    print((dp[N]))

    





if __name__ == "__main__":

    main()
import sys

input = sys.stdin.readline



def main():

    N = int(eval(input()))

    ans = N

    for i in range(N+1):# 6^pで何円引き出すか

        count = 0

        tmp = i

        while tmp > 0:

            count += tmp % 6

            tmp //= 6

        tmp = N - i

        while tmp > 0:

            count += tmp % 9

            tmp //= 9

        ans = min(ans, count)

    print(ans)



if __name__ == "__main__":

    main()
import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    ind = [1]
    tmp = 6
    while tmp <= N:
        ind.append(tmp)
        tmp *= 6
    tmp = 9
    while tmp <= N:
        ind.append(tmp)
        tmp *= 9
    ind = sorted(ind)
    INF = int(1e9)
    dp = [INF] * (N + 1)
    dp[0] = 0
    for i in range(1, N + 1):
        for t in ind:
            if i - t >= 0:
                dp[i] = min(dp[i], dp[i - t] + 1)
            else:
                break
    # print(dp[:20])
    print((dp[N]))


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    N = int(eval(input()))
    ans = N
    for i in range(N + 1):  # 6^pで何円引き出すか
        count = 0
        tmp = i
        while tmp > 0:
            count += tmp % 6
            tmp //= 6
        tmp = N - i
        while tmp > 0:
            count += tmp % 9
            tmp //= 9
        ans = min(ans, count)
    print(ans)


if __name__ == "__main__":
    main()

-    ind = [1] -    tmp = 6 -    while tmp <= N: -        ind.append(tmp) -        tmp *= 6 -    tmp = 9 -    while tmp <= N: -        ind.append(tmp) -        tmp *= 9 -    ind = sorted(ind) -    INF = int(1e9) -    dp = [INF] * (N + 1) -    dp[0] = 0 -    for i in range(1, N + 1): -        for t in ind: -            if i - t >= 0: -                dp[i] = min(dp[i], dp[i - t] + 1) -            else: -                break -    # print(dp[:20]) -    print((dp[N])) +    ans = N +    for i in range(N + 1):  # 6^pで何円引き出すか +        count = 0 +        tmp = i +        while tmp > 0: +            count += tmp % 6 +            tmp //= 6 +        tmp = N - i +        while tmp > 0: +            count += tmp % 9 +            tmp //= 9 +        ans = min(ans, count) +    print(ans)