#include <algorithm>
#include <climits>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;


#define all(collection) (collection).begin(), (collection).end()
#define loop(i, times) for(long i = 0; i < times; i++)
#define seloop(i, start_index, end_index) for(int i = start_index; i < end_index; i++)
#define eqseloop(i, start_index, end_index) for(int i = start_index; i <= end_index; i++)

struct Bank
{
	int count_;
	int money_;
	bool operator > (const Bank& b) const
	{
		return (money_ > b.money_);
	}
	bool operator < (const Bank& b) const
	{
		return (money_ < b.money_);
	}
};


class ABC99C1
{
public:
	ABC99C1(int n)
	{
		n_ = n;
		count_sum_ = 0;
	}
	int Run()
	{
		BankX(1);
		BankX(6);
		BankX(9);

		sort(bank_collection_.begin(), bank_collection_.end(), greater<Bank>());
		GetMoney();
		return count_sum_;
	}
private:
	int n_;
	int count_sum_;
	vector<Bank> bank_collection_;
	vector<int> debug_collection_;
	void BankX(int x)
	{
		Bank bank;

		if (x == 1)
		{
			loop(i, 5)
			{
				bank.count_ = (i + 1);
				bank.money_ = (i + 1);
				bank_collection_.push_back(bank);
			}
			return;
		}

		int base = 1;
		while (1)
		{
			base *= x;
			if (100000 < base)
				break;

			loop(i, 2)
			{
				if (100000 < base * (i + 1))
					break;
				bank.count_ = (i + 1);
				bank.money_ = base * (i + 1);
				bank_collection_.push_back(bank);
			}
		}
	}
	void GetMoney()
	{
		for (const auto & elm_bank : bank_collection_)
		{
			if (0 <= n_ - elm_bank.money_)
			{
				n_ -= elm_bank.money_;
				count_sum_ += elm_bank.count_;
			//	cout << elm_bank.money_ << endl;
			}
		}
	}
};


class NyaGadgetPositionalNotation
{	// N進数を表現するクラス
public:
	NyaGadgetPositionalNotation(int cardinal)
	{
		cardinal_ = cardinal;
		symbol_index_collection_.push_back(0);
	}
	void Inc()
	{	// 返り値なしのインクリメント
		bool carry = true;
		int index = (int)symbol_index_collection_.size() - 1;
		while (0 <= index && carry)
		{
			if (symbol_index_collection_[index] != cardinal_ - 1)
			{
				symbol_index_collection_[index]++;
				carry = false;
			}
			else
			{	// 桁上り
				symbol_index_collection_[index] = 0;
				if (index == 0)
					symbol_index_collection_.push_front(1);
				carry = true;
			}
			index--;
		}
	}
	NyaGadgetPositionalNotation operator ++()
	{	// 前置インクリメント
		Inc();
		return *this;
	}
	NyaGadgetPositionalNotation operator ++(int)
	{  // 後置インクリメント
		NyaGadgetPositionalNotation pn = *this;
		Inc();
		return pn;
	}
	/**
	@brief N進数取得関数
	@note
	 d桁のN進数として文字列で取得
	---------------------------------------------------------------------------
	[sample]
	int main()
	{	// 16進数テスト
		NyaGadgetPositionalNotation pn(16);

		for (int i = 0; i < 30; i++)
			pn++;

		cout << pn() << endl;		// 30を16進数で表示
		cout << pn(5) << endl;		// 5桁表示

		return 0;
	}
	---------------------------------------------------------------------------
	[output]
	1E
	0001E
	**/
	string operator()(int d = 0)
	{
		std::string positional_nation;

		for (const auto& e : symbol_index_collection_)
			positional_nation += symbols_[e];

		if (d == 0 || d == (int)positional_nation.size())
			return positional_nation;

		if ((int)positional_nation.size() < d)
		{	// 表示桁に空きがあるときは0で埋める
			std::string zero(d - positional_nation.size(), '0');
			positional_nation = zero + positional_nation;
		}

		return positional_nation;
	}
	/**
	@brief d桁目を取得する演算子
	@note
	 d桁目の数字を文字列で取得する。
	-------------------------------------------------------------------------- -
	[sample]
	int main()
	{	// 16進数テスト
		NyaGadgetPositionalNotation pn(16);

		for (int i = 0; i < 30; i++)
			pn++;

		cout << pn.() << endl;		// 30を16進数で表示
		cout << pn[0] << endl;		// １桁目を表示
		cout << pn[1] << endl;		// ２桁目を表示

		return 0;
	}
	-------------------------------------------------------------------------- -
	[output]
	1E
	E
	1
	**/
	char operator[](int d)
	{	// d桁目の数字を取得する
		int index = (int)symbol_index_collection_.size() - 1 - d;
		if (index < 0)
			return symbols_[0][0];
		return symbols_[symbol_index_collection_[index]][0];
	}
private:
	int cardinal_;	// 基数
	string symbols_[36] =
	{	// 36進数まで対応
		"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
		"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
		"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
		"U", "V", "W", "X", "Y", "Z",
	};
	// シンボルそのものでなく、シンボルに対応するindexを格納していく
	deque<int> symbol_index_collection_;
};


struct Ans {
	int count_;
	int sum_;
	Ans()
	{
		count_ = sum_ = 0;
	}
	bool operator < (const Ans& a) const
	{
		return (count_ < a.count_);
	}
};

class ABC99C2
{
public:
	ABC99C2(int n)
	{
		n_ = n;
	}
	int Run()
	{
		int get = 1;
		get_collection_.push_back(get);

		while (1)
		{
			get *= 6;
			if (100000 < get)
				break;
			get_collection_.push_back(get);
		}

		get = 1;
		while (1)
		{
			get *= 9;
			if (100000 < get)
				break;
			get_collection_.push_back(get);
		}

		ans_collection_.resize(1 << (int)get_collection_.size());
		sort(get_collection_.begin(), get_collection_.end(), greater<int>());
		NyaGadgetPositionalNotation pn(2);
		loop(i, 1 << (int)get_collection_.size())
		{
			GetMoney(pn, n_, ans_collection_[i]);
			pn++;
		}

		sort(ans_collection_.begin(), ans_collection_.end());
		for (const auto & e : ans_collection_)
		{
			if (e.sum_ == n_)
			{
				return e.count_;
			}
		}
		return 0;
	}
private:
	int n_;
	vector<int> get_collection_;
	vector<Ans> ans_collection_;
	void GetMoney(NyaGadgetPositionalNotation& pn, const int& n, Ans& ans)
	{
		loop(i, get_collection_.size())
		{
			if (pn[i] == '1')
			{
				while (ans.sum_ + get_collection_[i] <= n)
				{
					ans.sum_ += get_collection_[i];
					ans.count_++;
				}

			}
		}
	}
};

int main()
{
	int n; cin >> n;
	int ans1, ans2;
	ABC99C1 abc99c1(n);
	ans1 = abc99c1.Run();
	ABC99C2 abc99c2(n);
	ans2 = abc99c2.Run();

	cout << min(ans1, ans2);

	return 0;
}

Measured memory_bytes: 256