Problem:
Score : 300 points

Problem Statement

To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:

1 yen (the currency of Japan)

6 yen, 6^2(=36) yen, 6^3(=216) yen, ...

9 yen, 9^2(=81) yen, 9^3(=729) yen, ...

At least how many operations are required to withdraw exactly N yen in total?

It is not allowed to re-deposit the money you withdrew.

Constraints

1 \leq N \leq 100000

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

If at least x operations are required to withdraw exactly N yen in total, print x.

Sample Input 1

127

Sample Output 1

4

By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.

Sample Input 2

3

Sample Output 2

3

By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.

Sample Input 3

44852

Sample Output 3

16

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;

using Value = int32_t;

constexpr Value maxN = 100000;

Value calc(Value const n) {
  if (n < 6) {
    return n;
  }

  if (n < 9) {
    return n - 5;
  }

  std::vector<Value> base9s;
  Value d = 1;
  for (int i = 0;; ++i) {
    d *= 9;
    if (d > maxN) {
      break;
    }
    base9s.insert(base9s.begin(), d);
  }
  std::vector<Value> base6s;
  d = 1;
  base6s.insert(base6s.begin(), d);
  for (int i = 0;; ++i) {
    d *= 6;
    if (d > maxN) {
      break;
    }
    base6s.insert(base6s.begin(), d);
  }

  Value ret = 0;
  std::vector<int> digit9_maxs(base9s.size(), 0);
  Value nn = n;
  for (int i = 0; i < digit9_maxs.size(); ++i) {
    digit9_maxs[i] = nn / base9s[i];
    ret += digit9_maxs[i];
    nn %= base9s[i];
  }

  for (int d0 = 0; d0 <= digit9_maxs[0]; ++d0) {
    Value const nn0 = n - d0 * base9s[0];
    if (nn0 < 0 || d0 >= ret) {
      break;
    }
    if (d0 >= ret) {
      break;
    }
    for (int d1 = 0; d1 <= digit9_maxs[1]; ++d1) {
      Value const nn1 = nn0 - d1 * base9s[1];
      if (nn1 < 0) {
        break;
      }
      Value const dd1 = d0 + d1;
      if (dd1 >= ret) {
        break;
      }
      for (int d2 = 0; d2 <= digit9_maxs[2]; ++d2) {
        Value const nn2 = nn1 - d2 * base9s[2];
        if (nn2 < 0) {
          break;
        }
        Value const dd2 = dd1 + d2;
        if (dd2 >= ret) {
          break;
        }
        for (int d3 = 0; d3 <= digit9_maxs[3]; ++d3) {
          Value const nn3 = nn2 - d3 * base9s[3];
          if (nn3 < 0) {
            break;
          }
          Value const dd3 = dd2 + d3;
          if (dd3 >= ret) {
            break;
          }
          for (int d4 = 0; d4 <= digit9_maxs[4]; ++d4) {
            Value const nn4 = nn3 - d4 * base9s[4];
            if (nn4 < 0) {
              break;
            }
            Value const dd4 = dd3 + d4;
            if (dd4 >= ret) {
              break;
            }
            Value ddd = dd4;
            Value nnn = nn4;
            for (auto b6 : base6s) {
              ddd += nnn / b6;
              nnn %= b6;
            }
            ret = std::min(ddd, ret);
          }
        }
      }
    }
  }
  return ret;
}

int main() {
  Value n;
  std::cin >> n;
  auto ret = calc(n);
  std::cout << ret << std::endl;
  return 0;
}