N = int(eval(input()))

dp = [1e10]*(N+1)

dp[0] = 0

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

    power = 1

    while power <= i:

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

        power *= 6

    power = 1

    while power<=i:

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

        power *= 9

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

ans = N

for i in range(N+1):

    cnt = 0

    t = i

    while t>0:

        cnt+=t%6

        t//=6

    j=N-i

    while j>0:

        cnt+=j%9

        j//=9

    ans = min(ans,cnt)

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

N = int(eval(input()))
ans = N
for i in range(N + 1):
    cnt = 0
    t = i
    while t > 0:
        cnt += t % 6
        t //= 6
    j = N - i
    while j > 0:
        cnt += j % 9
        j //= 9
    ans = min(ans, cnt)
print(ans)

-dp = [1e10] * (N + 1) -dp[0] = 0 -for i in range(1, N + 1): -    power = 1 -    while power <= i: -        dp[i] = min(dp[i], dp[i - power] + 1) -        power *= 6 -    power = 1 -    while power <= i: -        dp[i] = min(dp[i], dp[i - power] + 1) -        power *= 9 -print((dp[N])) +ans = N +for i in range(N + 1): +    cnt = 0 +    t = i +    while t > 0: +        cnt += t % 6 +        t //= 6 +    j = N - i +    while j > 0: +        cnt += j % 9 +        j //= 9 +    ans = min(ans, cnt) +print(ans)