# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem in Contest #4 – Junior Division
# Author :  Mihir Sood
# Language: Python 3.x
# -----------------------------------------------------------------------------------------


#------------------------------------------------------------
# Auxiliary code for reading input and writing output

input = 'input.txt'
output = 'output.txt'

def readinput():
  words=[]
  with open(input) as f:
    lines = f.readlines()
    for line in lines:
      if line.split()!=[]:
        words.append(line.split())

  for j in range (0,len(words)):
    for i in range(0,len(words[j])):
      words[j][i]=int(words[j][i])

  return words

def writeoutput(line):

  with open(output,'a') as f:
     f.write(str(line)+'\n')

def clearoutput():
  f=open(output,'w')
  f.write('')


#------------------------------------------------------------
# Main Code to solve the problem

def game(opp, pos, dice):
    board = {1:{'square':True,'prime':False,'direction':'v'},
             2:{'square':False,'prime':True,'direction':'v'},
             3:{'square':False,'prime':True,'direction':'h'},
             4:{'square':False,'prime':False,'direction':'h'},
             5:{'square':False,'prime':True,'direction':'h'},
             6:{'square':False,'prime':False,'direction':'h'},
             7:{'square':False,'prime':True,'direction':'v'},
             8:{'square':False,'prime':False,'direction':'h'},
             9:{'square':True,'prime':False,'direction':'h'},
             10:{'square':False,'prime':False,'direction':'h'},
             11:{'square':False,'prime':True,'direction':'h'},
             12:{'square':False,'prime':False,'direction':'v'},
             13:{'square':False,'prime':True,'direction':'h'},
             14:{'square':False,'prime':False,'direction':'h'},
             15:{'square':False,'prime':False,'direction':'h'},
             16:{'square':True,'prime':False,'direction':'h'},
             17:{'square':False,'prime':True,'direction':'v'},
             18:{'square':False,'prime':False,'direction':'h'},
             19:{'square':False,'prime':True,'direction':'h'},
             20:{'square':False,'prime':False,'direction':'h'},
             21:{'square':False,'prime':False,'direction':'h'},
             22:{'square':False,'prime':False,'direction':'v'},
             23:{'square':False,'prime':True,'direction':'v'},
             24:{'square':False,'prime':False,'direction':'v'},
             25:{'square':True,'prime':False,'direction':'v'},
             26:{'square':False,'prime':False,'direction':'h'},
             27:{'square':False,'prime':False,'direction':'v'},
             28:{'square':False,'prime':False,'direction':'v'},
             29:{'square':False,'prime':True,'direction':'v'},
             30:{'square':False,'prime':False,'direction':'v'},
             31:{'square':False,'prime':True,'direction':'h'},
             32:{'square':False,'prime':False,'direction':'h'},
             33:{'square':False,'prime':False,'direction':'h'},
             34:{'square':False,'prime':False,'direction':'h'},
             35:{'square':False,'prime':False,'direction':'v'},
             36:{'square':True,'prime':False,'direction':'h'},
             37:{'square':False,'prime':True,'direction':'h'},
             38:{'square':False,'prime':False,'direction':'h'},
             39:{'square':False,'prime':True,'direction':'h'},
             40:{'square':False,'prime':False,'direction':'v'},
             41:{'square':False,'prime':True,'direction':'h'},
             42:{'square':False,'prime':False,'direction':'h'},
             43:{'square':False,'prime':True,'direction':'h'},
             44:{'square':False,'prime':False,'direction':'h'},
             45:{'square':False,'prime':False,'direction':'v'},
             46:{'square':False,'prime':False,'direction':'h'},
             47:{'square':False,'prime':True,'direction':'h'},
             48:{'square':False,'prime':False,'direction':'h'},
             49:{'square':True,'prime':False,'direction':'h'},
             50:{'square':False,'prime':False,'direction':'v'},
             51:{'square':False,'prime':False,'direction':'v'},
             52:{'square':False,'prime':False,'direction':None}}
    #game(opp, pos, dice)
    pos = int(pos)
    opp = list(map(int, opp))
    dice = list(map(int, dice))
    oldpos = 0
    while dice:
        posDerec = []
        if pos >= 52:
            return "GAME OVER"
        while dice:
            if pos + dice[0] in opp:
                dice.pop(0)
                
                if dice == []:
                    return pos
                
            else:
                oldpos = pos
                count = 0
                while count != dice[0]:
                    posDerec.append(board[pos]['direction'])
                    pos = pos + 1
                    count = count + 1
                break
            
        if pos >= 52:
            return "GAME OVER"

        
        elif board[pos]['prime']:
            count = 6
            while count:
                if pos + 1 in opp:
                    break
                
                else:
                    pos = pos + 1
                    
                count = count - 1
             
        elif board[pos]['square']:
            count = 6
            while count:
                if pos - 1 in opp:
                    break
                
                else:
                    pos = pos - 1
                    
                count = count - 1
        
        else:
            fail = 0
            hlocation = list(filter(lambda x: posDerec[x] == 'h', range(len(posDerec))))
            vlocation = list(filter(lambda x: posDerec[x] == 'v', range(len(posDerec))))
            if hlocation == [] or vlocation == []:
                fail = 1
                
            elif hlocation[0] > vlocation[-1]:
                fail = 1
                
            elif hlocation[0] < vlocation[-1]:
                check = False
                for x in range(oldpos+1, pos+1):
                    if x % dice[0] == 0:
                        if x not in opp:
                            check = True
                            pos = x
                            break
                        
                if not check:
                    pos = oldpos
                        
        dice.pop(0)
    return pos


input = readinput()

clearoutput()
for line in input:
    opp = line[0:3]
    pos = line[3]
    dice = line[5:]
    writeoutput(game(opp,pos,dice))