def nsin(X,n):

    if(int(X/n)):

        return nsin(int(X/n),n)+str(X%n)

    return str(X)



N = int(eval(input()))

score = N

for i in range(N+1):

    score = min(score,sum(map(int,nsin(i,6)+nsin(N-i,9))))

print(score)
N = int(eval(input()))

score = N



for i in range(N+1):

    count = 0

    t = i

    while(t):

        count += t%6

        t//=6

    t = N-i

    while(t):

        count += t%9

        t//=9

    score = min(count,score)



print(score)
def nsin(X, n):
    if int(X / n):
        return nsin(int(X / n), n) + str(X % n)
    return str(X)


N = int(eval(input()))
score = N
for i in range(N + 1):
    score = min(score, sum(map(int, nsin(i, 6) + nsin(N - i, 9))))
print(score)

N = int(eval(input()))
score = N
for i in range(N + 1):
    count = 0
    t = i
    while t:
        count += t % 6
        t //= 6
    t = N - i
    while t:
        count += t % 9
        t //= 9
    score = min(count, score)
print(score)

-def nsin(X, n): -    if int(X / n): -        return nsin(int(X / n), n) + str(X % n) -    return str(X) - - -    score = min(score, sum(map(int, nsin(i, 6) + nsin(N - i, 9)))) +    count = 0 +    t = i +    while t: +        count += t % 6 +        t //= 6 +    t = N - i +    while t: +        count += t % 9 +        t //= 9 +    score = min(count, score)