# Takoma Park M.S.
# Jr-5
# Contest 4
# Sameer Mehta

def printGrid():
  print("    ",grid[1],grid[52],"    ",sep='')
  print("    ",grid[2],grid[51],"    ",sep='')
  print(grid[7],grid[6],grid[5],grid[4],grid[3],grid[50],grid[49],grid[48],grid[47],grid[46],sep='')
  print(grid[8],grid[9],grid[10],grid[11],grid[12],grid[41],grid[42],grid[43],grid[44],grid[45],sep='')
  print(grid[17],grid[16],grid[15],grid[14],grid[13],grid[40],grid[39],grid[38],grid[37],grid[36],sep='')
  print(grid[18],grid[19],grid[20],grid[21],grid[22],grid[31],grid[32],grid[33],grid[34],grid[35],sep='')
  print("    ",grid[23],grid[30],"    ",sep='')
  print("    ",grid[24],grid[29],"    ",sep='')
  print("    ",grid[25],grid[28],"    ",sep='')
  print("    ",grid[26],grid[27],"    ",sep='')
  print()

 
fileName = "4jr_testdata.txt"

direction = ['','V','V','H','H','H','H','V','H','H','H','H','V','H','H','H','H','V','H','H','H','H','V','V','V','V',
'H','V','V','V','V','H','H','H','H','V','H','H','H','H','V','H','H','H','H','V','H','H','H','H','V','V']

with open(fileName) as f:
  for line in f:
    numbers = line.strip().split()
    numbers = list(map(int,numbers))
    # print(numbers)
    grid = ["."]*53
    gameOver = False
    # printGrid()
    for i in range(3):
      # mark opponent marker with an X
      grid[numbers[i]] = "X"
    # printGrid()

    playerMarker = numbers[3]
    grid[playerMarker] = "1"
    # printGrid()

    numberOfDiceRolls = numbers[4]
    # print('numberofDiceRolls=',numberOfDiceRolls,sep='')
    for i in range(numberOfDiceRolls):
      diceRoll = numbers[5+i]
      # print('dice Roll #',i+1,'=',diceRoll,sep='')
      updatedPlayerMarker=playerMarker+diceRoll
      if updatedPlayerMarker <= 52 and grid[updatedPlayerMarker] != ".":
        # print("Occupied, lose turn and remain")
        # printGrid()
        continue
      elif updatedPlayerMarker == 52:
        print("GAME OVER")
        gameOver = True
        break
      elif updatedPlayerMarker in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]:
        # print("prime, move forward 6, stopping immediately before any occupied location")
        extraUpdatedPlayerMarker = updatedPlayerMarker
        for j in range(6):
          if grid[extraUpdatedPlayerMarker+1] != ".":
            break
          else:
            extraUpdatedPlayerMarker+=1
        # check to make sure not going off grid by coming from location 47 with no occupied locations to the end of the grid
        if extraUpdatedPlayerMarker < 52:
            updatedPlayerMarker = extraUpdatedPlayerMarker
      elif updatedPlayerMarker in [9,16,25,36,49]:
        # print("perfect square greater than 4, move backward 6, stopping immediately before any occupied location")
        extraUpdatedPlayerMarker = updatedPlayerMarker
        for j in range(6):
          if grid[extraUpdatedPlayerMarker-1] != "." and grid[extraUpdatedPlayerMarker-1]!='1':
            break
          else:
            extraUpdatedPlayerMarker-=1
        updatedPlayerMarker = extraUpdatedPlayerMarker
      else:
        # print("not prime or perfect square, check for horizontal move followed by vertical move")
        horizontal=False
        verticalAfterHorizontal=False
        extraUpdatedPlayerMarker = 0
        for j in range(diceRoll):
          if ((playerMarker+j+1)%diceRoll==0) and (grid[playerMarker+j+1]=="."):
            extraUpdatedPlayerMarker = playerMarker+j+1
            # print("Possible multiple",extraUpdatedPlayerMarker)
          if direction[playerMarker+j] == 'H':
            # print("Found horizontal")
            horizontal=True
          elif horizontal and direction[playerMarker+j] == 'V':
            # print("Found vertical after horizontal")
            verticalAfterHorizontal=True
        if verticalAfterHorizontal:
          if extraUpdatedPlayerMarker==0:
            # print("Any possible multiple is occupied, skipping move")
            # printGrid()
            continue
          else:
            # print("Using multiple", extraUpdatedPlayerMarker)
            updatedPlayerMarker = extraUpdatedPlayerMarker
      # print("Updating...")  
      grid[playerMarker]="."
      playerMarker = updatedPlayerMarker
      grid[playerMarker] = "1"
      # printGrid()
    if not gameOver:
      print(playerMarker)