Under construction

Still being built. Things may change

Make 1 (Part 3)

Interview

Time limit0.5sMemory limit1024 MB

Summary
Given N up to 10^18, find the minimum number of operations (divide by 3, divide by 2, or subtract 1) to reduce N to 1.
Level

Medium5 of 10

Topics
Dynamic programming, Recursion, Hash map
Solved
No attempts yet

Problem

For an integer XX, there are three allowed operations.

  1. If XX is divisible by 3, divide it by 3.
  2. If XX is divisible by 2, divide it by 2.
  3. Subtract 1 from XX.

Given an integer NN, use these three operations as needed to make 1. Print the minimum number of operations needed.

Input

The first line contains an integer NN with 1≤N≤10181 \le N \le 10^{18}.

Output

Print the minimum number of operations on the first line.

Hint

For N=10N = 10, you can make 1 in 3 operations: 10 → 9 → 3 → 1.

Examples2

  1. Example 1

    Input
    2
    
    Expected output
    1
    
  2. Example 2

    Input
    10
    
    Expected output
    3