#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 */
long long logtwo(long long);
int pow(int, long long);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, long long *);
int main() {
  long long int t;
  long long int n;
  long long int nthTermSum, nthTermStarting;
  scanf("%lld", &t);
  n = logtwo((((t / (double)3)) + 1));
  nthTermStarting = 3 * pow(2, n);
  nthTermSum = 3 * (pow(2, n) - 1);
  if (nthTermSum == t) {
    printf("1");
  } else {
    long long nPlusOnethTermSum = 3 * (pow(2, (n + 1)) - 1);
    printf("%lld\n", nPlusOnethTermSum - t + 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeba(%rip),%rdi
lea    -0x10(%rbp),%rsi
call   1050 <scanf@plt>
cvtsi2sdq -0x10(%rbp),%xmm0
movsd  0xe9b(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  0xe87(%rip),%xmm1
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rdi
call   1040 <logtwo@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x2,%edi
call   1060 <pow@plt>
imul   $0x3,%eax,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    $0x2,%edi
call   1060 <pow@plt>
sub    $0x1,%eax
imul   $0x3,%eax,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11dd <main+0x9d>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1215 <main+0xd5>
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    $0x2,%edi
call   1060 <pow@plt>
sub    $0x1,%eax
imul   $0x3,%eax,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
sub    -0x10(%rbp),%rsi
add    $0x1,%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1060 <scanf@plt>
cvtsi2sdq 0x8(%rsp),%xmm0
divsd  0xe8b(%rip),%xmm0
addsd  0xe8b(%rip),%xmm0
cvttsd2si %xmm0,%rdi
call   1050 <logtwo@plt>
mov    %rax,%rbx
mov    $0x2,%edi
mov    %rax,%rsi
call   1070 <pow@plt>
mov    $0x2,%edi
mov    %rbx,%rsi
call   1070 <pow@plt>
lea    (%rax,%rax,2),%eax
add    $0xfffffffd,%eax
cltq
cmp    %rax,0x8(%rsp)
jne    11bf <main+0x6f>
mov    $0x31,%edi
call   1030 <putchar@plt>
jmp    11f0 <main+0xa0>
add    $0x1,%rbx
mov    $0x2,%edi
mov    %rbx,%rsi
call   1070 <pow@plt>
lea    (%rax,%rax,2),%eax
add    $0xfffffffd,%eax
movslq %eax,%rsi
sub    0x8(%rsp),%rsi
add    $0x1,%rsi
lea    0xe2c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%lld\00"
@.str.1 = [2 x i8] c"1\00"
@.str.2 = [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
%7 = call i32 @scanf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64* %2)
%8 = load i64, i64* %2
%9 = sitofp i64 %8 to double
%10 = fdiv double %9, 3.000000e+00
%11 = fadd double %10, 1.000000e+00
%12 = fptosi double %11 to i64
%13 = call i64 @logtwo(i64 %12)
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = call i32 @pow(i32 2, i64 %14)
%16 = mul nsw i32 3, %15
%17 = sext i32 %16 to i64
store i64 %17, i64* %5
%18 = load i64, i64* %3
%19 = call i32 @pow(i32 2, i64 %18)
%20 = sub nsw i32 %19, 1
%21 = mul nsw i32 3, %20
%22 = sext i32 %21 to i64
store i64 %22, i64* %4
%23 = load i64, i64* %4
%24 = load i64, i64* %2
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %0
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %40
28: ; preds = %0
%29 = load i64, i64* %3
%30 = add nsw i64 %29, 1
%31 = call i32 @pow(i32 2, i64 %30)
%32 = sub nsw i32 %31, 1
%33 = mul nsw i32 3, %32
%34 = sext i32 %33 to i64
store i64 %34, i64* %6
%35 = load i64, i64* %6
%36 = load i64, i64* %2
%37 = sub nsw i64 %35, %36
%38 = add nsw i64 %37, 1
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %38)
br label %40
40: ; preds = %28, %26
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"%lld\00"
@.str.2 = [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 = load i64, i64* %1
%5 = sitofp i64 %4 to double
%6 = fdiv double %5, 3.000000e+00
%7 = fadd double %6, 1.000000e+00
%8 = fptosi double %7 to i64
%9 = call i64 @logtwo(i64 %8)
%10 = call i32 @pow(i32 2, i64 %9)
%11 = call i32 @pow(i32 2, i64 %9)
%12 = mul i32 %11, 3
%13 = add i32 %12, -3
%14 = sext i32 %13 to i64
%15 = load i64, i64* %1
%16 = icmp eq i64 %15, %14
br i1 %16, label %17, label %19
17: ; preds = %0
%18 = call i32 @putchar(i32 49)
br label %29
19: ; preds = %0
%20 = add nsw i64 %9, 1
%21 = call i32 @pow(i32 2, i64 %20)
%22 = mul i32 %21, 3
%23 = add i32 %22, -3
%24 = sext i32 %23 to i64
%25 = load i64, i64* %1
%26 = sub i64 1, %25
%27 = add i64 %26, %24
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 %27)
br label %29
29: ; preds = %19, %17
ret i32 0
}