/*
 * atcoder/abc099/c.cpp
 */

// C++ 14
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <math.h>
#include <cstring>

#define ll long long
#define Int ll
#define loop(__x, __start, __end) for(Int __x = __start; __x < __end; __x++)
#define loopdown(__x, __start, __end) for(Int __x = __start; __x > __end; __x--)
#define rep(__n) for(Int __x = 0; __x < __n; __x++)
#define span(__a,__x,__y) __a.begin()+__x,__a.begin()+__y
#define span_all(__a) __a.begin(),__a.end()
#define len(__x) (__x.size())
#define last(__x) (__x[__x.size()-1])

using namespace std;

struct Point {
  Int x, y;
  Point(Int x, Int y): x(x), y(y) {}
  Point(): x(0), y(0) {}

  Point operator + (Point v) { return Point(x + v.x, y + v.y); }
  Point operator - (Point v) { return Point(x - v.x, y - v.y); }
  bool operator == (const Point &v) const {
    return x == v.x && y == v.y;
  }
  bool operator< (const Point &p1) const {
    return x < p1.x && y < p1.y;
  }
};
ostream & operator << (ostream & out, Point const & v) {
  out << "Point(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Point & v) {
  Int x, y;
  in >> x; in >> y; v.x = x; v.y = y;
  return in;
}

// Geometry

#define EPS 0.0000000001

class Vector2 {
public:
  double x, y;
  
  Vector2(double x = 0, double y = 0): x(x), y(y) {}
  
  Vector2 operator + (Vector2 v) { return Vector2(x + v.x, y + v.y); }
  Vector2 operator - (Vector2 v) { return Vector2(x - v.x, y - v.y); }
  Vector2 operator * (double k) { return Vector2(x * k, y * k); }
  Vector2 operator / (double k) { return Vector2(x / k, y / k); }
  
  double length() { return sqrt(norm()); }
  double norm() { return x * x + y * y; }
  double dot (Vector2 v) { return x * v.x + y * v.y; }
  double cross (Vector2 v) { return x * v.y - y * v.x; }
  
  bool operator < (const Vector2 &v) const {
    return x != v.x ? x < v.x : y < v.y;
  }
  
  bool operator == (const Vector2 &v) const {
    return fabs(x - v.x) < EPS && fabs(y - v.y) < EPS;
  }
};

ostream & operator << (ostream & out, Vector2 const & v) { 
  out<< "Vector2(" << v.x << ", " << v.y << ')';
  return out;
}

istream & operator >> (istream & in, Vector2 & v) { 
  double x, y;
  in >> x;
  in >> y;
  v.x = x;
  v.y = y;
  return in;
}


vector<Int> slice(vector<Int> &vec, int s, int t) {
  return vector<Int>(vec.begin()+s, vec.begin()+t);
}

vector<Int> trim(vector<Int> &vec, int n) {
  auto init = slice(vec, 0, n);
  auto tail = slice(vec, n+2, vec.size());
  init.insert(init.end(), tail.begin(), tail.end());
  return init;
}


template <class T>
bool chmin(T &a, T b) { if (a > b) {a = b; return true; } return false; }
template <class T>
bool chmax(T &a, T b) { if (a < b) {a = b; return true; } return false; }


const Int INFTY = 100002;
const Int MAX_N = 100000;
Int N;

vector<Int> dp(MAX_N+1, INFTY);

void input() {
  cin >> N;
}

void receive() {
  dp.at(0) = 0;
  dp.at(1) = 1;
  loop(n,2,N+1) {
    Int amount = 1;
    chmin(dp.at(n), dp.at(n-amount) + 1);

    for (amount = 6; amount <= n; amount *= 6) {
      chmin(dp.at(n), dp.at(n-amount) + 1);
    }

    for (amount = 9; amount <= n; amount *= 9) {
      chmin(dp.at(n), dp.at(n-amount) + 1);
    }
  }
}


void give() {
  dp.at(0) = 0;
  dp.at(1) = 1;
  loop(n,1,N+1) {
    Int amount = 1;
    if (n+amount>N) break;
    chmin(dp.at(n+amount), dp.at(n) + 1);

    for (amount = 6; amount + n <= N; amount *= 6) {
      chmin(dp.at(n+amount), dp.at(n) + 1);
    }

    for (amount = 9; amount + n <= N; amount *= 9) {
      chmin(dp.at(n+amount), dp.at(n) + 1);
    }
  }
}

void bfs() {
  queue<Int> Q;
  Q.push(1);
  dp.at(1) = 1;

  while (Q.size()) {
    Int parent = Q.front(); Q.pop();
    Int amount = 1;
    if (parent + amount > N) break;
    Q.push(parent + amount);
    chmin(dp.at(parent + amount), dp.at(parent) + 1);

    for (amount = 6; parent + amount <= N; amount *= 6) {
      if (dp.at(parent + amount) == INFTY) {
        dp.at(parent + amount) = dp.at(parent) + 1;
        Q.push(parent + amount);
      }
    }

    for (amount = 9; parent + amount <= N; amount *= 9) {
      if (dp.at(parent + amount) == INFTY) {
        dp.at(parent + amount) = dp.at(parent) + 1;
        Q.push(parent + amount);
      }
    }
  }
}

struct Vertex {
  Int v, d;
  bool operator< (const Vertex &v) const {
    return d > v.d;
  }
};

void dijkstra() {
  priority_queue<Vertex> Q;
  Q.push({ 1, 1 });
  dp.at(1) = 1;

  while (Q.size()) {
    auto vert = Q.top(); Q.pop();
    Int parent = vert.v;
    Int dist = vert.d;
    if (dist > dp.at(parent)) continue;

    Int amount = 1;
    Int child = parent + amount;
    if (child > N) break;
    if (chmin(dp.at(child), dp.at(parent) + 1))
      Q.push({ child, dp.at(child) });

    for (amount = 6; parent + amount <= N; amount *= 6) {
      child = parent + amount;
      if (chmin(dp.at(child), dp.at(parent) + 1))
        Q.push({ child, dp.at(child) });
    }

    for (amount = 9; parent + amount <= N; amount *= 9) {
      child = parent + amount;
      if (chmin(dp.at(child), dp.at(parent) + 1))
        Q.push({ child, dp.at(child) });
    }
  }
}

void solve() {
  receive();
  cout << dp.at(N) << endl;
}

int main() {
  input();
  solve();
  return 0;
}


Measured memory_bytes: 1024