Problem:


旅行はいつ?

  あなたは友人と旅行に行きたいと考えています。ところが、浪費癖のある友人はなかなか旅行費用を貯めることができません。友人が今の生活を続けていると、旅行に行くのはいつになってしまうか分かりません。そこで、早く旅行に行きたいあなたは、友人が計画的に貯蓄することを助けるプログラムを作成することにしました。

  友人のある月のお小遣いを M 円、その月に使うお金を N 円とすると、その月は (M - N) 円貯蓄されます。毎月の収支情報 M 、 N を入力とし、貯蓄額が旅行費用 L に達するのにかかる月数を出力するプログラムを作成してください。ただし、12 ヶ月を過ぎても貯蓄額が旅行費用に達しなかった場合はNA と出力してください。

Input

複数のデータセットの並びが入力として与えられます。     入力の終わりはゼロひとつの行で示されます。
各データセットは以下の形式で与えられます。

L
M1 N1
M2 N2
:
M12 N12

1 行目に旅行費用 L (1 ≤ L ≤ 1000000, 整数) が与えられます。続く 12 行に、i 月目の収支情報 Mi, Ni (0 ≤ Mi, Ni ≤ 100000, Ni ≤ Mi, 整数) が与えられます。

データセットの数は 1000 を超えません。

Output

入力データセットごとに、貯蓄額が旅行費用に達するのにかかる月数を１行に出力します。

Sample Input

10000
5000 3150
5000 5000
0 0
5000 1050
5000 3980
5000 210
5000 5000
5000 5000
0 0
5000 2100
5000 2100
5000 2100
29170
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 70831
0

Output for the Sample Input

6
NA



Sample input:
10000
5000 3150
5000 5000
0 0
5000 1050
5000 3980
5000 210
5000 5000
5000 5000
0 0
5000 2100
5000 2100
5000 2100
29170
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 100000
100000 70831
0


Sample output:
6
NA

Submission status: Wrong Answer

Solution code:
def alogrithm():
    while True:
        budget = int(input())
        if budget == 0:
            break

        total = 0
        months = 0
        for _ in range(12):
            income, outcome = map(int, input().split())
            total += income - outcome
            months += 1 if income > outcome else 0

        if total < budget:
            print('NA')
        else:
            print(months)


def main():
    alogrithm()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
while True:
    L= int(input())
    if L== 0: break
    mn= [list(map(int, input().split())) for _ in range(12)]
    ans=c=1
    b= True
    for m, n in mn:
        ans+= m-n
        if ans>= L:
            print(c)
            b= False
            break
        c+= 1
    if b: print("NA")

Submission status: Wrong Answer

Solution code:
while 1:
    l = input()
    if l==0: break
    c = []; m = 0;
    for i in xrange(12):
        c += map(int, raw_input().split())
    for i in xrange(0,12,2):
        m += c[i]-c[i+1]
        if m>=l:
            print i/2+1; break;
    if m<l:
        print "NA"

Submission status: Wrong Answer

Solution code:
from bisect import bisect_right as br
while True:
    n = input()
    if n == 0:
        break
    d = map(lambda (x,y):x-y, [map(int, raw_input().split()) for _ in xrange(12)])
    print (range(1,13)+["NA"])[br([sum(d[:i+1]) for i in xrange(12)], n)]

Submission status: Accepted

Solution code:
from sys import stdin
readline = stdin.readline


from itertools import accumulate

while True:
    trip_cost = int(readline())
    if trip_cost == 0:
        break
    saving = []
    for _ in range(12):
        income, outgo = map(int, readline().split())
        saving.append(income - outgo)

    for i, accumulative_saving in enumerate(accumulate(saving)):
        if trip_cost <= accumulative_saving:
            print(i + 1)
            break
    else:
        print('NA')

Submission status: Accepted

Solution code:
while True:
    L = int(raw_input())
    if L == 0: break
    ans = 0
    for i in range(1, 13):
        M, N = map(int, raw_input().split())
        L -= (M-N)
        if not ans and L <= 0:
            ans = i
    else:
        if ans:
            print ans
        else:
            print 'NA'

Submission status: Accepted

Solution code:

def algorithm():
    while True:
        budget = int(input())
        if budget == 0:
            break

        total, months = 0, 0
        for _ in range(12):
            income, outcome = map(int, input().split())
            total += income - outcome
            if total >= budget and months == 0:
                months = _ + 1

        print('NA' if total < budget else months)


def main():
    algorithm()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while 1:
    n=int(input())
    if n==0: break
    for i in range(12):
        a,b=map(int,input().split())
        if n<=0: continue
        c=i+1
        n-=a-b
    if n>0:print("NA")
    else:print(c)

Submission status: Accepted

