prime_numbers = [2,3,5,7,11,13,17,23,29,31,37,41,43,47]
perfect_square = [9,16,25,36,49]
hoverStr =["-1,0","1,0","1,2","2,1","2,-1"]
board = [[0,0,0,0,1,52,0,0,0,0],[0,0,0,0,2,51,0,0,0,0],[7,6,5,4,3,50,49,48,47,46],[8,9,10,11,12,41,42,43,44,45],[17,16,15,14,13,40,39,38,37,36],[18,19,20,21,22,31,32,33,34,35],[0,0,0,0,23,30,0,0,0,0],[0,0,0,0,24,29,0,0,0,0],[0,0,0,0,25,28,0,0,0,0],[0,0,0,0,26,27,0,0,0,0]]
nextPostn={3:-1,13:-1,36:-1,46:-1,8:1,18:1,31:1,41:1,26:1,1:0,7:0,12:0,17:0,22:0,27:2,35:2,40:2,45:2,50:2}
lastPos = 0 
hoverMove = 0
def getCurrentPosition(val):
  pos="0,0"
  i=0
  j=0
  k=0
  l=0
  m=0
  n=0
  global lastPos
  global hoverMove
  for i in range(0,10):
    for j in range(0,10):
      if(board[i][j]==val-1):
        k=i
        l=j
      elif(board[i][j]==val):
        pos= str(i)+","+str(j)
        m=i
        n=j
  if(km):
    lastPos=2
  elif(nl):
    lastPos=1
  #print("Pos:" + str(pos) +" k:"+str(k) +" l:"+str(l)+" m:"+str(m)+" n:"+str(n))
  return pos;
def nextPos(i,prevPos):
  return prevPos if None== nextPostn.get(i) else nextPostn.get(i)
def traverseBoard(initialPos, dieRoll):
  #print("traverse board, initialPos: " + str(initialPos) + " dieRoll:" + str(dieRoll))
  global lastPos
  global hoverMove
  global hoverStr
  moves=0
  seq=""
  exitloop=0
  initPos=getCurrentPosition(initialPos)
  ints = initPos.split(",")
  #print(ints[0])
  i=int(ints[0])
  j=int(ints[1])
  a=0
  while(a<52 and exitloop == 0):
    if(i>=0 and board[i][j]>0):
      nextPosition = nextPos(board[i][j], lastPos)
      moves=moves+1
      if(nextPosition == -1):
        j=j-1
        lastPos = -1
        seq=seq+",-1"
      elif(nextPosition ==1):
        j=j+1
        lastPos=1
        seq=seq+",1"
      elif(nextPosition == 0):
        if(board[i][j]!=2):
          seq=seq+ ",0"
        i=i+1
        lastPos=0
      elif(nextPosition == 2):
        if(board[i][j]!=30 and board[i][j]!=29):
          seq=seq+",2"
        i=i-1
        lastPos=2
      if(int(board[i][j])==52 or int(moves) == int(dieRoll)):
        exitloop = 1
      a=a+1
      
  for s in hoverStr:
    if( s in seq):
      hoverMove = 1
  return board[i][j]
 
def sortArray(x):
    n = len(x)
    for i in range(n):
        for j in range(i+1,n):
            if (x[i] > x[j]):
                temp = x[i]
                x[i] = x[j]
                x[j] = temp
    #print(x)
    return x
	
count = 5 #no.of inputs
#expression = ["4 6 8 1 5 6 3 5 1 1","10 24 32 8 4 4 4 3 5","10 22 32 8 7 4 4 3 5 5 5 6","17 20 27 16 7 3 5 4 6 5 1 4","43 46 50 40 5 3 1 2 4 4","25 27 49 22 7 2 2 6 6 5 3 6","50 41 38 45 9 4 2 5 3 1 6 4 3 1","21 26 30 19 6 6 4 6 1 2 3","5 14 18 2 7 2 5 4 5 2 1 6","10 17 20 9 12 4 5 3 1 6 2 3 3 5 4 1 6"]
expression = []
print('Enter all valid Inputs')
for i in range(count):
  expression.append(input())
print('Output')
currPosition = 1
pos=0
marker1=0
marker2=0
marker3=0
dRoll=0
for s in expression:       
  print("parsing expression to Traverse: " + s)     
  pos = int(s.split(" ")[3])
  marker1=int(s.split(" ")[0])
  marker2=int(s.split(" ")[1])
  marker3=int(s.split(" ")[2])
  a=0
  b=5
  while (a 4 and bl==0 and currPosition in perfect_square):
      c=2
      while(c>=0 and bl==0):
        if(currPosition > makerArray[c]):
          bl=1
          if(currPosition-6 > makerArray[c]):
            currPosition = currPosition-6
          else:
            currPosition = makerArray[c]+1
        c=c-1
      if(bl==0):
        currPosition= currPosition-6
      hoverMove=0
      
    if(hoverMove ==1):
      while((currPosition%int(dRoll)) !=0):
        currPosition= currPosition -1
    
    if(not(currPosition in makerArray)):
      pos=currPosition
    hoverMove=0
    bl=0
    currPosition = pos
    a=a+1
    b=b+1
  #print("Final result")
  print("GAME OVER" if currPosition == 52 else currPosition )
  lastPos = 0
  hoverMove=1