N = int(eval(input()))

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

dp[0] = 0



for i in range(N+1):

    p = 1

    while p <= N:

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

        p *= 6

    p = 1

    while p <= N:

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

        p *= 9



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

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

dp[0] = 0



for i in range(N+1):

    p = 1

    while p <= i:

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

        p *= 6

    p = 1

    while p <= i:

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

        p *= 9



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

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

-    while p <= N: +    while p <= i: -    while p <= N: +    while p <= i: