#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 *, int);
int scanf(char *, int *);
int main() {
  int n, s, a;
  while (scanf("%d", &n) == 1) {
    a = n;
    if (n == 0)
      break;
    s = 0;
    while (n > 1) {
      if (n == 2) {
        s += 1;
        break;
      }
      s += n / 3;
      n = (n / 3) + (n % 3);
    }
    printf("%d\n", s + a);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    11d4 <main+0xb4>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    115d <main+0x3d>
jmp    11d4 <main+0xb4>
movl   $0x0,-0xc(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11bd <main+0x9d>
cmpl   $0x2,-0x8(%rbp)
jne    1186 <main+0x66>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11bd <main+0x9d>
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
add    -0xc(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    -0x14(%rbp),%eax
add    %edx,%eax
mov    %eax,-0x8(%rbp)
jmp    1164 <main+0x44>
mov    -0xc(%rbp),%esi
add    -0x10(%rbp),%esi
lea    0xe39(%rip),%rdi
call   1030 <printf@plt>
jmp    112f <main+0xf>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
lea    0xed1(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    11b3 <main+0x93>
mov    $0xaaaaaaab,%ebx
lea    0xeb9(%rip),%r14
lea    0xeaf(%rip),%r15
lea    0x4(%rsp),%r12
jmp    117d <main+0x5d>
nopl   0x0(%rax,%rax,1)
add    $0x1,%esi
add    %eax,%esi
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <scanf@plt>
cmp    $0x1,%eax
jne    11b3 <main+0x93>
mov    0x4(%rsp),%eax
test   %eax,%eax
je     11b3 <main+0x93>
xor    %esi,%esi
cmp    $0x2,%eax
jl     1163 <main+0x43>
xor    %esi,%esi
mov    %eax,%ecx
cmp    $0x2,%ecx
je     1160 <main+0x40>
mov    %ecx,%edx
imul   %rbx,%rdx
shr    $0x21,%rdx
add    %edx,%esi
lea    (%rdx,%rdx,2),%edi
sub    %edi,%ecx
add    %edx,%ecx
mov    %ecx,0x4(%rsp)
cmp    $0x1,%ecx
ja     1190 <main+0x70>
jmp    1163 <main+0x43>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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