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

# method to detect horizontal then vertical moves
def HorizontalThenVertical(currentLoc, newLoc, xVal, op1, op2 ,op3):
    if currentLoc <= 6 < newLoc and currentLoc < 8 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 11 < newLoc and currentLoc < 13 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 16 < newLoc and currentLoc < 18 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 21 < newLoc and currentLoc < 23 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 26 < newLoc and currentLoc < 28 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 34 < newLoc and currentLoc < 36 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 39 < newLoc and currentLoc < 41 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 44 < newLoc and currentLoc < 46 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = newLoc
                else:
                    newLoc = x
    elif currentLoc <= 49 < newLoc and currentLoc < 51 <= newLoc:
        for x in range(currentLoc, newLoc):
            if x % xVal == 0 and x > currentLoc:
                if op1 == x or op2 == x or op3 == x:
                    newLoc = currentLoc
                else:
                    newLoc = x
    return newLoc

# method to detect if an opponent is behind you after landing on a perfect square
def OpBehind(op1, op2, op3, newLoc):
    if newLoc > op1 >= newLoc-6:
        newLoc = op1+1
        return newLoc
    elif newLoc > op2 >= newLoc-6:
        newLoc = op2+1
        return newLoc
    elif newLoc > op3 >= newLoc-6:
        newLoc = op3+1
        return newLoc
    else:
        newLoc = newLoc-6
        return newLoc

# method to detect perfect square
def isPerfectSquareGreaterThanFour(number):
    root = math.sqrt(number)
    if int(root+0.5)**2 == number and number>4:
        return True
    else:
        return False

# method to detect if an opponent is ahead of you when going forward after landing on a prime number
def OpBlocking(op1, op2, op3, newLoc):
    if newLoc < op1 < newLoc+6:
        newLoc=op1-1
        return newLoc
    elif newLoc < op2 < newLoc+6:
        newLoc = op2 - 1
        return newLoc
    elif newLoc < op3 < newLoc+6:
        newLoc = op3 - 1
        return newLoc
    else:
        newLoc+=6
        return newLoc
# method to detect prime number
def IsPrimeNumber(newLoc):
    isPrime=True
    if newLoc>1:
        for i in range(2, newLoc, 1):
            if newLoc % i == 0:
                 isPrime = False
    return isPrime

# Patolli game method
def Patolli(line):
    op1 = line[0]
    op2 = line[1]
    op3 = line[2]
    yourLoc = line[3]
    rolls = line[4]
    rollValues = line[5:]
    newLoc = yourLoc
    for x in rollValues:
        currentLoc = newLoc
        xVal = x
        newLoc = newLoc+x
        if newLoc == op1 or newLoc == op2 or newLoc == op2:
            newLoc = currentLoc
        if newLoc == 52:
            return "GAME OVER"
        if IsPrimeNumber(newLoc)==True:
            newLoc = OpBlocking(op1, op2, op3, newLoc)
        elif isPerfectSquareGreaterThanFour(newLoc):
            newLoc = OpBehind(op1, op2, op3, newLoc)
        else:
           newLoc = HorizontalThenVertical(currentLoc, newLoc, xVal, op1, op2, op3)
    return newLoc


input = readinput()
clearoutput()
for line in input:

    out = Patolli(line)
    writeoutput(out)