# -*- coding: utf-8 -*-



def count(val1, val2):

    num = 1

    while True:

        if val1 < (val2 ** num):

            break

        num += 1



    cnt = 0

    digit = num

    while True:

        if val1 >= val2 ** digit:

            val1 -= val2 ** digit

            cnt += 1

        else:

            digit -= 1

        

        if digit== 0:

            break



    return cnt,val1





N = int(eval(input()))

ans = N



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

    n = i

    m = N - i

    res = 0



    cnt, nn = count(n, 9)

    n = nn

    res += cnt



    cnt, mm = count(m, 6)

    m = mm

    res += cnt



    res += n + m

    ans = min(ans, res)



print(ans)

# -*- coding: utf-8 -*-



N = int(eval(input()))



ans = N

for i in range(N+1):

    n = i

    m = N - i

    res = 0



    nine = 1

    while nine*9 <= n:

        nine *= 9

    while nine != 1:

        res += n // nine

        n %= nine

        nine /= 9

    res += n



    six = 1

    while six*6 <= m:

        six *= 6

    while six != 1:

        res += m // six

        m %= six

        six /= 6

    res += m



    ans = min(ans, int(res))



print(ans)

# -*- coding: utf-8 -*-
def count(val1, val2):
    num = 1
    while True:
        if val1 < (val2**num):
            break
        num += 1
    cnt = 0
    digit = num
    while True:
        if val1 >= val2**digit:
            val1 -= val2**digit
            cnt += 1
        else:
            digit -= 1
        if digit == 0:
            break
    return cnt, val1


N = int(eval(input()))
ans = N
for i in range(0, N + 1):
    n = i
    m = N - i
    res = 0
    cnt, nn = count(n, 9)
    n = nn
    res += cnt
    cnt, mm = count(m, 6)
    m = mm
    res += cnt
    res += n + m
    ans = min(ans, res)
print(ans)

# -*- coding: utf-8 -*-
N = int(eval(input()))
ans = N
for i in range(N + 1):
    n = i
    m = N - i
    res = 0
    nine = 1
    while nine * 9 <= n:
        nine *= 9
    while nine != 1:
        res += n // nine
        n %= nine
        nine /= 9
    res += n
    six = 1
    while six * 6 <= m:
        six *= 6
    while six != 1:
        res += m // six
        m %= six
        six /= 6
    res += m
    ans = min(ans, int(res))
print(ans)

-def count(val1, val2): -    num = 1 -    while True: -        if val1 < (val2**num): -            break -        num += 1 -    cnt = 0 -    digit = num -    while True: -        if val1 >= val2**digit: -            val1 -= val2**digit -            cnt += 1 -        else: -            digit -= 1 -        if digit == 0: -            break -    return cnt, val1 - - -for i in range(0, N + 1): +for i in range(N + 1): -    cnt, nn = count(n, 9) -    n = nn -    res += cnt -    cnt, mm = count(m, 6) -    m = mm -    res += cnt -    res += n + m -    ans = min(ans, res) +    nine = 1 +    while nine * 9 <= n: +        nine *= 9 +    while nine != 1: +        res += n // nine +        n %= nine +        nine /= 9 +    res += n +    six = 1 +    while six * 6 <= m: +        six *= 6 +    while six != 1: +        res += m // six +        m %= six +        six /= 6 +    res += m +    ans = min(ans, int(res))