# define _SCL_SECURE_NO_WARNINGS

# include <iostream>
# include <string>
# include <iomanip>
# include <stdio.h>
# include <array>
# include <numeric>
# include <algorithm>
# include <vector>
# include <functional>
# include <fstream>
# include <random>
# include <bitset>
# include <map>
# include <unordered_set>




using Ull = unsigned long long;
using Ll = long long;

template<typename T>
using Vec = std::vector<T>;

template<typename T, size_t L>
using Ary = std::array<T, L>;

namespace
{


    template<typename T>
    std::istream& operator >> (std::istream& is, std::vector<T>& vec)
    {
        for (T& x : vec) is >> x;
        return is;
    }

    template<typename T, size_t L>
    std::istream& operator >> (std::istream& is, std::array<T, L>& ary)
    {
        for (T& x : ary) is >> x;
        return is;
    }

    template<typename T, size_t L>
    std::istream& operator >> (std::istream& is, T(&ary)[L])
    {
        for (int i{}; i < L; ++i) is >> ary[i];
        return is;
    }






    template<typename T, size_t L>
    std::ostream& operator << (std::ostream& os, const std::array<T, L>& vec)
    {
        for (const T& x : vec) os << x << " ";
        return os;
    }

    template<typename T, size_t L, size_t L2>
    std::ostream& operator << (std::ostream& os, const std::array<std::array<T, L2>, L>& vec)
    {
        for (const std::array<T, L2>& x : vec) os << x << "\n";
        return os;
    }


    //template<typename T>
    //std::ostream& operator << (std::ostream& os, const std::vector<T>& vec)
    //{
    //    for (const T& x : vec) os << x << " ";
    //    return os;
    //}

    //template<typename T>
    //std::ostream& operator << (std::ostream& os, const std::vector<std::vector<T>>& vec)
    //{
    //    for (const std::vector<T>& x : vec) os << x << "\n";
    //    return os;
    //}

    template<typename T, template<typename> typename Container>
    std::ostream& operator << (std::ostream& os, const Container<T>& container)
    {
        for (const T& x : container) os << x << " ";
        return os;
    }

    template<typename T, template<typename> typename Container>
    std::ostream& operator << (std::ostream& os, const Container<Container<T>>& contaier)
    {
        for (const Container<T>& x : contaier) os << x << "\n";
        return os;
    }

    template<typename T, template<typename, typename > typename Container, typename U>
    std::ostream& operator << (std::ostream& os, const Container<T, U>& container)
    {
        for (const T& x : container) os << x << " ";
        return os;
    }

    template<typename T, template<typename, typename > typename Container, typename U, typename V>
    std::ostream& operator << (std::ostream& os, const Container<Container<T, U>, V>& container)
    {
        for (const Container<T, U>& x : container) os << x << "\n";
        return os;
    }


    namespace charAry
    {
        template<size_t L>
        std::istream& in(std::istream& is, char(&ary)[L])
        {
            return in(is, ary, L);
        }

        std::istream& in(std::istream& is, char* ary, size_t size)
        {
            for (int i{}; i < size; ++i)
            {
                char temp;
                is >> temp;
                ary[i] = temp;
            }
            return is;
        }
    }

    template<typename T>
    std::string to_string(std::vector<T> vec, std::string sep = " ")
    {
        using std::to_string;

        std::string result;
        for (T& x : vec) result += to_string(x) + sep;
        if (!result.empty()) result.erase(result.size() - sep.length());

        return result;
    }
    template<>
    std::string to_string(std::vector<char> vec, std::string sep)
    {
        using std::to_string;

        std::string result;
        for (char c : vec) result += std::string{c} +sep;
        if (!result.empty()) result.erase(result.size() - sep.length());

        return result;
    }

    template<typename T, size_t L>
    std::string to_string(T(&ary)[L], std::string sep = " ", size_t size = L)
    {
        using std::to_string;

        std::string result;
        for (int i = 0; i < size; ++i) result += to_string(ary[i]);
        if (!result.empty()) result.erase(result.size() - sep.length());

        return result;
    }
    template<size_t L>
    std::string to_string(char(&ary)[L], std::string sep = " ", size_t size = L)
    {
        using std::to_string;

        std::string result;
        for (int i = 0; i < size; ++i) result += ary[i];
        if (!result.empty()) result.erase(result.size() - sep.length());

        return result;
    }

    template<typename T, size_t L, typename Function>
    void for_each(T(&ary)[L], Function lambda)
    {
        for (size_t i{}; i < L; ++i)
            lambda(static_cast<T&>(ary[i]));
    }

    template<typename T>
    void SetAll(T& container, typename T::value_type value = typename T::value_type{})
    {
        for (typename T::reference content : container)
            content = value;
    }

