Problem:


すごろく

 問題

JOI さんは一人ですごろく遊びをしている．
このすごろくには一直線上に N 個のマスがあり，それぞれ移動の指示が書かれている．スタート地点は 1 マス目であり，ゴールはNマス目である．
JOI さんはゴールするまで次を繰り返す．

サイコロを振って出た目の数だけ現在のマスから進み，そのマスの指示に従う．
指示に従って移動した先のマスの指示には従わない．

ちょうど N マス目に止まる時だけでなく，移動先が N マス目を超える場合もゴールとなる．

すごろくの盤面と， M 回分のサイコロの出る目が与えられたとき，サイコロを何回振ったところでゴールするかを出力するプログラムを作成せよ．

  入力

入力は複数のデータセットからなる．各データセットは以下の形式で与えられる．

各データセットは 1+N+M 行からなる．

入力の 1 行目には２つの整数 N,M （2 ≤ N ≤ 1000 ，1 ≤ M ≤ 1000 ）が空白を区切りとして書かれている．
N はすごろくのマス目の個数を， M は与えられるサイコロの目の個数を表す．

続く N 行には -999 以上 999 以下の整数が１つずつ書かれている．
1+i 行目 （ 1 ≤  i ≤ N ） の整数は，すごろくの i 番目のマスの指示を表す．
書かれている整数を X とする．
X=0 のときは「何もしない」を， X>0 のときは「 X マス進む」を，X

次の図は2つ目の入力例を表す．

上記問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
10 5
0
0
5
6
-3
8
1
8
-4
0
1
3
5
1
5
10 10
0
-1
-1
4
4
-5
0
1
-6
0
1
5
2
4
6
5
5
4
1
6
0 0


Sample output:
5
6

Submission status: Wrong Answer

Solution code:
n = -1

while n != 0:
    n,m = map(int,input().split())
    nn = [int(input()) for i in range(n)]
    mm = [int(input()) for i in range(m)]
    p = 0
    c = 0
    while p < n - 1:
        p += nn[p]
        p += mm[c]
        c += 1
    print(c)

Submission status: Wrong Answer

Solution code:
field=[]
x=0
n,m=map(int,input().split(" "))
for i in range(0,n):
	field.append(int(input()))

for i in range(0,m):
	y=int(input())
	if((n-1)<=x+y):
		break
	x+=field[x+y]+y

y=i+1
for n in range(i+1,m):
	x=input()
print(y)

Submission status: Wrong Answer

Solution code:
field=[]
x=0
y=0
i=0
n,m=map(int,input().split(" "))
for i in range(0,n):
	field.append(int(input()))

for i in range(0,m):
	y=int(input())
	if((n-1)<=x+y):
		break
	x+=field[x+y]+y

y=i+1
for n in range(i+1,m):
	x=input()
print(y)

Submission status: Wrong Answer

Solution code:
field=[]
x=0
i=0
n,m=map(int,input().split(" "))
for i in range(0,n):
	field.append(int(input()))

for i in range(0,m):
	y=int(input())
	if((n-1)<=x+y):
		break
	x+=field[x+y]+y

if(n==0):
	y=0
else:
	y=i+1

for n in range(i+1,m):
	x=input()
print(y)

Submission status: Wrong Answer

Solution code:
def sugoroku(list_cube, list_X, N):
    now_position = 0
    num = 0
    for cube in list_cube:
        num += 1
        now_position += cube
        if now_position < N:
            now_position += list_X[now_position]
        else:
            break

    print(num)


while True:
    N, M = map(int, input().split(' '))
    if N == M == 0:
        break
    list_X = [int(input()) for i in range(N)]
    list_cube = [int(input()) for i in range(M)]
    sugoroku(list_cube=list_cube, list_X=list_X, N=N)

Submission status: Wrong Answer

Solution code:
while True:
    N,M=map(int,raw_input().split())
    if N==M==0:break
    square=[input() for i in range(N)]
    roll=[input() for i in range(M)]
    plc,cnt=0,0
    for i in roll:
        plc+=i
        cnt+=1
        if plc>=N-1:
            print cnt
            break
        plc+=square[plc]

Submission status: Wrong Answer

