N = int(eval(input()))

dp = [N] * (N+1)

dp[0] = 0



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

    pow6 = []

    pow9 = []

    for j in range(i):

        if 6**j > i:

            break

        pow6.append(6**j)

    for k in range(i):

        if 9**k > i:

            break

        pow9.append(9**k)

    L = set(pow6 + pow9)

    

    for l in L:

        if i-l < 0:

            break

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



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

INF = 10**9

dp = [INF]*(N+1)

dp[0] = 0

num6 = [6**i for i in range(1, 10)]

num9 = [9**i for i in range(1, 7)]

S = sorted([1] + num6 + num9)



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

    tmp = INF

    for j in S:

        if j > i:

            break

        else:

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

            tmp = dp[i]



print((dp[N]))
N = int(eval(input()))
dp = [N] * (N + 1)
dp[0] = 0
for i in range(1, N + 1):
    pow6 = []
    pow9 = []
    for j in range(i):
        if 6**j > i:
            break
        pow6.append(6**j)
    for k in range(i):
        if 9**k > i:
            break
        pow9.append(9**k)
    L = set(pow6 + pow9)
    for l in L:
        if i - l < 0:
            break
        dp[i] = min(dp[i], dp[i - l] + 1)
print((dp[N]))

N = int(eval(input()))
INF = 10**9
dp = [INF] * (N + 1)
dp[0] = 0
num6 = [6**i for i in range(1, 10)]
num9 = [9**i for i in range(1, 7)]
S = sorted([1] + num6 + num9)
for i in range(1, N + 1):
    tmp = INF
    for j in S:
        if j > i:
            break
        else:
            dp[i] = min(tmp, dp[i - j] + 1)
            tmp = dp[i]
print((dp[N]))

-dp = [N] * (N + 1) +INF = 10**9 +dp = [INF] * (N + 1) +num6 = [6**i for i in range(1, 10)] +num9 = [9**i for i in range(1, 7)] +S = sorted([1] + num6 + num9) -    pow6 = [] -    pow9 = [] -    for j in range(i): -        if 6**j > i: +    tmp = INF +    for j in S: +        if j > i: -        pow6.append(6**j) -    for k in range(i): -        if 9**k > i: -            break -        pow9.append(9**k) -    L = set(pow6 + pow9) -    for l in L: -        if i - l < 0: -            break -        dp[i] = min(dp[i], dp[i - l] + 1) +        else: +            dp[i] = min(tmp, dp[i - j] + 1) +            tmp = dp[i]