n = int(eval(input()))

ans = float("inf")

for n_nine in range(0, n//9 + 1):

    s = n - 9 * n_nine

    n_six = s // 6

    n_one = s - 6 * n_six

    n_nine2 = 0

    n_six2 = 0

    pow_nine = 5

    pow_six = 6

    

    while n_nine >= 9:

        if n_nine >= 9 ** (pow_nine - 1):

            n_nine -= 9 ** (pow_nine - 1)

            n_nine2 += 1

        else:

          pow_nine -= 1



    while n_six >= 6:

        if n_six >= 6 ** (pow_six - 1):

            n_six -= 6 ** (pow_six - 1)

            n_six2 += 1

        else:

          pow_six -= 1          

          

    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)

          

print(ans)
def counting(num, power, base):

    num2 = 0

    while num >= base:

        if num >= base ** (power - 1):

            num -= base ** (power - 1)

            num2 += 1

        else:

            power -= 1

    return num + num2



def main():

    n = int(eval(input()))

    ans = float("inf")

    for n_nine in range(0, n// 9 + 1):

        s = n - 9 * n_nine

        n_six = s // 6

        n_1 = s - 6 * n_six

        pow_nine = 5

        pow_six = 6

        n_9 = counting(n_nine, pow_nine, 9)

        n_6 = counting(n_six, pow_six, 6)

        ans = min(ans, n_1 + n_6 + n_9)



    print(ans)



main()
n = int(eval(input()))
ans = float("inf")
for n_nine in range(0, n // 9 + 1):
    s = n - 9 * n_nine
    n_six = s // 6
    n_one = s - 6 * n_six
    n_nine2 = 0
    n_six2 = 0
    pow_nine = 5
    pow_six = 6
    while n_nine >= 9:
        if n_nine >= 9 ** (pow_nine - 1):
            n_nine -= 9 ** (pow_nine - 1)
            n_nine2 += 1
        else:
            pow_nine -= 1
    while n_six >= 6:
        if n_six >= 6 ** (pow_six - 1):
            n_six -= 6 ** (pow_six - 1)
            n_six2 += 1
        else:
            pow_six -= 1
    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)
print(ans)

def counting(num, power, base):
    num2 = 0
    while num >= base:
        if num >= base ** (power - 1):
            num -= base ** (power - 1)
            num2 += 1
        else:
            power -= 1
    return num + num2


def main():
    n = int(eval(input()))
    ans = float("inf")
    for n_nine in range(0, n // 9 + 1):
        s = n - 9 * n_nine
        n_six = s // 6
        n_1 = s - 6 * n_six
        pow_nine = 5
        pow_six = 6
        n_9 = counting(n_nine, pow_nine, 9)
        n_6 = counting(n_six, pow_six, 6)
        ans = min(ans, n_1 + n_6 + n_9)
    print(ans)


main()

-n = int(eval(input())) -ans = float("inf") -for n_nine in range(0, n // 9 + 1): -    s = n - 9 * n_nine -    n_six = s // 6 -    n_one = s - 6 * n_six -    n_nine2 = 0 -    n_six2 = 0 -    pow_nine = 5 -    pow_six = 6 -    while n_nine >= 9: -        if n_nine >= 9 ** (pow_nine - 1): -            n_nine -= 9 ** (pow_nine - 1) -            n_nine2 += 1 +def counting(num, power, base): +    num2 = 0 +    while num >= base: +        if num >= base ** (power - 1): +            num -= base ** (power - 1) +            num2 += 1 -            pow_nine -= 1 -    while n_six >= 6: -        if n_six >= 6 ** (pow_six - 1): -            n_six -= 6 ** (pow_six - 1) -            n_six2 += 1 -        else: -            pow_six -= 1 -    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one) -print(ans) +            power -= 1 +    return num + num2 + + +def main(): +    n = int(eval(input())) +    ans = float("inf") +    for n_nine in range(0, n // 9 + 1): +        s = n - 9 * n_nine +        n_six = s // 6 +        n_1 = s - 6 * n_six +        pow_nine = 5 +        pow_six = 6 +        n_9 = counting(n_nine, pow_nine, 9) +        n_6 = counting(n_six, pow_six, 6) +        ans = min(ans, n_1 + n_6 + n_9) +    print(ans) + + +main()