Solution code:
while True:
    N,M=map(int,raw_input().split())
    if N==M==0:break
    square=[0]+[input() for i in range(N)]
    roll=[input() for i in range(M)]
    plc,cnt=1,0
    for i in roll:
        plc+=i
        cnt+=1
        if plc>=N:
            print cnt
            break
        plc+=square[plc]

Submission status: Wrong Answer

Solution code:
n,m=map(int,raw_input().split())
field=[int(raw_input()) for i in range(n)]
dice=[int(raw_input()) for i in range(m)]
now=0
for i in range(m):
	now+=dice[i]
	try:now+=field[now]
	except:pass
	if now>=n-1:
		print i+1
		break

Submission status: Accepted

Solution code:
while 1:
    n, m = map(int, raw_input().split())
    if n == 0: break
    board = [input() for i in range(n)]
    pos = 0
    dice = [input() for i in range(m)]
    for i in range(m):
        pos += dice[i]
        if pos >= n - 1:
            print i + 1
            break
        pos += board[pos]
        if pos >= n - 1:
            print i + 1
            break

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if not n:
        break
    stops = [int(input()) for _ in range(n)] + [0] * 5
    dices = [int(input()) for _ in range(m)]
    i, p = 1, 0
    g = n - 1
    for d in dices:
        p += d + stops[p + d]
        if p >= g:
            break
        i += 1
    print(i)

Submission status: Accepted

Solution code:
import sys
def line():return sys.stdin.readline().strip()
def cin():return sys.stdin.readline().strip().split()
while True:
    N,M = [int(i) for i in cin()]
    if N == 0:break
    X = [int(line()) for _ in range(N)]
    Y = [int(line()) for _ in range(M)]
    c = 1
    for i in range(M):
        c+=int(Y[i])
        if c < N and X[c - 1] != 0:
            c+=X[c - 1]
        if c >= N:
            print(i + 1)
            break

Submission status: Accepted

Solution code:
while 1:
    n,m=map(int,raw_input().split())
    if n==m==0:break
    info=[int(raw_input()) for _ in xrange(n)]
    sai=[int(raw_input()) for _ in xrange(m)]
    now=0
    cnt=0
    for i in xrange(m):
        cnt+=1
        now+=sai[i]
        if now>=n-1:
            print(cnt)
            break
        now+=info[now]
        if now>=n-1:
            print(cnt)
            break

Submission status: Accepted

Solution code:
while 1:
    n,m=map(int,input().split())
    if n==0:break
    c,now=1,0
    f=[int(input()) for _ in range(n)]
    d=[int(input()) for _ in range(m)]
    for i in d:
        now+= n if now+i>=n-1 else i+f[now+i]
        if now>=n:break
        c+=1
    print(c)

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n==0: break
    mapp = []
    for _ in range(n):
        mapp.append(int(input()))
    mas = 0
    ans = 0
    for dice in range(m):
        mas += int(input())
        if mas >= n-1 and ans == 0: ans = dice+1
        if mas < n-1 : mas += mapp[mas]
        if mas >= n-1 and ans == 0: ans = dice+1
    print(ans)

Submission status: Accepted

Solution code:
while True:
    N, M = map(int, input().split())
    if N == 0:
        break
    mas = [int(input()) for _ in range(N)]
    dice = [int(input()) for _ in range(M)]
    p = 0
    ans = 0
    for d in dice:
        p += d
        ans += 1
        if p >= N:
            break
        p += mas[p]
        if p >= N:
            break
    print(ans)

Submission status: Accepted

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

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




def move(pos, result):
    pos = pos + result
    try:
        if squares[pos] != 0:
            pos += squares[pos]
        return pos
    except:
        return float('inf')


squares = []
def main(args):
    global squares
    while True:
        N, M = map(int, input().split())
        if N == 0 and M == 0:
            break

        squares = [int(input()) for _ in range(N)]
        dice = [int(input()) for _ in range(M)]

        turn = 0
        pos = 0
        for d in dice:
            turn += 1
            pos = move(pos, d)
            if pos >= (N - 1):
                break
        print(turn)


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

Submission status: Accepted

Solution code:
while True:
    n,m=[int(s)for s in input().split(" ")]
    if n==m==0:break
    map_=[int(input())for i in range(n)]
    dice=[int(input())for i in range(m)]
    pos=0
    for i in range(m):
        pos+=dice[i]
        try:pos+=map_[pos]
        except:pass
        if pos>=n-1:
            print(i+1)
            break

