Problem:
Fast Forwarding

Mr. Anderson frequently rents video tapes of his favorite classic films. Watching the films so many times, he has learned the precise start times of his favorite scenes in all such films. He now wants to find how to wind the tape to watch his favorite scene as quickly as possible on his video player.

When the [play] button is pressed, the film starts at the normal playback speed. The video player has two buttons to control the playback speed: The [3x] button triples the speed, while the [1/3x] button reduces the speed to one third. These speed control buttons, however, do not take effect on the instance they are pressed. Exactly one second after playback starts and every second thereafter, the states of these speed control buttons are checked. If the [3x] button is pressed on the timing of the check, the playback speed becomes three times the current speed. If the [1/3x] button is pressed, the playback speed becomes one third of the current speed, unless it is already the normal speed.

For instance, assume that his favorite scene starts at 19 seconds from the start of the film. When the [3x] button is on at one second and at two seconds after the playback starts, and the [1/3x] button is on at three seconds and at five seconds after the start, the desired scene can be watched in the normal speed five seconds after starting the playback, as depicted in the following chart.

Your task is to compute the shortest possible time period after the playback starts until the desired scene starts. The playback of the scene, of course, should be in the normal speed.

Input

The input consists of a single test case of the following format.

$t$

The given single integer $t$ ($0 \leq t < 2^{50}$) is the start time of the target scene.

Output

Print an integer that is the minimum possible time in seconds before he can start watching the target scene in the normal speed.

Sample Input 1

19

Sample Output 1

5

Sample Input 2

13

Sample Output 2

5

Sample Input 3

123456789098765

Sample Output 3

85

Sample Input 4

51

Sample Output 4

11

Sample Input 5

0

Sample Output 5

0

Sample Input 6

3

Sample Output 6

3

Sample Input 7

4

Sample Output 7

2

Solution in C# (judge verdict: Accepted)

using static System.Math;
using System;

public class hello
{
    public static void Main()
    {
        var t = long.Parse(Console.ReadLine().Trim());
        var n = getN(t);
        getAns(t, n);
    }
    static void getAns(long t, int n)
    {
        var t2 = t - 2 * (long)Pow(3, n) + 2;
        var n2 = n;
        var res = 0L;
        while (n2 >= 0)
        {
            var pow3 = (long)Pow(3, n2);
            var w = t2 / pow3;
            res += w;
            t2 -= w * pow3;
            n2--;
        }
        Console.WriteLine(2 * n + res);
    }
    static int getN(long t)
    {
        var n = 0;
        var a = 1L;
        while (2 * a <= t + 2)
        {
            a *= 3;
            n++;
        }
        return n - 1;
    }
}