Solution code:
while True:
    L= int(input())
    if L== 0: break
    mn= [list(map(int, input().split())) for _ in range(12)]
    ans=c=0
    b= True
    for m, n in mn:
        ans+= m-n
        c+= 1
        if ans>= L:
            print(c)
            b= False
            break
    if b: print("NA")

Submission status: Accepted

Solution code:
# Aizu Problem 0206: The Next Trip

import sys, math, os

# read input:
PYDEV = os.environ.get('PYDEV')
if PYDEV=="True":
    sys.stdin = open("sample-input.txt", "rt")


def next_trip(L, cashflow):
    saved = 0
    month = 0
    for m, n in cashflow:
        month += 1
        saved += (m - n)
        if saved >= L:
            return month
    return "NA"

    
while True:
    L = int(input())
    if L == 0:
        break
    cashflow = [[int(_) for _ in input().split()] for __ in range(12)]
    print(next_trip(L, cashflow))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0206

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        L = int(input())
        if L == 0:
            break
        ans = 'NA'
        for i in range(1, 12+1):
            M, N = map(int, input().split())
            if ans == 'NA':
                L -= (M - N)
                if L <= 0:
                    ans = i
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0206

"""
import sys
from sys import stdin
input = stdin.readline


def main(args):
    while True:
        L = int(input())
        if L == 0:
            break
        ans = 'NA'
        for i in range(1, 12+1):
            if ans == 'NA':
                M, N = map(int, input().split())
                L -= (M - N)
                if L <= 0:
                    ans = i
            else:
                _ = input()
        print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

Solution code:
def jug(l,income):
    assert len(income)==12, "income strange"
    s = 0 
    for i in range(len(income)):
        inc = income[i]
        s += inc[0] - inc[1]
        if s >= l:
            return(str(i+1))
    return("NA")

while True:
    income=[]
    l=int(input())
    if l==0:
        break
    for i in range(12):
        m,n = list(map(int,input().strip().split()))
        income.append((m,n))
    print(jug(l,income))

Submission status: Accepted

Solution code:
# AOJ 0206: Next Trip
# Python3 2018.6.25 bal4u

while True:
	L = int(input())
	if L == 0: break
	s, k = 0, 0
	for i in range(1, 13):
		m, n = map(int, input().split())
		s += m-n
		if s >= L and k == 0: k = i
	print(k if k > 0 else "NA")

Submission status: Accepted

Solution code:
while True:
  l = int(input())
  if l == 0:
    break
  acc = 0
  ans = 0
  for i in range(1, 13):
    m, n = map(int, input().split())
    acc += (m - n)
    if not ans and acc >= l:
      ans = i
  if not ans:
    print("NA")
  else:
    print(ans)

Submission status: Accepted

Solution code:
while True:
  y,o = 0,0
  x  = int(input())
  if x == 0:
    break
  else:
    for q in range(12):
      m = map(int,raw_input().split())
      m,n = m[0],m[1]
      y += m-n
      if y >= x:
        o += 1

    if y >= x: print 12-o+1
    else:
      print "NA"

Submission status: Accepted

Solution code:
while True:
  y,o = 0,0
  x  = int(input())
  if x == 0:
    break
  else:
    for q in range(12):
      m,n= map(int,raw_input().split())
      y += m-n
      if y >= x:
        o += 1
 
    if y >= x: print 12-o+1
    else:
      print "NA"

Submission status: Accepted

Solution code:
while True:
    L=input()
    if L==0:break
    T=[]
    for i in range(1,13):
        M,N=map(int,raw_input().split())
        T.append(M-N)
    for i in range(1,13):
        L-=T[i-1]
        if L<=0:
            print i
            break
    else:
        print "NA"

Submission status: Accepted

Solution code:
while 1:
    L=input()
    if L<1:break
    a,t=0,0
    for i in range (12):
        k=map(int,raw_input().split())
        t+=int(k[0]-k[1])
        if L<=t:a+=1
    print 12-a+1 if a else "NA"

Submission status: Accepted

Solution code:
while 1:
    L=input()
    if L<1:break
    a,t=0,0
    for i in range(12):
        k=map(int,raw_input().split())
        t+=k[0]-k[1]
        if L<=t:a+=1
    print 12-a+1 if a else "NA"

Submission status: Accepted

Solution code:
while 1:
    l = input()
    if l==0: break
    c = []; m = 0;
    for i in xrange(12):
        c += map(int, raw_input().split())
    for i in xrange(0,24,2):
        m += c[i]-c[i+1]
        if m>=l:
            print i/2+1; break;
    if m<l:
        print "NA"

Submission status: Accepted

Solution code:
from bisect import bisect_left as bl
while True:
    n = input()
    if n == 0:
        break
    d = map(lambda (x,y):x-y, [map(int, raw_input().split()) for _ in xrange(12)])
    print (range(1,13)+["NA"])[bl([sum(d[:i+1]) for i in xrange(12)], n)]

Submission status: Accepted

Solution code:
while True:
	L = int(raw_input())
	if L == 0: break
	M = [map(int, raw_input().split()) for i in range(12)]
	for i in range(12):
		L -= M[i][0] - M[i][1]
		if L <= 0:
			print i+1
			break
	else:
		print "NA"

Submission status: Accepted

Solution code:
while True:
    
    #旅行費用を入力し0なら終わる
    num = int(input())
    if num == 0:break
    num_list = []
    
    #小遣いの差額をリストに追加していく
    for i in range(0,12):
        a,b = map(int,input().split())
        num_list.append(a - b)
        
    #12ヶ月ためても無理な場合は"NA"を出力する
    if num > sum(num_list):
        print("NA")
        continue
    
    #毎月減らしていき達成したらループを抜ける
    for i in range(0,12):
        num -= num_list[i]
        if num <= 0:break
    
    #リストに合わせるため変数を1減らしていたため1足して出力する
    print(i + 1)

Submission status: Accepted

Solution code:
while True :
    l = int(input())
    if l == 0 :
        break
    
    money = 0
    END = False
    for i in range(12) :
        m, n = map(int, input().split())
        money += m - n
        
        if money >= l and END == False :
            month = i + 1
            END = True
    
    if END == False :
        print("NA")
    else :
        print(month)

Submission status: Accepted

Solution code:
while True:
    z = True
    a = int(input())
    if a == 0:
        break
    for i in range(12):
        b,c = map(int,input().split())
        a -= b-c
        if a <= 0 and z:
            print(i+1)
            z = False
    if a > 0:
        print("NA")

Submission status: Accepted

Solution code:
while 1:
    l = int(input())
    if l == 0:
        break;
    s = 0
    for i in range(1,13):
        m, n = [int(_) for _ in input().split()]
        l -= m - n
        if s == 0 and l <= 0:
            s = i
    print(s) if s != 0 else print('NA')

Submission status: Accepted

Solution code:
while True:
    v = int(input())
    if v == 0:
        break
    
    c = 0
    f = 0
    for i in range(1,13):
        m, n = [int(x) for x in input().split()]
        c += m - n
        if f == 0 and c >= v:
            f = i
    
    if f > 0:
        print(f)
    else:
        print("NA")

Submission status: Accepted

Solution code:
while True:
    L=int(input())
    if L==0:
        break
    S=0
    B=0
    for i in range(1,13):
        M,N=map(int,input().split())
        A=M-N
        S+=A
        if S>=L and B==0:
            B=i
    if B>0:
        print(B)
    else:
        print("NA")

Submission status: Accepted

Solution code:
while True:
    L = int(input())
    if L == 0:
        break
    
    flag = True
    ans = -1
    d = 0
    for i in range(12):
        M,N = [int(i) for i in input().split()]
        d = d + M - N
        if d >= L and flag:
            ans = i+1
            flag = False
    
    if ans >= 0:
        print(ans)
    else:
        print("NA")

Submission status: Accepted

Solution code:
while True:
  L = int(input())
  if not L: break
  for i in range(1, 13):
    m, n = map(int, input().split())
    if L <= 0: continue
    c = i
    L -= m - n
  if L > 0:
    print('NA')
  else:
    print(c)

Submission status: Accepted

Solution code:
while 1:
    l = int(input())
    if l == 0:
        break

    money = []
    for _ in range(12):
        m, n = map(int, input().split())
        money.append(m-n)

    total = 0
    for i, p in enumerate(money):
        total += p
        if total >= l:
            print(i+1)
            break
    else:
        print("NA")

Submission status: Accepted

Solution code:
import sys

while True:
    L = int(input())
    if L == 0:
        break
    savings = 0
    a = [map(int, sys.stdin.readline().split()) for _ in [0]*12]
    for i, (M, N) in enumerate(a, start=1):
        savings += M - N
        if savings >= L:
            print(i)
            break
    else:
        print("NA")

Submission status: Accepted

Solution code:
while(True):
    L = int(input())
    if not L:
        break
    a = 0
    b = 0
    c = 0
    for _ in range(12):
        a += 1
        M,N = map(int, input().split())
        b += M-N
        if not c and b >= L:
            c = a
    print(c if c else "NA")

Submission status: Runtime Error

Solution code:
while True:
    L=input()
    if L=0:break
    flag=True
    for i in range(1,13):
        M,N=map(int,raw_input().split())
        L-=(M-N)
        if L<=0 and flag:
            print i
            flag=False
    else:
        print "NA"