Submission status: Accepted

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 S=[int(input())for _ in[0]*N]
 p=b=1
 for i in range(M):
  d=int(input())
  p+=d
  if N<=p:
   if b:print(-~i);b=0
   continue
  p+=S[~-p]
  if(N<=p)*b:print(-~i);b=0

Submission status: Accepted

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 k,p=1,0
 S=[int(input()) for _ in[0]*N]
 for d in[int(input())for _ in[0]*M]:
  p+=d if N<=p+d else d+S[p+d]
  if N<=p+1:break
  k+=1
 print(k)

Submission status: Accepted

Solution code:
def s():
 import sys
 r=sys.stdin.readline
 for e in iter(r,'0 0\n'):
  N,M=map(int,e.split())
  k,p=1,0
  S=[int(r()) for _ in[0]*N]
  for d in[int(r())for _ in[0]*M]:
   p+=d if N<=p+d else d+S[p+d]
   if N<=p+1:break
   k+=1
  print(k)
if'__main__'==__name__:s()

Submission status: Accepted

Solution code:
import sys
r=sys.stdin.readline
for e in iter(r,'0 0\n'):
 N,M=map(int,e.split())
 k,p=1,0
 S=[int(r()) for _ in[0]*N]
 for d in[int(r())for _ in[0]*M]:
  p+=d if N<=p+d else d+S[p+d]
  if N<=p+1:break
  k+=1
 print(k)

Submission status: Accepted

Solution code:
import sys
r=sys.stdin.readline
for e in iter(r,'0 0\n'):
 N,M=map(int,e.split())
 k,p=1,0
 S=[int(r()) for _ in[0]*N]
 for d in[int(r())for _ in[0]*M]:
  p+=d;
  if p<N:p+=S[p]
  if N<=p+1:break
  k+=1
 print(k)

Submission status: Accepted

Solution code:
import sys
r=sys.stdin.readline
for e in iter(r,'0 0\n'):
 N,M=map(int,e.split())
 k,p=1,0
 S=[int(r())for _ in[0]*N]
 for d in[int(r())for _ in[0]*M]:
  p+=d;
  if p<N:p+=S[p]
  if N<=p+1:break
  k+=1
 print(k)

Submission status: Accepted

Solution code:
while True:
  n, m = map(int, input().split())
  if not n:
    break
  smap = [int(input()) for _ in range(n)]
  ind = 0
  cnt = 0
  flag = True
  for _ in range(m):
    ind += int(input())
    if flag:
      cnt += 1
      if ind >= n - 1:
        print(cnt)
        flag = False
      else:
        ind += smap[ind]
        if ind >= n:
          print(cnt)
          flag = False

Submission status: Accepted

Solution code:
def solve():
  while True:
    n, m = map(int, input().split())
    if not n:
      break
    smap = [int(input()) for _ in range(n)]
    ind = 0
    cnt = 0
    flag = True
    for _ in range(m):
      ind += int(input())
      if flag:
        cnt += 1
        if ind >= n - 1:
          print(cnt)
          flag = False
        else:
          ind += smap[ind]
          if ind >= n:
            print(cnt)
            flag = False
  
solve()

Submission status: Accepted

Solution code:
# AOJ 0544: Sugoroku
# Python3 2018.7.1 bal4u

import sys
from sys import stdin
input = stdin.readline

while True:
	n, m = map(int, input().split())
	if n == 0: break
	s = [0] + [int(input()) for i in range(n)]
	x = [0] + [int(input()) for i in range(m)]
	p = i = 1
	while p < n:
		p += x[i]
		i += 1
		if p < n: p += s[p]
	print(i-1)

Submission status: Accepted

Solution code:
while True:
    N,M=map(int,raw_input().split())
    if N==M==0:break
    square=[0]+[input() for i in range(N)]
    roll=[input() for i in range(M)]
    plc,cnt=1,0
    for i in roll:
        plc+=i
        cnt+=1
        if plc<N:
            plc+=square[plc]
        if plc>=N:
            print cnt
            break

Submission status: Accepted

