#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 m, n, i, t, f;
  while (scanf("%d", &n) != -1) {
    if (!n)
      break;
    for (m = 2; 1; m++) {
      f = 1;
      t = n - 1;
      for (i = m; t > 1; i += m - 1) {
        i = ((i - 1) % t) + 1;
        if (i == 1)
          f = 0;
        t--;
      }
      if (f) {
        printf("%d\n", m);
        break;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1040 <scanf@plt>
cmp    $0xffffffff,%eax
je     11fe <main+0xde>
cmpl   $0x0,-0xc(%rbp)
jne    1157 <main+0x37>
jmp    11fe <main+0xde>
movl   $0x2,-0x8(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    116d <main+0x4d>
jmp    11f9 <main+0xd9>
movl   $0x1,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x1,-0x14(%rbp)
jle    11c8 <main+0xa8>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
cltd
idivl  -0x14(%rbp)
add    $0x1,%edx
mov    %edx,-0x10(%rbp)
cmpl   $0x1,-0x10(%rbp)
jne    11ae <main+0x8e>
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x1,%eax
add    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
jmp    1183 <main+0x63>
cmpl   $0x0,-0x18(%rbp)
je     11e6 <main+0xc6>
mov    -0x8(%rbp),%esi
lea    0xe27(%rip),%rdi
call   1030 <printf@plt>
jmp    11f9 <main+0xd9>
jmp    11eb <main+0xcb>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    115e <main+0x3e>
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    $0xffffffff,%eax
je     11e3 <main+0xc3>
mov    0x4(%rsp),%r9d
test   %r9d,%r9d
je     11e3 <main+0xc3>
lea    0xeac(%rip),%r14
lea    0xea2(%rip),%r15
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0x1(%r9),%r8d
mov    $0x2,%eax
nopl   0x0(%rax)
mov    %eax,%esi
cmp    $0x3,%r9d
jl     11c1 <main+0xa1>
mov    $0x1,%ebx
mov    %r8d,%edi
mov    %esi,%edx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    -0x2(%rdi),%ecx
add    $0xffffffff,%edx
mov    %edx,%eax
cltd
idiv   %ecx
test   %edx,%edx
cmove  %edx,%ebx
add    %esi,%edx
add    $0xffffffff,%edi
cmp    $0x3,%edi
ja     11a0 <main+0x80>
lea    0x1(%rsi),%eax
test   %ebx,%ebx
je     1180 <main+0x60>
mov    %r14,%rdi
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <scanf@plt>
cmp    $0xffffffff,%eax
je     11e3 <main+0xc3>
mov    0x4(%rsp),%r9d
test   %r9d,%r9d
jne    1170 <main+0x50>
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
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
br label %7
7: ; preds = %50, %0
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%9 = icmp ne i32 %8, -1
br i1 %9, label %10, label %51
10: ; preds = %7
%11 = load i32, i32* %3
%12 = icmp ne i32 %11, 0
br i1 %12, label %14, label %13
13: ; preds = %10
br label %51
14: ; preds = %10
store i32 2, i32* %2
br label %15
15: ; preds = %47, %14
br i1 true, label %16, label %50
16: ; preds = %15
store i32 1, i32* %6
%17 = load i32, i32* %3
%18 = sub nsw i32 %17, 1
store i32 %18, i32* %5
%19 = load i32, i32* %2
store i32 %19, i32* %4
br label %20
20: ; preds = %35, %16
%21 = load i32, i32* %5
%22 = icmp sgt i32 %21, 1
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = load i32, i32* %4
%25 = sub nsw i32 %24, 1
%26 = load i32, i32* %5
%27 = srem i32 %25, %26
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %32
31: ; preds = %23
store i32 0, i32* %6
br label %32
32: ; preds = %31, %23
%33 = load i32, i32* %5
%34 = add nsw i32 %33, -1
store i32 %34, i32* %5
br label %35
35: ; preds = %32
%36 = load i32, i32* %2
%37 = sub nsw i32 %36, 1
%38 = load i32, i32* %4
%39 = add nsw i32 %38, %37
store i32 %39, i32* %4
br label %20
40: ; preds = %20
%41 = load i32, i32* %6
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %40
%44 = load i32, i32* %2
%45 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %44)
br label %50
46: ; preds = %40
br label %47
47: ; preds = %46
%48 = load i32, i32* %2
%49 = add nsw i32 %48, 1
store i32 %49, i32* %2
br label %15
50: ; preds = %43, %15
br label %7
51: ; preds = %13, %7
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 ne i32 %3, -1
%5 = load i32, i32* %1
%6 = icmp ne i32 %5, 0
%7 = select i1 %4, i1 %6, i1 false
br i1 %7, label %8, label %34
8: ; preds = %0, %27
%9 = phi i32 [ %31, %27 ], [ %5, %0 ]
%10 = icmp sgt i32 %9, 2
br label %11
11: ; preds = %24, %8
%12 = phi i32 [ %26, %24 ], [ 2, %8 ]
br i1 %10, label %13, label %27
13: ; preds = %11, %13
%14 = phi i32 [ %17, %13 ], [ %9, %11 ]
%15 = phi i32 [ %21, %13 ], [ 1, %11 ]
%16 = phi i32 [ %22, %13 ], [ %12, %11 ]
%17 = add nsw i32 %14, -1
%18 = add nsw i32 %16, -1
%19 = srem i32 %18, %17
%20 = icmp eq i32 %19, 0
%21 = select i1 %20, i32 0, i32 %15
%22 = add i32 %19, %12
%23 = icmp ugt i32 %14, 3
br i1 %23, label %13, label %24
24: ; preds = %13
%25 = icmp eq i32 %21, 0
%26 = add nuw nsw i32 %12, 1
br i1 %25, label %11, label %27
27: ; preds = %11, %24
%28 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%29 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%30 = icmp ne i32 %29, -1
%31 = load i32, i32* %1
%32 = icmp ne i32 %31, 0
%33 = select i1 %30, i1 %32, i1 false
br i1 %33, label %8, label %34
34: ; preds = %27, %0
ret i32 0
}