N=int(eval(input()))

weights=[1]

pow6=6

while N>=pow6:

    weights.append(pow6)

    pow6*=6

pow9=9

while N>=pow9:

    weights.append(pow9)

    pow9*=9

dp=[1<<28]*(N+1)

dp[0]=0

for i in range(len(weights)):

    for j in range(weights[i],N+1):

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

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

res=N

for i in range(N+1):

    cc=0

    tmp=i

    while tmp>0:

        cc+=tmp%6

        tmp=tmp//6

    tmp=N-i

    while tmp>0:

        cc+=tmp%9

        tmp=tmp//9

    res=min(res,cc)

print(res)
N = int(eval(input()))
weights = [1]
pow6 = 6
while N >= pow6:
    weights.append(pow6)
    pow6 *= 6
pow9 = 9
while N >= pow9:
    weights.append(pow9)
    pow9 *= 9
dp = [1 << 28] * (N + 1)
dp[0] = 0
for i in range(len(weights)):
    for j in range(weights[i], N + 1):
        dp[j] = min(dp[j], dp[j - weights[i]] + 1)
print((dp[N]))

N = int(eval(input()))
res = N
for i in range(N + 1):
    cc = 0
    tmp = i
    while tmp > 0:
        cc += tmp % 6
        tmp = tmp // 6
    tmp = N - i
    while tmp > 0:
        cc += tmp % 9
        tmp = tmp // 9
    res = min(res, cc)
print(res)

-weights = [1] -pow6 = 6 -while N >= pow6: -    weights.append(pow6) -    pow6 *= 6 -pow9 = 9 -while N >= pow9: -    weights.append(pow9) -    pow9 *= 9 -dp = [1 << 28] * (N + 1) -dp[0] = 0 -for i in range(len(weights)): -    for j in range(weights[i], N + 1): -        dp[j] = min(dp[j], dp[j - weights[i]] + 1) -print((dp[N])) +res = N +for i in range(N + 1): +    cc = 0 +    tmp = i +    while tmp > 0: +        cc += tmp % 6 +        tmp = tmp // 6 +    tmp = N - i +    while tmp > 0: +        cc += tmp % 9 +        tmp = tmp // 9 +    res = min(res, cc) +print(res)