Solution code:
mass = [0] * 2000
num = [0] * 1000
while True:
  N, M = map(int, raw_input().split())
  if N == 0 and M == 0:
    break
  p = 0
  for i in range(N):
    mass[i] = input()
  for i in range(M):
    num[i] = input()

  for i in range(M):
    p += num[i];
    p += mass[p]
    if p >= N - 1:
      print i + 1
      break

Submission status: Accepted

Solution code:
while 1:
    n, m = [int(i) for i in raw_input().split(" ")]
    if not n and not m:
        break
    dic = []
    now_pos = 0
    for i in range(n):
        dic.append(int(raw_input()))
    
    for i in range(m):
        now_pos += int(raw_input())
        if now_pos >= n-1:
            for j in range(m-i-1):
                raw_input()
            print i+1
            break
        now_pos += dic[now_pos]
        if now_pos >= n-1:
             for j in range(m-i-1):
                 raw_input()
             print i+1
             break

Submission status: Accepted

Solution code:
while 1:
	n,m=map(int,raw_input().split())
	if n==0:break
	field=[int(raw_input()) for i in range(n)]
	dice=[int(raw_input()) for i in range(m)]
	now=0
	for i in range(m):
		now+=dice[i]
		try:now+=field[now]
		except:pass
		if now>=n-1:
			print i+1
			break

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, raw_input().split())
    if n==0 and m==0:
        break
    b = [input() for i in xrange(n)]
    d = [input() for i in xrange(m)]
    p = 0; ans = 0;
    for i in xrange(m):
        p += d[i]
        if p>=n-1 or p+b[p]>=n-1:
            ans = i+1
            break
        p += b[p]
    print ans

Submission status: Accepted

Solution code:
while True:
    n,m = map(int,input().split())
    if n == m == 0:
        break
    N = [int(input()) for i in range(n)]
    M = [int(input()) for i in range(m)]
    x = 1
    for i in range(m):
        x += M[i]
        if n <= x:
            print(i+1)
            break
        else:
            x +=  N[x-1]
            if n <= x:
                print(i+1)
                break

Submission status: Accepted

Solution code:
while True :
    N, M = map(int, input().split())
    if N == 0 and M == 0 :
        break
    
    Ins = list(int(input()) for i in range(N))
    Sai = list(int(input()) for i in range(M))
    
    ans = 0
    Now = 0
    i = 0
    while True :
        if Now >= N-1 :
            break
        
        ans += 1
        Now += Sai[i]
        if Now >= N-1 :
            break
        Now += Ins[Now]
        i += 1
    print(ans)

Submission status: Accepted

Solution code:
while True:
	N, M = map(int, input().split())
	if N==0 and M==0: break
	n = [int(input()) for _ in range(N)]
	m = [int(input()) for _ in range(M)]
	j = 0  #現在のマス目
	for i in range(M):
		j += m[i]
		if j>=N: break
		j += n[j]
		if j>=N: break
	print(i+1)

Submission status: Accepted

Solution code:
while True:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    a = []
    b = []
    for i in range(n):
        a.append(int(input()))
    masu = 1
    cnt = 0

    for i in range(m):
        b.append(int(input()))

    for i in range(m):
        cnt += 1
        masu += b[i]
        if masu >= n:
            print(cnt)
            break
        masu += a[masu-1]
        if masu >= n:
            print(cnt)
            break

Submission status: Accepted

Solution code:
def main():
    while True:
        N, M = map(int, input().split())
        if not N:
            return
        order = [int(input()) for _ in range(N)]
        draw = [int(input()) for _ in range(M)]
        p = 0
        ans = 0
        for i in range(M):
            p += draw[i]
            if p >= N - 1:
                ans = i + 1
                break
            p += order[p]
            if p >= N - 1:
                ans = i + 1
                break

        print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
while 1:
    n, m = map(int, input().split())
    if n == 0:
        break

    masu = []
    for _ in range(n):
        x = int(input())
        masu.append(x)

    dice = []
    for _ in range(m):
        x = int(input())
        dice.append(x)

    p = 0
    for i, d in enumerate(dice):
        p += d
        if p >= n:
            break
        p += masu[p]
        if p >= n:
            break

    print(i+1)

Submission status: Runtime Error

Solution code:
n = -1

while n != 0:
    n,m = map(int,input().split())
    nn = [int(input()) for i in range(n)]
    mm = [int(input()) for i in range(m)]
    p = 0
    c = 0
    while p <= n - 1:
        p += nn[p]
        p += mm[c]
        c += 1
    print(c)

