Water Calculator

Time limit: 0.08s Memory limit: 32MB Input: Output:

Task

William has recently built a Water CalculatorTM{}^{TM}, which mimics the operation of a computer through water pipes, sinks and siphons. Due to the rudimentary nature of its circuits, the Water CalculatorTM{}^{TM} is able to store a single number up to a billion and perform only a handful of atomic operations:

  • add or subtract 11;
  • multiply by 22;
  • divide by a power of 33 (which divides exactly the stored number).

These few operations are anyway more than sufficient for William's basic mathematical needs.

In this moment, the Water CalculatorTM{}^{TM} is holding number NN. Since William needs to leave his home for a while, it is crucial to empty the calculator in order to avoid malicious rusting! Calculate the minimum number of atomic operations necessary for William to completely empty the calculator.

Input data

The first and only line contains the only integer NN.

Output data

You need to write a single line with an integer: the minimum number of atomic operations needed to empty the Water CalculatorTM{}^{TM}.

Constraints and clarifications

  • 1N100000001 \le N \le 10\,000\,000.
  • Operations that would increase the number stored over 10910^9 are not allowed.
# Points Constraints
1 5 Examples.
2 35 N10N \le 10
3 30 N1000N \le 1000
4 30 No additional limitations.

Example 1

stdin

13

stdout

4

Explanation

In the first sample case, a possible sequence of operations is the following: 1326271013 \rightarrow 26 \rightarrow 27 \rightarrow 1 \rightarrow 0

Example 2

stdin

81

stdout

2

Explanation

In the second sample case, a single operation brings 8181 to 11 and a second one to 00.

Log in or sign up to be able to send submissions!