#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 /*<<< orphan*/ scanf(char *, int *, ...);
int main(void) {
  int cost, income[12], expense[12], savings, i, flag;
  while (1) {
    scanf("%d", &cost);
    if (cost == 0)
      break;
    savings = 0, flag = 0;
    for (i = 0; i < 12; i++) {
      scanf("%d %d", &income[i], &expense[i]);
      savings += income[i] - expense[i];
      if (flag == 0 && savings >= cost)
        flag = i + 1;
    }
    if (flag == 0) {
      printf("NA\n");
    } else {
      printf("%d\n", flag);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeca(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1153 <main+0x33>
jmp    1219 <main+0xf9>
movl   $0x0,-0x74(%rbp)
movl   $0x0,-0x7c(%rbp)
movl   $0x0,-0x78(%rbp)
cmpl   $0xc,-0x78(%rbp)
jge    11e6 <main+0xc6>
movslq -0x78(%rbp),%rax
lea    -0x40(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
movslq -0x78(%rbp),%rax
lea    -0x70(%rbp),%rdx
shl    $0x2,%rax
add    %rax,%rdx
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
movslq -0x78(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%eax
movslq -0x78(%rbp),%rcx
sub    -0x70(%rbp,%rcx,4),%eax
add    -0x74(%rbp),%eax
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jne    11d3 <main+0xb3>
mov    -0x74(%rbp),%eax
cmp    -0x8(%rbp),%eax
jl     11d3 <main+0xb3>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x7c(%rbp)
jmp    11d8 <main+0xb8>
mov    -0x78(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x78(%rbp)
jmp    1168 <main+0x48>
cmpl   $0x0,-0x7c(%rbp)
jne    1203 <main+0xe3>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1214 <main+0xf4>
mov    -0x7c(%rbp),%esi
lea    0xdfd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1132 <main+0x12>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
lea    0xebe(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
je     1216 <main+0xe6>
lea    0xe9d(%rip),%rbx
lea    0xc(%rsp),%r12
xor    %r14d,%r14d
xor    %ebp,%ebp
xor    %eax,%eax
jmp    11bd <main+0x8d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %ebp,%ebp
je     1208 <main+0xd8>
lea    0xe77(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %r14d,%r14d
lea    0xe61(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1050 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
mov    $0x0,%ebp
mov    $0x0,%eax
je     1216 <main+0xe6>
mov    %eax,%r15d
lea    (%rsp,%r14,4),%rsi
add    $0x40,%rsi
lea    (%rsp,%r14,4),%rdx
add    $0x10,%rdx
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <scanf@plt>
mov    0x40(%rsp,%r14,4),%eax
sub    0x10(%rsp,%r14,4),%eax
add    %r15d,%eax
add    $0x1,%r14
cmp    0xc(%rsp),%eax
jl     11f7 <main+0xc7>
mov    %r14d,%r13d
test   %ebp,%ebp
cmove  %r13d,%ebp
cmp    $0xc,%r14
jne    11bd <main+0x8d>
jmp    1180 <main+0x50>
lea    0xdfb(%rip),%rdi
call   1030 <puts@plt>
jmp    1198 <main+0x68>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [4 x i8] c"NA\0A\00"
@.str.3 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [12 x i32]
%4 = alloca [12 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
br label %8
8: ; preds = %0, %57
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%10 = load i32, i32* %2
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %8
br label %58
13: ; preds = %8
store i32 0, i32* %5
store i32 0, i32* %7
store i32 0, i32* %6
br label %14
14: ; preds = %46, %13
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 12
br i1 %16, label %17, label %49
17: ; preds = %14
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 %19
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [12 x i32], [12 x i32]* %4, i64 0, i64 %22
%24 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %20, i32* %23)
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 %26
%28 = load i32, i32* %27
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [12 x i32], [12 x i32]* %4, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = sub nsw i32 %28, %32
%34 = load i32, i32* %5
%35 = add nsw i32 %34, %33
store i32 %35, i32* %5
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %17
%39 = load i32, i32* %5
%40 = load i32, i32* %2
%41 = icmp sge i32 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %6
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %45
45: ; preds = %42, %38, %17
br label %46
46: ; preds = %45
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %14
49: ; preds = %14
%50 = load i32, i32* %7
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
br label %57
54: ; preds = %49
%55 = load i32, i32* %7
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %54, %52
br label %8
58: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.3 = [4 x i8] c"%d\0A\00"
@str = [3 x i8] c"NA\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i32]
%3 = alloca [12 x i32]
%4 = bitcast i32* %1 to i8*
%5 = bitcast [12 x i32]* %2 to i8*
%6 = bitcast [12 x i32]* %3 to i8*
%7 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%8 = load i32, i32* %1
%9 = icmp eq i32 %8, 0
br i1 %9, label %43, label %10
10: ; preds = %0, %29
%11 = phi i64 [ %30, %29 ], [ 0, %0 ]
%12 = phi i32 [ %31, %29 ], [ 0, %0 ]
%13 = phi i32 [ %32, %29 ], [ 0, %0 ]
%14 = getelementptr inbounds [12 x i32], [12 x i32]* %2, i64 0, i64 %11
%15 = getelementptr inbounds [12 x i32], [12 x i32]* %3, i64 0, i64 %11
%16 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %14, i32* %15)
%17 = load i32, i32* %14
%18 = load i32, i32* %15
%19 = sub nsw i32 %17, %18
%20 = add nsw i32 %19, %13
%21 = icmp eq i32 %12, 0
%22 = load i32, i32* %1
%23 = icmp slt i32 %20, %22
%24 = add nuw nsw i64 %11, 1
%25 = trunc i64 %24 to i32
%26 = select i1 %23, i32 0, i32 %25
%27 = select i1 %21, i32 %26, i32 %12
%28 = icmp eq i64 %24, 12
br i1 %28, label %33, label %29
29: ; preds = %10, %39
%30 = phi i64 [ %24, %10 ], [ 0, %39 ]
%31 = phi i32 [ %27, %10 ], [ 0, %39 ]
%32 = phi i32 [ %20, %10 ], [ 0, %39 ]
br label %10
33: ; preds = %10
%34 = icmp eq i32 %27, 0
br i1 %34, label %35, label %37
35: ; preds = %33
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @str, i64 0, i64 0))
br label %39
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %27)
br label %39
39: ; preds = %37, %35
%40 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%41 = load i32, i32* %1
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %29
43: ; preds = %39, %0
ret i32 0
}