N = int(eval(input()))

INF = float('inf')

dp = [INF] * (N+1)

dp[0] = 0



for n in range(N + 1):

    power = 0

    while pow(6, power) <= n:

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

        power += 1



    power = 0

    while pow(9, power) <= n:

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

        power += 1



print((dp[N]))

N = int(eval(input()))

INF = float('inf')

dp = [INF for _ in range(N+1)]

dp[0] = 0



for n in range(N + 1):

    power = 0

    while pow(6, power) <= n:

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

        power += 1



    power = 0

    while pow(9, power) <= n:

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

        power += 1



print((dp[N]))

N = int(eval(input()))
INF = float("inf")
dp = [INF] * (N + 1)
dp[0] = 0
for n in range(N + 1):
    power = 0
    while pow(6, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(6, power)] + 1)
        power += 1
    power = 0
    while pow(9, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(9, power)] + 1)
        power += 1
print((dp[N]))

N = int(eval(input()))
INF = float("inf")
dp = [INF for _ in range(N + 1)]
dp[0] = 0
for n in range(N + 1):
    power = 0
    while pow(6, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(6, power)] + 1)
        power += 1
    power = 0
    while pow(9, power) <= n:
        dp[n] = min(dp[n], dp[n - pow(9, power)] + 1)
        power += 1
print((dp[N]))

-dp = [INF] * (N + 1) +dp = [INF for _ in range(N + 1)]