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

output_equation = ""
horizontalToVertical = [7,12,17,22,27,35,40,45,50]
squares = [9, 16, 25, 36, 49]
PlayerPosition = []


def isSquare(n):
    if (n in squares):
        return True
    else:
        return False


def isPrime(n):
    if (n <= 1):
        return False
    else:
        for i in range (2, n -1):
            if((n % i) == 0):
                return False
        else:
            return True

def isHorizontalVerticalMove(start_position, end_position):

    # condition checks for the number of numbers in the range between start and end range
    # if count > 1 then there is a horizontal to vertical move

    count = len(list(x for x in horizontalToVertical if start_position < x < end_position))
    #print('isHorizontalVerticalMove:', start_position, end_position, 'count =', count)

    if (count):
        return True
    else:
        return False

def move_ahead(Player4, Die):

    new_position = Player4 + Die

    # If occupied don't move
    if new_position in PlayerPosition:
        new_position = Player4

    elif(isPrime(new_position)):
        new_position = ifPrime(new_position)

    elif(isSquare(new_position)):
        new_position = ifSquare(new_position)

    elif(isHorizontalVerticalMove(Player4, new_position)):
        new_position = ifHorizontalVerticalMove(Player4, Die)

    #If dieroll go off board max, stay at same place
    if new_position > 52:
        new_position = Player4

    return new_position

def ifPrime( position):

    current_position = position
    new_position = current_position + 6
    # If any occupied location in between then stop behind occupied

    new_position = findanyplayersinbetween(current_position, new_position)
    return new_position

def ifSquare( position):

    current_position = position
    new_position = current_position - 6
    # If any occupied location in between then stop before occupied

    new_position = findanyplayersinbetween(current_position, new_position)
    return new_position

def findanyplayersinbetween(start_position, end_position):

    new_position = end_position

    # move forward for prime case
    if (start_position < end_position):
        for i in range(start_position + 1, end_position + 1):
            #print("findanyplayersinbetween increase: ", i)
            if i in PlayerPosition:
                #print("findanyplayersinbetween increase: found player at#", i)
                new_position = i - 1
                break

    # move backward - square case
    else :
        for i in range(start_position - 1, end_position - 1, -1):
            #print("findanyplayersinbetween decrease: ", i)
            if i in PlayerPosition:
                #print("findanyplayersinbetween decrease: found player at#", i)
                new_position = i + 1
                break

    return new_position

def ifHorizontalVerticalMove(position, Die):
    #print('start ifHorizontalVerticalMove: ', position, Die)

    original_position = position
    current_position = original_position
    end_position = current_position + Die
    new_position = original_position
    #print('ifHorizontalVerticalMove: ', original_position, end_position)

    for i in range(current_position + 1, end_position + 1):
        if (i > 52):
            new_position = original_position

        elif (i % Die == 0):
            if (i not in PlayerPosition):
                #print('ifHorizontalVerticalMove: not occupied i =', i)
                new_position = i
                break
        #print('ifHorizontalVerticalMove: i =',i)

    return new_position


################################################
# main program
################################################
Die = []
input = ovcc_grader.readinput()
ovcc_grader.clearoutput()
for line in input:
    PlayerPosition.append(int(line[0]))
    PlayerPosition.append(int(line[1]))
    PlayerPosition.append(int(line[2]))

    #print(PlayerPosition)
    Player4 = int(line[3])

    if (Player4 > 52):
        output_equation += "INVALID STARTING POSITION"
        ovcc_grader.writeoutput(output_equation)
        break

    DieRolls = int(line[4])
    i = 5
    for d in range(0, DieRolls):
        Die.append(int(line[i]))
        i = i + 1


    for d in range(0, DieRolls):
        new_position = move_ahead(Player4, Die[d])
        #print('end of Die#',d+1, 'new_position', new_position)
        Player4 = new_position

        if new_position == 52:
            output_equation += "GAME OVER"
            ovcc_grader.writeoutput(output_equation)
            break

    if output_equation == "":
        output_equation += str(Player4)
        ovcc_grader.writeoutput(output_equation)

    ##################################


    #print('end of input')
    #reset all global variables
    output_equation = ""
    PlayerPosition.clear()
    Die.clear()