#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, long long);
int scanf(char *, long long *);
int main() {
  long long n, t, d, i, ans;
  while (scanf("%lld", &n) == 1) {
    ans = n;
    t = n;
    while (t > 1) {
      if (t == 2) {
        ans++;
        break;
      }
      d = t % 3;
      t = t / 3;
      ans += t;
      t += d;
    }
    printf("%lld\n", ans);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    11d5 <main+0xb5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jle    11c0 <main+0xa0>
cmpq   $0x2,-0x18(%rbp)
jne    117f <main+0x5f>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11c0 <main+0xa0>
mov    -0x18(%rbp),%rax
mov    $0x3,%ecx
cqto
idiv   %rcx
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    $0x3,%ecx
cqto
idiv   %rcx
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
add    -0x18(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1158 <main+0x38>
mov    -0x30(%rbp),%rsi
lea    0xe3a(%rip),%rdi
call   1030 <printf@plt>
jmp    112f <main+0xf>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xed1(%rip),%rdi
mov    %rsp,%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    11b4 <main+0x94>
movabs $0xaaaaaaaaaaaaaaab,%rbx
aa aa aa
lea    0xeb8(%rip),%r14
lea    0xeac(%rip),%r15
mov    %rsp,%r12
jmp    117c <main+0x5c>
nopl   0x0(%rax)
add    $0x1,%rsi
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    11b4 <main+0x94>
mov    (%rsp),%rsi
cmp    $0x2,%rsi
jl     1164 <main+0x44>
mov    %rsi,%rcx
nopl   0x0(%rax)
cmp    $0x2,%rcx
je     1160 <main+0x40>
mov    %rcx,%rax
mul    %rbx
shr    %rdx
lea    (%rdx,%rdx,2),%rax
sub    %rax,%rcx
add    %rdx,%rsi
add    %rdx,%rcx
cmp    $0x1,%rcx
ja     1190 <main+0x70>
jmp    1164 <main+0x44>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i32 0, i32* %1
br label %7
7: ; preds = %33, %0
%8 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %36
10: ; preds = %7
%11 = load i64, i64* %2
store i64 %11, i64* %6
%12 = load i64, i64* %2
store i64 %12, i64* %3
br label %13
13: ; preds = %22, %10
%14 = load i64, i64* %3
%15 = icmp sgt i64 %14, 1
br i1 %15, label %16, label %33
16: ; preds = %13
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 2
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i64, i64* %6
%21 = add nsw i64 %20, 1
store i64 %21, i64* %6
br label %33
22: ; preds = %16
%23 = load i64, i64* %3
%24 = srem i64 %23, 3
store i64 %24, i64* %4
%25 = load i64, i64* %3
%26 = sdiv i64 %25, 3
store i64 %26, i64* %3
%27 = load i64, i64* %3
%28 = load i64, i64* %6
%29 = add nsw i64 %28, %27
store i64 %29, i64* %6
%30 = load i64, i64* %4
%31 = load i64, i64* %3
%32 = add nsw i64 %31, %30
store i64 %32, i64* %3
br label %13
33: ; preds = %19, %13
%34 = load i64, i64* %6
%35 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %34)
br label %7
36: ; preds = %7
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [6 x i8] c"%lld\0A\00"
define i32 @main() {
%1 = alloca i64
%2 = bitcast i64* %1 to i8*
%3 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %1)
%4 = icmp eq i32 %3, 1
br i1 %4, label %5, label %25
5: ; preds = %0, %20
%6 = load i64, i64* %1
%7 = icmp sgt i64 %6, 1
br i1 %7, label %8, label %20
8: ; preds = %5, %14
%9 = phi i64 [ %17, %14 ], [ %6, %5 ]
%10 = phi i64 [ %18, %14 ], [ %6, %5 ]
%11 = icmp eq i64 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %8
%13 = add nsw i64 %9, 1
br label %20
14: ; preds = %8
%15 = urem i64 %10, 3
%16 = udiv i64 %10, 3
%17 = add nsw i64 %9, %16
%18 = add nuw nsw i64 %16, %15
%19 = icmp ugt i64 %18, 1
br i1 %19, label %8, label %20
20: ; preds = %14, %5, %12
%21 = phi i64 [ %13, %12 ], [ %6, %5 ], [ %17, %14 ]
%22 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i64 %21)
%23 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %1)
%24 = icmp eq i32 %23, 1
br i1 %24, label %5, label %25
25: ; preds = %20, %0
ret i32 0
}