# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem in Contest #4 – Junior Division
# Author :  Harsha Ganta
# 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 check_for_edges(my_marker, my_marker_new):
    edges = [7, 12, 17, 22, 27, 35, 40, 45, 50]
    #print("Checking for edges")
    for k in range(len(edges)):
        if my_marker_new > edges[k] > my_marker:
            status = 1
            break
        else:
            status = 0
    return status

def patolli_game(S):
    opp_markers = S[0:3]    
    #print("Oppenent markers:", opp_markers)    

    my_marker = S[3]
    #print("My marker:", my_marker)

    die_rolls = S[4]
    #print("# of Die Rolls:", die_rolls)

    die_values = (S[5:])
    #print("Values of rolls", die_values)

    primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
    squares = [9, 16, 25, 36, 49]
    edges = [7, 12, 17, 22, 27, 35, 40, 45, 50]


    for i in range(die_rolls):
        #print("current die value:", die_values[i])
        my_marker_new = my_marker + die_values[i]
        #print("my_marker_new:", my_marker_new)
        if my_marker_new in opp_markers:
            #print(my_marker_new, "is already occupied")
            my_marker = my_marker
        elif my_marker_new in primes:
            my_marker_prime = my_marker_new + 6
            for j in range(len(opp_markers)):
                if my_marker_prime >= opp_markers[j] > my_marker_new:
                    my_marker = (opp_markers[j] - 1)
                    #print("Prime-check triggered - foward movement blocked by", opp_markers[j])
                    break
                else: my_marker = my_marker_prime
            #print("Prime-check triggered, my_marker value is:", my_marker)
        elif my_marker_new in squares:
            my_marker_square = my_marker_new - 6
            for j in range(len(opp_markers)):
                if my_marker_square <= opp_markers[j] <= my_marker_new:
                    #print("Opp Marker:", opp_markers[j])
                    my_marker = opp_markers[j] + 1
                    #print("Square-check triggered - back-ward movement blocked by", opp_markers[j])
                    break
                else: my_marker = my_marker_square
            #print("Square-check triggered, my_marker value is:", my_marker)
        elif check_for_edges(my_marker, my_marker_new)== 1:
            my_marker = my_marker_new//die_values[i] * die_values[i]
            #print("Hor-Vert Check triggered, my_marker_value is", my_marker)
        else:
            my_marker = my_marker_new
            #print("No other check triggered")
            
        #print(my_marker,"my marker is updated")
            
    if int(my_marker) == 52:
        return "GAME OVER"
    if int(my_marker) > 52:
        my_marker = my_marker
    #print("my marker final is:", my_marker)
    return my_marker   
    


input = readinput()
clearoutput()
for line in input:
    S = line
    #print ("****Starting new line*****")
    writeoutput(patolli_game(S))