n = int(eval(input()))

ans = n

for i in range(n+1):

    count = 0

    num = i

    while num > 0:

        count += num % 6

        num //= 6

    num = n - i

    while num > 0:

        count += num % 9

        num //= 9

    ans = min(ans, count)

print(ans)
def main():

    n = int(eval(input()))

    dp = [None]*(n+1)

    dp[0] = 0

    dp[1] = 1

    nl = list(sorted([6**i for i in range(10) if (6**i)<=n]+[9**i for i in range(1, 10) if (9**i)<=n]))

    for i in range(2, n+1):

        d = 10**15

        for j in nl:

            if j > i:

                break

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

        dp[i] = d

    print((dp[n]))



if __name__ == "__main__":

    main()
n = int(eval(input()))
ans = n
for i in range(n + 1):
    count = 0
    num = i
    while num > 0:
        count += num % 6
        num //= 6
    num = n - i
    while num > 0:
        count += num % 9
        num //= 9
    ans = min(ans, count)
print(ans)

def main():
    n = int(eval(input()))
    dp = [None] * (n + 1)
    dp[0] = 0
    dp[1] = 1
    nl = list(
        sorted(
            [6**i for i in range(10) if (6**i) <= n]
            + [9**i for i in range(1, 10) if (9**i) <= n]
        )
    )
    for i in range(2, n + 1):
        d = 10**15
        for j in nl:
            if j > i:
                break
            d = min(d, 1 + dp[i - j])
        dp[i] = d
    print((dp[n]))


if __name__ == "__main__":
    main()

-n = int(eval(input())) -ans = n -for i in range(n + 1): -    count = 0 -    num = i -    while num > 0: -        count += num % 6 -        num //= 6 -    num = n - i -    while num > 0: -        count += num % 9 -        num //= 9 -    ans = min(ans, count) -print(ans) +def main(): +    n = int(eval(input())) +    dp = [None] * (n + 1) +    dp[0] = 0 +    dp[1] = 1 +    nl = list( +        sorted( +            [6**i for i in range(10) if (6**i) <= n] +            + [9**i for i in range(1, 10) if (9**i) <= n] +        ) +    ) +    for i in range(2, n + 1): +        d = 10**15 +        for j in nl: +            if j > i: +                break +            d = min(d, 1 + dp[i - j]) +        dp[i] = d +    print((dp[n])) + + +if __name__ == "__main__": +    main()