    template<typename T, size_t L>
    void SetAll(T(&ary)[L], T value = T{})
    {
        for (size_t i{}; i < L; ++i)
            ary[i] = value;
    }

    template<typename T, size_t L>
    std::array<T, L> AsContainer(T(&ary)[L], bool isSwap = true)
    {
        std::array<T, L> result;

        if (isSwap)
        {
            for (size_t i{}; i < L; ++i)
                std::swap(result[i], ary[i]);
        }
        else
        {
            for (size_t i{}; i < L; ++i)
                result[i] = ary[i];
        }

        return result;
    }

    template<size_t length>
    constexpr std::array<size_t, length> range()
    {
        std::array<size_t, length> range{};

        for (size_t i{}; i < length; ++i)
            range[i] = i;

        return range;
    }

    template<typename T>
    const std::vector<T> range(const T length)
    {
        std::vector<T> range(length);

        for (T i{}; i < length; ++i)
            range[i] = i;

        return range;
    }

    template<typename T>
    const std::vector<T> range(const T start, const T end)
    {
        if (start >= end) return std::vector<T>{};

        std::vector<T> range(end - start);

        for (T i{start}; i < end; ++i)
            range[i - start] = i;

        return range;
    }

    bool IsPrime(const Ull n)
    {
        switch (n)
        {
        case 0: case 1:
            return false;
        case 2:
            return true;
        default:

            if (n % 2 == 0) return false;

            for (Ull i{3}; i * i <= n; i += 2)
            {
                if (n % i == 0) return false;
            }
        }

        return true;
    }

    // 最大公約数
    Ull EuclideanAlg(Ull n1, Ull n2)
    {
        if (n1 < 0 || n2 < 0) return 0;
        if (n2 > n1) std::swap(n1, n2);

        int r;
        while ((r = n1 % n2) != 0)
        {
            n1 = n2;
            n2 = r;
        }

        return n2;
    }

    // 最小公倍数
    Ull LCM(const std::vector<Ull>& values)
    {
        if (values.size() == 0)
            return 0;
        if (values.size() == 1)
            return values[0];
        if (values.size() == 2)
            return values[0] * values[1] / EuclideanAlg(values[0], values[1]);

        auto mid = values.cbegin() + values.size() / 2;

        return LCM(std::vector<Ull>{LCM(std::vector<Ull>(values.begin(), mid)), LCM(std::vector<Ull>(mid, values.end()))});
    }




    template<int E, int... A>
    struct Seq
    {
        static constexpr auto value = Seq<E - 1, E, A...>::value;
    };

    template<int... A>
    struct Seq<0, A...>
    {
        static constexpr int value[] = {0, A...};
    };

    template<int... A>
    constexpr int Seq<0, A...>::value[];





    template<typename T, typename ...Args>
    constexpr std::array<T, sizeof...(Args)> make_array(Args&&... args)
    {
        return std::array<T, sizeof...(Args)>{ static_cast<Args&&>(args)... };
    }





    template<size_t Max, int count = Max, int ... A>
    struct Count
    {
        static constexpr auto up = Count<Max, count - 1, count - 1, A...>::up;
    };

    template<size_t Max, int ... A>
    struct Count<Max, 0, A...>
    {
        static constexpr std::array<int, Max> up = {A...};
    };


    template<size_t Max, int ... A>
    constexpr std::array<int, Max> Count<Max, 0, A...>::up;



    //template<size_t Max, int count = Max, int ... A>
    //struct Count
    //{
    //    static constexpr std::array<int, Max> down = Count<Max, count - 1, A..., count - 1>::down;
    //};

    //template<size_t Max, int ... A>
    //struct Count<Max, 0, A...>
    //{
    //    static constexpr std::array<int, Max> down = {A...};
    //};

    //template<size_t Max, int ... A>
    //constexpr std::array<int, Max> Count<Max, 0, A...>::down;




    void Solve();
}

# if true
int main()
{
    Solve();
    return 0;
}
# endif



namespace
{

    void Solve()
    {
        int N;
        std::cin >> N;

        Vec<int> dp(N + 1, std::numeric_limits<int>::max());
        Vec<int> moneys = {1};
        
        for (int s{6}, n{9}; s <= N || n <= N;)
        {
            if (s < n)
            {
                moneys.push_back(s);
                s *= 6;
            }
            else if (n < s)
            {
                moneys.push_back(n);
                n *= 9;
            }
            else
            {
                moneys.push_back(s);
                s *= 6;
                n *= 9;
            }
        }


        dp[0] = 0;

        for(const int m : moneys)
            for (int i{}; i <= N; ++i)
            {
                if(i - m >= 0)
                    dp[i] = std::min(dp[i - m] + 1, dp[i]);
            }

        std::cout << dp[N];

    }

}

Measured memory_bytes: 640