# -----------------------------------------------------------------------------------------
# Oak Valley Coding Club 2019-2020
# Solution for 2020 ACSL Programming Problem in Contest #4 – Junior Division
# Author :  Aadit Mathur
# 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

#This is the gameboard. 0s just fill in spaces
#This is for rule 9 as you must track if it has gone horizontally and vertically
gameBoard = [
    [ 0, 0, 0, 0, 1,52, 0, 0, 0, 0],
    [ 0, 0, 0, 0, 2,51, 0, 0, 0, 0],
    [ 7, 6, 5, 4, 3,50,49,48,47,46],
    [ 8, 9,10,11,12,41,42,43,44,45],
    [17,16,15,14,13,40,39,38,37,36],
    [18,19,20,21,22,31,32,33,34,35],
    [ 0, 0, 0, 0,23,30, 0, 0, 0, 0],
    [ 0, 0, 0, 0,24,29, 0, 0, 0, 0],
    [ 0, 0, 0, 0,25,28, 0, 0, 0, 0],
    [ 0, 0, 0, 0,26,27, 0, 0, 0, 0]
]

def checkForGameConds(playerPos):
    if playerPos == 52:
        return 'GAME OVER'
    elif playerPos > 52:
        return 'Too big'
    elif playerPos < 52:
        return 'Small & sweet'
def play_game(opponents, player, R, die_rolls, gameBoard):
    primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
    playerPos = player
    for x in range(R):
        curr_roll = die_rolls[x]
        localPos = playerPos
        if ((playerPos + curr_roll) not in opponents):
            playerPos += curr_roll

            cond = checkForGameConds(playerPos)
            if cond == 'GAME OVER':
                playerPos = 'GAME OVER'
                return playerPos
            elif cond == 'Too big':
                playerPos = localPos
            
            #Rule 7 - for convenience, check if in list of primes until 52
            if playerPos in primes:
                localPos2 = playerPos
                # Goes step by step and stops before opponent
                for x in range(6):
                    playerPos += 1
                    if playerPos in opponents:
                        playerPos -= 1
                        break
                
                cond = checkForGameConds(playerPos)
                if cond == 'GAME OVER':
                    playerPos = 'GAME OVER'
                    return playerPos
                elif cond == 'Too big':
                    playerPos = localPos2
            #Rule 8 - to check if it is square: "playerPos**(.5) == round(playerPos**(.5))"
            #       - to check if it is greater than 4: "playerPos > 4"
            elif (playerPos**(.5) == round(playerPos**(.5))) and (playerPos > 4):
                localPos3 = playerPos
                # Goes step by step and stops before opponent
                for x in range(6):
                    playerPos -= 1
                    if playerPos in opponents:
                        playerPos += 1
                        break
                
                cond = checkForGameConds(playerPos)
                if cond == 'GAME OVER':
                    playerPos = 'GAME OVER'
                    return playerPos
                elif cond == 'Too big':
                    playerPos = localPos2
            #Rule 9 - use gameboard matrix
            #       - 
            #elif and not if because it can be square like 1 or 4 but not bigger than 4
            elif (playerPos**(.5) != round(playerPos**(.5))) and (playerPos not in primes):
                localPos4 = playerPos
                # My Shortcut for this stuff - ask me if you want me to explain it, it'll take too much space to write here

                # Step 1: Get position of playerPos & localPos4 in board
                # 10 = gameboard rows
                # 10 = gameboard row cols
                for row in range(10):
                    for col in range(10):
                        if localPos == gameBoard[row][col]:
                            oldRow = row
                            oldCol = col
                        if playerPos == gameBoard[row][col]:
                            currRow = row
                            currCol = col
                #Step 2: The shortcut - consult me for info
                if (oldRow != currRow) and (oldCol not in [0, 4, 5, 9]):
                    #Do the stuff that the rule says
                    backupPos = playerPos
                    if playerPos % curr_roll == 0:
                        pass
                    else:
                        multiples = [n for n in range(localPos, playerPos + 1) if n % curr_roll == 0]
                        multiples = multiples[::-1]
                        for multiple in multiples:
                            if multiple not in opponents:
                                playerPos = multiple
                                break
                    cond = checkForGameConds(playerPos)
                    if cond == 'GAME OVER':
                        playerPos = 'GAME OVER'
                        return playerPos
                    elif cond == 'Too big':
                        playerPos = localPos4

    return playerPos


input = readinput()

clearoutput()
for line in input:
    opponents = line[0:3]
    player = line[3]
    R = line[4]
    die_rolls=line[5:]
    writeoutput(play_game(opponents, int(player), int(R), die_rolls, gameBoard))