# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem in Contest #4 – Junior Division
# Author :  Rahul Verma
# 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 CheckSquare(x):
    threshold = 0.0000000000000000000000000000000000000001
    approximation = x/2 # Start with some or other guess at the answer
    square  = False
    if(x>6):
        while True:
            better = (approximation + x/approximation)/2
            if (abs(approximation - better) < threshold):
                break
            approximation = better

        if(better%1 == 0):
            end = True
            return end
        else:
            end = False
            return end
    else:
        end = False
        return end
def CheckPrime(x):
    prime = False
    prime = True
    max = int(x/2) + 1
    for i in range(2,max):
        if (x/i == int(x/i)):
            prime = False
            break
    
    return prime
def patolli(Opponent1,Opponent2,Opponent3,Player1,NumMoves,Move):
    Opponent1 = int(Opponent1)
    Opponent2 = int(Opponent2)
    Opponent3 = int(Opponent3)
    Player1 = int(Player1)
    NumMoves = int(NumMoves)
    board = []
    HorVer = [7,12,17,22,27,35,40,46,50]
    for i in range(1,53):
        board.append([i,False,False,False])
        for j in range(0,9):
            if(board[i-1][0] == HorVer[j]):
                board[i-1][2] = True
    board[Opponent1-1][1] = True
    board[Opponent2-1][1] = True
    board[Opponent3-1][1] = True
    board[Player1-1][3] = True
    PreviousMark = Player1
    PlayerMark = Player1
    result = ""
    for i in range(0,NumMoves):
        PreviousMark = PlayerMark
        PlayerMark = int(Move[i]) + PlayerMark
        if(PlayerMark == 52 and board[51][1] == False):
            return "GAME OVER"
            break
        elif(PlayerMark>52):
            PlayerMark = PreviousMark
            continue
        elif(board[PlayerMark-1][1] == True):
            PlayerMark = PreviousMark
            if(PlayerMark == 52 and board[51][1] == False):
                return "GAME OVER"
                break
            continue
        elif (CheckPrime(PlayerMark) == True):
            PreviousMark = PlayerMark
            PlayerMark = PlayerMark+6
            for a in range(PreviousMark,PlayerMark):
                if(board[a-1][1] == True):
                    PlayerMark = a-1
                    break
                else:
                    continue
            if(PlayerMark == 52 and board[51][1] == False):
                return "GAME OVER"
                break
            continue
        elif (CheckSquare(PlayerMark) == True):
            PreviousMark = PlayerMark
            PlayerMark = PlayerMark-6
            for b in range(PreviousMark,PlayerMark,-1):
                if(board[b-2][1] == True):
                    PlayerMark = b
                    break
                else:
                    continue
            continue
        else:
            #print(PreviousMark,PlayerMark,len(HorVer),Mo
            for k in range(0,len(HorVer)):
                if(PreviousMark52):
            PlayerMark = PreviousMark
            continue
                   
    return PlayerMark
                
            
                
        

input = readinput()

clearoutput()
for line in input:
    Enter1 = line[0]
    Enter2 = line[1]
    Enter3 = line[2]
    Enter4 = line[3]
    Enter5 = line[4]
    line.pop(0)
    line.pop(0)
    line.pop(0)
    line.pop(0)
    line.pop(0)
    writeoutput(patolli(Enter1,Enter2,Enter3,Enter4,Enter5,line))
"""
4 6 8 1 5 6 3 5 1 1
10 24 32 8 4 4 4 3 5
10 22 32 8 7 4 4 3 5 5 5 6
17 20 27 16 7 3 5 4 6 5 1 4
43 46 50 40 5 3 1 2 4 4
25 27 49 22 7 2 2 6 6 5 3 6
50 41 38 45 9 4 2 5 3 1 6 4 3 1
21 26 30 19 6 6 4 6 1 2 3
5 14 18 2 7 2 5 4 5 2 1 6
10 17 20 9 12 4 5 3 1 6 2 3 3 5 4 1 6
"""