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

import math

# Debug flag
DEBUG = False
DEBUG_LOW = False

# Game over - Reached the end of the game
END = 52

# Bonus points for landing on a prime
BONUS_POINTS_FOR_LANDING_PRIME = 6

# Penalty points for landing on a perfect square
PENALTY_FOR_LANDING_SQUARE = 6

# H-V table for the game
rule_9 = {6, 11, 16, 21, 26, 34, 39, 44, 49}

# Is it a H-V corner move?
def is_h_v_move(start, end):
  for index in rule_9:
    h = False;
    v = False;
    for x in range (int(start), int(end+1)):
      if (x == index):
        h = True
      if (x == index+2):
        v = True; 
        if (h and v):
          if DEBUG:
            print ("Rule 9: Detected an H-V Move - Start: ", start, " End: ", end)
          return True;
  return False;

# Is this a prime?
def is_prime(factor):
  x = int(factor)
  if x >= 2:
    for y in range(2,x):
      if not ( x % y ):
        return False
  else:
	  return False
  if DEBUG:
    print ("Rule 7: Detected landing on a prime number location - ", factor)
  return True

# Is this a perfect sqaure?
def is_square(factor):
    num = int(factor)
    if (num<=4):
      return False
    root = math.sqrt(num)
    if int(root + 0.5) ** 2 == num:
      if DEBUG:
        print ("Rule 8: Detected a perfect square > 4 - ", factor)
      return True
    else:
        return False

# Did we reach the end of game?
def is_game_over(new_pos):
  # Rule 6: Game over when marker lands on location 52
  if new_pos == END:
    if DEBUG:
      print ("Rule 6: Detected Game over!")
    return True
  return False

# Is the position occupied?
def is_pos_occupied(new_pos, opponents):
  omarker_positions = map (int, opponents)
  if int(new_pos) in omarker_positions:
    if DEBUG:
      print ("DEBUG: Position is occupied! - ", new_pos)
    return True
  return False

# Find a spot from prime spot
def find_spot_from_prime (new_pos, opponents):
  om = map (int, opponents)
  omarker_positions = list(om) 
  for step in range (1, BONUS_POINTS_FOR_LANDING_PRIME+1):
    if int(new_pos) + step in omarker_positions:
      if DEBUG:
        print ("DEBUG: Position is occupied! - ", int(new_pos)+step)
      return new_pos+step-1
  return new_pos+BONUS_POINTS_FOR_LANDING_PRIME

# Find a spot from perfect square spot
def find_spot_from_square (new_pos, opponents):
  om = map (int, opponents)
  omarker_positions = list(om) 
  for step in range (1, PENALTY_FOR_LANDING_SQUARE+1):
    if int(new_pos) - step in omarker_positions:
      if DEBUG:
        print ("DEBUG: Position is occupied! - ", int(new_pos)-step)
      return new_pos-step+1
  return new_pos-PENALTY_FOR_LANDING_SQUARE

# Find a spot from HV spot
def find_spot_from_h_v (prev_pos, opponents, new_pos, roll):
  om = map (int, opponents)
  omarker_positions = list(om) 
  for step in range (int(prev_pos+1), int(new_pos+1)):
    if step in omarker_positions:
      if DEBUG:
        print ("DEBUG: Position is occupied! - ", step)
      continue
    else:
      value = step % roll
      if value == 0:
        if DEBUG:
          print ("DEBUG: HV Muiltiple - ", step)
        return step
  return prev_pos

# Play the game - Execute this once per every line in input.txt
def play_game (opponents, player, R, die_rolls):

  # Before proceeding further, make sure we are not at the end of the game already
  if is_game_over(int(player)):
    return "GAME OVER"

  # Map the die rolls to incremental steps in the game 
  rolls = map (int, die_rolls)

  # Save the player start pos
  prev_pos = int(player)

  # Walk through the rolls   
  for roll in rolls:

      # Before we evaluate the roll, check if game is over already
      if is_game_over(int(player)):
        return "GAME OVER" 

      # Compute the new position based on the roll 
      new_pos = int(prev_pos) + int(roll)
      if DEBUG:
        print("Roll: ", roll, " Eval New pos: ", new_pos)

      # Rule 6: If new place > 52, stay where you are 
      if int(new_pos) > END:
        if DEBUG:
          print ("Rule 6: Can't go beyond 52, stay where u are")
        continue;
       
      # Check if the new position is alrady occupied by the opponent marker 
      # If yes, let's stay where we are and go to the next roll ..
      if is_pos_occupied(new_pos, opponents):
        if DEBUG:
          print ("Rule #5: Occupied slot - Stay where we are! ", prev_pos)
        new_pos = prev_pos
        continue

      # If slot is not special, update position and go to next roll
      if is_prime(new_pos) == False and is_square(new_pos) == False and is_h_v_move(prev_pos, new_pos) == False:
        if DEBUG:
          print ("Rule #3: Moving from ", prev_pos, " to ", new_pos)
        prev_pos = new_pos
        if is_game_over(int(prev_pos)):
          return "GAME OVER"
        continue

      # If we come here, this is a special case

      # Special Case - Handling Prime Number Spots
      if is_prime(new_pos):
        new_pos = find_spot_from_prime(new_pos, opponents)
        if DEBUG:
          print ("DEBUG: Moving from ", prev_pos, " to ", new_pos)
        prev_pos = new_pos 
        if is_game_over(int(prev_pos)):
          return "GAME OVER"
        continue

      # Special Case - Handling Perfect Square > 4 Spots
      if is_square(new_pos):
        new_pos = find_spot_from_square(new_pos, opponents)
        if DEBUG:
          print ("DEBUG: Moving from ", prev_pos, " to ", new_pos)
        prev_pos = new_pos 
        prev_pos = new_pos
        if is_game_over(int(prev_pos)):
          return "GAME OVER"
        continue

      # Special Case - Handling Perfect Square > 4 Spots
      if is_h_v_move(prev_pos, new_pos):
        new_pos = find_spot_from_h_v(prev_pos, opponents, new_pos, roll)
        if DEBUG:
          print ("DEBUG: Moving from ", prev_pos, " to ", new_pos)
        prev_pos = new_pos
        if is_game_over(int(prev_pos)):
          return "GAME OVER"
        continue

  if DEBUG_LOW:
    print(prev_pos)
  
  return prev_pos

# Main Driver code for the Game
input = readinput()
clearoutput()

for line in input:
  opponents = line[0:3]
  player = line[3]
  R= line[4]
  die_rolls=line[5:] 
  if DEBUG_LOW:
    print("Opponents:", opponents, "\n\tPlayer: ", player, "\n\tR: ", R, "\n\tDie rolls: ", die_rolls, sep=' ', end='\n')
  writeoutput(play_game(opponents, player, R, die_rolls))