n = int(eval(input()))

dp = [float("Inf")]*(n+1)

for i in range(n+1):

    if i < 6:dp[i] = i

    power = 1

    while i >= 6 ** power:

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

        power += 1

    power = 1

    while i >= 9 ** power:

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

        power += 1

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

dp = [float("Inf")]*(n+1)

for i in range(n+1):

    if i < 6:dp[i] = i

    power = 1

    while i >= 6 ** power:

        if dp[i] > dp[i-6**power]+1:dp[i] = dp[i-6**power] + 1

        power += 1

    power = 1

    while i >= 9 ** power:

        if dp[i] > dp[i-9**power]+1:dp[i] = dp[i-9**power] + 1

        power += 1

print((int(dp[n])))
n = int(eval(input()))
dp = [float("Inf")] * (n + 1)
for i in range(n + 1):
    if i < 6:
        dp[i] = i
    power = 1
    while i >= 6**power:
        dp[i] = min(dp[i], dp[i - 6**power] + 1)
        power += 1
    power = 1
    while i >= 9**power:
        dp[i] = min(dp[i], dp[i - 9**power] + 1)
        power += 1
print((dp[n]))

n = int(eval(input()))
dp = [float("Inf")] * (n + 1)
for i in range(n + 1):
    if i < 6:
        dp[i] = i
    power = 1
    while i >= 6**power:
        if dp[i] > dp[i - 6**power] + 1:
            dp[i] = dp[i - 6**power] + 1
        power += 1
    power = 1
    while i >= 9**power:
        if dp[i] > dp[i - 9**power] + 1:
            dp[i] = dp[i - 9**power] + 1
        power += 1
print((int(dp[n])))

-        dp[i] = min(dp[i], dp[i - 6**power] + 1) +        if dp[i] > dp[i - 6**power] + 1: +            dp[i] = dp[i - 6**power] + 1 -        dp[i] = min(dp[i], dp[i - 9**power] + 1) +        if dp[i] > dp[i - 9**power] + 1: +            dp[i] = dp[i - 9**power] + 1 -print((dp[n])) +print((int(dp[n])))