import itertools

N = int(eval(input()))



base = [1]

for i in range(1, 7):

    if 6 ** i <= N:

        base.append(6 ** i)

for i in range(1, 6):

    if 9 ** i <= N:

        base.append(9 ** i)

ans = N

dp = [0] * (N + 1)

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

    dp[i] = min([dp[i - j] + 1 if i - j >= 0 else j for j in base])



print((dp[N]))
N = int(eval(input()))



def base_n(x, n):

    results = 0

    while x > 0:

        results += x % n

        x //= n

    return results



print((min([base_n(i, 6) + base_n(N - i, 9) for i in range(N + 1)])))
import itertools

N = int(eval(input()))
base = [1]
for i in range(1, 7):
    if 6**i <= N:
        base.append(6**i)
for i in range(1, 6):
    if 9**i <= N:
        base.append(9**i)
ans = N
dp = [0] * (N + 1)
for i in range(1, N + 1):
    dp[i] = min([dp[i - j] + 1 if i - j >= 0 else j for j in base])
print((dp[N]))

N = int(eval(input()))


def base_n(x, n):
    results = 0
    while x > 0:
        results += x % n
        x //= n
    return results


print((min([base_n(i, 6) + base_n(N - i, 9) for i in range(N + 1)])))

-import itertools +N = int(eval(input())) -N = int(eval(input())) -base = [1] -for i in range(1, 7): -    if 6**i <= N: -        base.append(6**i) -for i in range(1, 6): -    if 9**i <= N: -        base.append(9**i) -ans = N -dp = [0] * (N + 1) -for i in range(1, N + 1): -    dp[i] = min([dp[i - j] + 1 if i - j >= 0 else j for j in base]) -print((dp[N])) + +def base_n(x, n): +    results = 0 +    while x > 0: +        results += x % n +        x //= n +    return results + + +print((min([base_n(i, 6) + base_n(N - i, 9) for i in range(N + 1)])))