#DP　動的計画法



N = int(eval(input()))



INF = 10 ** 9

dp = [INF] * (N+1) #初期化配列



dp[0] = 0

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

    power = 1

    while power <= N:

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

        power *= 6

    power = 1

    while power <= N:

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

        power *= 9



#print (dp)

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



INF = 10 ** 9



dp = [INF] * (N + 1)

dp[0] = 0



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

    dp[i] = dp[i - 1] + 1

    

    tmp = 6

    while i - tmp >= 0:

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

        tmp *= 6

    

    tmp = 9

    while i - tmp >= 0:

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

        tmp *= 9



# print (dp)

print((dp[N]))
# DP　動的計画法
N = int(eval(input()))
INF = 10**9
dp = [INF] * (N + 1)  # 初期化配列
dp[0] = 0
for n in range(1, N + 1):
    power = 1
    while power <= N:
        dp[n] = min(dp[n], dp[n - power] + 1)
        power *= 6
    power = 1
    while power <= N:
        dp[n] = min(dp[n], dp[n - power] + 1)
        power *= 9
# print (dp)
print((dp[N]))

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

-# DP　動的計画法 -dp = [INF] * (N + 1)  # 初期化配列 +dp = [INF] * (N + 1) -for n in range(1, N + 1): -    power = 1 -    while power <= N: -        dp[n] = min(dp[n], dp[n - power] + 1) -        power *= 6 -    power = 1 -    while power <= N: -        dp[n] = min(dp[n], dp[n - power] + 1) -        power *= 9 +for i in range(1, N + 1): +    dp[i] = dp[i - 1] + 1 +    tmp = 6 +    while i - tmp >= 0: +        dp[i] = min(dp[i], dp[i - tmp] + 1) +        tmp *= 6 +    tmp = 9 +    while i - tmp >= 0: +        dp[i] = min(dp[i], dp[i - tmp] + 1) +        tmp *= 9