N = int(eval(input()))



dp = [float('inf') for _ in range(N + 1)]

dp[N] = 0



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

    if i - 1 >= 0 :

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

    dis = 6

    while i - dis >= 0 :

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

        dis *= 6

    dis = 9

    while i - dis >= 0 :

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

        dis *= 9



print((dp[0]))
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()))
dp = [float("inf") for _ in range(N + 1)]
dp[N] = 0
for i in range(N, -1, -1):
    if i - 1 >= 0:
        dp[i - 1] = min(dp[i - 1], dp[i] + 1)
    dis = 6
    while i - dis >= 0:
        dp[i - dis] = min(dp[i - dis], dp[i] + 1)
        dis *= 6
    dis = 9
    while i - dis >= 0:
        dp[i - dis] = min(dp[i - dis], dp[i] + 1)
        dis *= 9
print((dp[0]))

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)

-dp = [float("inf") for _ in range(N + 1)] -dp[N] = 0 -for i in range(N, -1, -1): -    if i - 1 >= 0: -        dp[i - 1] = min(dp[i - 1], dp[i] + 1) -    dis = 6 -    while i - dis >= 0: -        dp[i - dis] = min(dp[i - dis], dp[i] + 1) -        dis *= 6 -    dis = 9 -    while i - dis >= 0: -        dp[i - dis] = min(dp[i - dis], dp[i] + 1) -        dis *= 9 -print((dp[0])) +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)