Submission status: Runtime Error

Solution code:
import sys
def line():return sys.stdin.readline().strip()
def cin():return sys.stdin.readline().strip().split()
while True:
    N,M = [int(i) for i in cin()]
    if N == 0:break
    X = [int(line()) for _ in range(N)]
    c = 1
    for i in range(M):
        c+=int(line())
        if c < N and X[c - 1] != 0:
            c+=X[c - 1]
        if c >= N:
            print(i + 1)
            break

Submission status: Runtime Error

Solution code:
def sugoroku(list_cube, list_X, N):
    now_position = 0
    num = 0
    for cube in list_cube:
        num += 1
        now_position += cube
        if now_position <= N:
            now_position += list_X[now_position]
        else:
            break

    print(num)


while True:
    N, M = map(int, input().split(' '))
    if N == M == 0:
        break
    list_X = [int(input()) for i in range(N)]
    list_cube = [int(input()) for i in range(M)]
    sugoroku(list_cube=list_cube, list_X=list_X, N=N)

Submission status: Runtime Error

Solution code:
nums = []


def sugoroku(list_cube, list_X, N):
    now_position = 0
    num = 0
    for cube in list_cube:
        num += 1
        now_position += cube
        if now_position <= N:
            now_position += list_X[now_position]
        else:
            break
    nums.append(num)


while True:
    N, M = map(int, input().split(' '))
    if N == M == 0:
        break
    list_X = [int(input()) for i in range(N)]
    list_cube = [int(input()) for i in range(M)]
    sugoroku(list_cube=list_cube, list_X=list_X, N=N)


for i in nums:
    print(i)

Submission status: Runtime Error

Solution code:
nums = []


def sugoroku(list_cube, list_X, N):
    now_position = 0
    num = 0
    while now_position < N:
        now_position += list_cube[num]
        now_position += list_X[now_position]
        num += 1
    print(num)


while True:
    N, M = map(int, input().split(' '))
    if N == M == 0:
        break
    list_X = [int(input()) for i in range(N)]
    list_cube = [int(input()) for i in range(M)]
    sugoroku(list_cube=list_cube, list_X=list_X, N=N)

Submission status: Runtime Error

Solution code:
nums = []


def sugoroku(list_cube, list_X, N):
    now_position = 0
    num = 0
    while now_position < N:
        now_position += list_cube[num]
        now_position += list_X[now_position]
        num += 1
    nums.append(num)


while True:
    N, M = map(int, input().split(' '))
    if N == M == 0:
        break
    list_X = [int(input()) for i in range(N)]
    list_cube = [int(input()) for i in range(M)]
    sugoroku(list_cube=list_cube, list_X=list_X, N=N)


for i in nums:
    print(i)

Submission status: Runtime Error

Solution code:
while True:
    N, M = map(int, input().split())
    if n == 0:
        break
    mapp = [int(input()) for _ in range(N)]
    mas = 0
    ans = 0
    for dice in range(M):
        mas += int(input())
        if mas >= n - 1 and ans == 0:
            ans = dice + 1
        
        if mas < n - 1:
            lmas += mapp[ans]
        
        if mas >= n - 1 and ans == 0: ans = dice + 1
    print(ans)

Submission status: Runtime Error

Solution code:
while True:
    N, M = map(int, input().split())
    if N == 0:
        break
    mapp = [int(input()) for _ in range(N)]
    mas = 0
    ans = 0
    for dice in range(M):
        mas += int(input())
        if mas >= n - 1 and ans == 0:
            ans = dice + 1

        if mas < n - 1:
            lmas += mapp[ans]

        if mas >= n - 1 and ans == 0: ans = dice + 1
    print(ans)

Submission status: Runtime Error

Solution code:
while True:
    N, M = map(int, input().split())
    if N == 0:
        break
    mapp = [int(input()) for _ in range(N)]
    mas = 0
    ans = 0
    for dice in range(M):
        mas += int(input())
        if mas >= n - 1 and ans == 0:
            ans = dice + 1

        if mas < n - 1:
            lmas += mapp[ans]

        if mas >= n - 1 and ans == 0:
            ans = dice + 1
    print(ans)

