N = int(eval(input()))



dp = [10**18] * (N + 1)

dp[0] = 0



for i in range(N):

    prd = 6

    while i + prd <= N:

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

        prd *= 6

    prd = 9

    while i + prd <= N:

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

        prd *= 9



ans = 10**18

for i, c in enumerate(dp):

    ans = min(ans, N - i + c)

print(ans)

N = int(eval(input()))



def digit(x, base):

    if x == 0:

        return 0

    return digit(x // base, base) + x % base



def calc(six, nine):

    return digit(six, 6) + digit(nine, 9)



ans = 10**18

for six in range(N + 1):

    ans = min(ans, calc(six, N - six))

print(ans)
N = int(eval(input()))
dp = [10**18] * (N + 1)
dp[0] = 0
for i in range(N):
    prd = 6
    while i + prd <= N:
        dp[i + prd] = min(dp[i + prd], dp[i] + 1)
        prd *= 6
    prd = 9
    while i + prd <= N:
        dp[i + prd] = min(dp[i + prd], dp[i] + 1)
        prd *= 9
ans = 10**18
for i, c in enumerate(dp):
    ans = min(ans, N - i + c)
print(ans)

N = int(eval(input()))


def digit(x, base):
    if x == 0:
        return 0
    return digit(x // base, base) + x % base


def calc(six, nine):
    return digit(six, 6) + digit(nine, 9)


ans = 10**18
for six in range(N + 1):
    ans = min(ans, calc(six, N - six))
print(ans)

-dp = [10**18] * (N + 1) -dp[0] = 0 -for i in range(N): -    prd = 6 -    while i + prd <= N: -        dp[i + prd] = min(dp[i + prd], dp[i] + 1) -        prd *= 6 -    prd = 9 -    while i + prd <= N: -        dp[i + prd] = min(dp[i + prd], dp[i] + 1) -        prd *= 9 + + +def digit(x, base): +    if x == 0: +        return 0 +    return digit(x // base, base) + x % base + + +def calc(six, nine): +    return digit(six, 6) + digit(nine, 9) + + -for i, c in enumerate(dp): -    ans = min(ans, N - i + c) +for six in range(N + 1): +    ans = min(ans, calc(six, N - six))