# -*- coding: utf-8 -*-
"""
Created on Fri Apr 10 21:36:49 2020

@author: Mahika Banerjee
"""
import math

dbg = 0

opp1 = 0
opp2 = 0
opp3 = 0

def prime(num):
    prm = True
    if num < 1:
        prm = False
    elif num > 3:
        if num % 2 == 0 or num % 3 == 0:
            prm = False
        else:
            for i in range(5,math.ceil(int(math.sqrt(num)+1))):
               if num % i == 0:
                   prm = False
                   break 
    return prm

def oppo(mypos):
    if mypos == opp1 or mypos == opp2 or mypos == opp3:
        return True
    else:
        return False

def perf_sqr(num):
    temp = int(math.sqrt(num))
    if num == temp * temp:
        return True
    else:
        return False
            
istr_1 = "Enter your opponent's markers, your position, the number of dice rolls, and their results. Enter '0' to exit: "
istr_2 = 'Make sure you inputted the same number of dice roll values as you stated!'
while True:
    bsc_cmpnnts = input(istr_1)
                      
    if bsc_cmpnnts == '0':
        print('Thank you!!')
        break
    
    inp_split = bsc_cmpnnts.split(' ')
    #print(inp_split)
    try:
        for i in range(0,len(inp_split)):
            inp_split[i] = int(inp_split[i])
    except:
        print('Enter integers only!')
        continue
    
    opp1 = inp_split[0]
    opp2 = inp_split[1]
    opp3 = inp_split[2]
    mypos = inp_split[3]
    if inp_split[4] != len(inp_split)-5:
        print(istr_2)
        continue
    
    for i in range(0, inp_split[4]):
        npos = mypos + inp_split[5+i]
        if dbg == 1:
            print("Start pos: ", mypos, "Dice vale:", inp_split[5+i], end="  ")
        
        #Condition 6    
        if npos == 52:
            mypos = 52
            break
        #Condition 4 & 6  
        elif oppo(npos) or npos > 52:
            npos = mypos
            if dbg == 1:
                print("opp or >52", end="  ")
        else:
            #Condition 7
            if prime(npos):
                if dbg == 1:
                    print("prime: ", npos, end="  ")
                for j in range(0, 6):
                    if oppo(npos+1):
                        break
                    else:
                        npos += 1
                if dbg == 1:
                    print('Moved 6 spc fwdrd: ', npos, end="  ")
            #Conditon 8 
            elif npos > 4 and perf_sqr(npos):
                for j in range(0, 6):
                    if oppo(npos-1):
                        break
                    else:
                        npos -= 1
                if dbg == 1:
                    print('Moved 6 spc bkwrd: ', npos, end="  ")
            #Condition 9
            else:
                if dbg == 1:
                        print('old pos', mypos, end="  ")
                if mypos <= 6 and npos >= 8:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 11 and npos >= 13:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 16 and npos >= 18:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 21 and npos >= 23:
                    npos -= npos % inp_split[5+i]    
                elif mypos <= 26 and npos >= 28:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 34 and npos >= 36:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 39 and npos >= 41:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 44 and npos >= 46:
                    npos -= npos % inp_split[5+i]
                elif mypos <= 49 and npos >= 51:
                    npos -= npos % inp_split[5+i]
                
                if oppo(npos):
                    npos = mypos

            if dbg == 1:
                    print('new pos', npos)

            mypos = npos
            
##            if npos <= 52:
##                mypos = npos
                
    if mypos == 52:
        print('GAME OVER')
    else:
        print('The final position is: ', mypos)