Submission status: Runtime Error

Solution code:
while True:
    N, M = map(int, input().split())
    if N == 0:
        break
    maps = [int(input()) for _ in range(N)]
    mas = 0
    ans = 0
    for dice in range(M):
        mas += int(input())
        if mas >= n - 1 and ans == 0:
            ans = dice + 1

        if mas < n - 1:
            mas += maps[ans]

        if mas >= n - 1 and ans == 0:
            ans = dice + 1
    print(ans)

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 S=[int(input())for _ in[0]*N]
 print(S)
 p=b=1
 for i in range(M):
  d=int(input())
  p+=d
  p+=S[~-p]
  print('pos', p)
  if(N<=p)*b:print(-~i);b=0

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 S=[int(input())for _ in[0]*N]
 print(S)
 p=b=1
 for i in range(M):
  d=int(input())
  p+=d
  p+=S[~-p]
  if(N<=p)*b:print(-~i);b=0

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 S=[int(input())for _ in[0]*N]
 p=b=1
 for i in range(M):
  d=int(input())
  p+=d
  p+=S[~-p]
  if(N<=p)*b:print(-~i);b=0

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 k=p=1
 S=[int(input()) for _ in[0]*N]
 for d in[int(input())for _ in[0]*M]:
  p+=n if n-1<=p+i else i+S[p+i]
  if n<=p:break
  k+=1
 print(k)

Submission status: Runtime Error

Solution code:
for e in iter(input,'0 0'):
 N,M=map(int,e.split())
 k,p=1,0
 S=[int(input()) for _ in[0]*N]
 for d in[int(input())for _ in[0]*M]:
  p+=d if N<p+d else d+S[p+d]
  if N<=p+1:break
  k+=1
 print(k)

Submission status: Runtime Error

Solution code:
import sys
r=sys.stdin.readline
def s():
 for e in iter(r,'0 0'):
  N,M=map(int,e.split())
  k,p=1,0
  S=[int(r()) for _ in[0]*N]
  for d in[int(r())for _ in[0]*M]:
   p+=d if N<=p+d else d+S[p+d]
   if N<=p+1:break
   k+=1
  print(k)
if'__main__'==__name__:solve()

Submission status: Runtime Error

Solution code:
import sys
r=sys.stdin.readline
def s():
 for e in iter(r,'0 0'):
  N,M=map(int,e.split())
  k,p=1,0
  S=[int(r()) for _ in[0]*N]
  for d in[int(r())for _ in[0]*M]:
   p+=d if N<=p+d else d+S[p+d]
   if N<=p+1:break
   k+=1
  print(k)
if'__main__'==__name__:s()

Submission status: Runtime Error

Solution code:
def s():
 import sys
 r=sys.stdin.readline
 for e in iter(r,'0 0'):
  N,M=map(int,e.split())
  k,p=1,0
  S=[int(r()) for _ in[0]*N]
  for d in[int(r())for _ in[0]*M]:
   p+=d if N<=p+d else d+S[p+d]
   if N<=p+1:break
   k+=1
  print(k)
if'__main__'==__name__:s()

Submission status: Runtime Error

Solution code:
import sys
r=sys.stdin.readline
for e in iter(r,'0 0\n'):
 N,M=map(int,e.split())
 k,p=1,0
 S=[int(r()) for _ in[0]*N]
 for d in[int(r())for _ in[0]*M]:
  p+=d;
  if N<=p:p+=S[p]
  if N<=p+1:break
  k+=1
 print(k)

Submission status: Runtime Error

Solution code:
mass = [0] * 1000
num = [0] * 1000
while True:
  N, M = map(int, raw_input().split())
  if N == 0 and M == 0:
    break
  p = 0
  for i in range(N):
    mass[i] = input()
  for i in range(M):
    num[i] = input()

  for i in range(M):
    p += num[i];
    p += mass[p]
    if p >= N - 1:
      print i + 1
      break

Submission status: Runtime Error

Solution code:
while True:
    n, m = map(int, raw_input().split())
    if n==0 and m==0:
        break
    b = [input() for i in xrange(n)]
    p = 0; ans = 0;
    for i in xrange(m):
        d = input()
        p += d
        if p>=n-1 or p+b[p]>=n-1:
            ans = i+1
            break
        p += b[p]
    print ans