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

always  = [7,12,17,22,35,40,45,50]


def isPrime(num):
    if num > 1:
        for i in range(2, num // 2):
            if (num % i == 0):
                return False
        return True


def isPerfectSquare(numb):
    for y in range(1, (numb // 2) + 1):
        if y * y == numb and numb > 4:
            return True
    return False


def patolli(line):
    # opponent marker position
    oppo = line[0:3]
    oppo.sort()
    # the players marker position
    pmp = line[3]
    # the number of rolls you do
    rolls = line[4]
    # the values of the die rolls
    vls = line[5:]
    for x in range(0, len(vls)):
        flag = 0
        flag1 = 0
        flag2 = 0
        npmp = pmp + vls[x]
        # if it is already occupied, stay at current location
        if npmp in oppo:
            npmp = pmp
        # make sure the marker is taken off and given GAME OVER sign
        if npmp == 52:
            return 'GAME OVER'
        elif npmp > 52:
            npmp = pmp
        elif isPrime(npmp) == True:
            if npmp in oppo:
                npmp = pmp
            else:
                for y in range(0, len(oppo)):
                    if oppo[y] in range(npmp, npmp + 7):
                        flag = 1
                        npmp = oppo[y] - 1
                        while npmp in oppo:
                            npmp = npmp - 1
                        break
                if flag == 0:
                    npmp = npmp + 6
                pmp = npmp

        elif isPerfectSquare(npmp) == True:
            if npmp in oppo:
                npmp = pmp
            else:
                for y in range(len(oppo) - 1, -1, -1):
                    if oppo[y] in range(npmp - 6, npmp + 1):
                        flag1 = 1
                        npmp = oppo[y] + 1
                        while npmp in oppo:
                            npmp = npmp + 1
                        break
                if flag1 == 0:
                    npmp = npmp - 6
                pmp = npmp
        else:
            if pmp == 26 and vls[x] > 1:
                for number in range(pmp + 1,npmp+1):
                    if number%vls[x]==0:
                        if number in oppo:
                            continue
                        else:
                            pmp=number
                            break

            else:
                for z in range(0, len(always)):
                    if pmp < always[z] < npmp:
                        flag2 = 1
                        for number in range(pmp + 1, npmp + 1):
                            if number % vls[x] == 0:
                                if number in oppo:
                                    continue
                                else:
                                    pmp = number
                                    break
                if flag2 == 0:
                    pmp = npmp
    return pmp





input = readinput()

clearoutput()
for line in input:
    writeoutput(patolli(line))