# Montgomery Blair HS
# Intermediate
# Contest 4
# Joshua Hsieh

import math

def isPrime(n):
    for i in range(2, math.floor(math.sqrt(n))+1):
        if (n%i == 0):
            return False
    return True

r = open("4int_testdata.txt", 'r')
inputs = []
for line in r:
    inputs.append(line.split())
    
for test in inputs:     
    for i in range(len(test)):
        test[i] = int(test[i])
        
    player = test[3:6]
    markers = test[0:6]
    moves = test[7:]
    corners = [7, 12, 17, 22, 27, 35, 40, 45, 50]
    for i in range(len(moves)):
        if len(player) > 0:
            marker = player[player.index(min(player))]
            new = marker+moves[i]
            if not new in markers and new <= 52:
                if new == 52: # if reached end.
                    player.remove(marker)
                    markers.remove(marker)
                elif isPrime(new): # if new location is prime
                    for j in range(6):
                        if new+1 in markers: # if the location in front is taken, stop
                            if new == 52: # reached end
                                player.remove(marker)
                                markers.remove(marker)
                            elif new < 52:
                                player[player.index(marker)] = new
                                markers[markers.index(marker)] = new
                            break
                        else:
                            new += 1
                elif new > 4 and math.floor(math.sqrt(new)) == math.sqrt(new): # if new location is a perfect square > 4
                    for j in range(6):
                        if new-1 in markers: # if the location behind is taken, stop
                            player[player.index(marker)] = new
                            markers[markers.index(marker)] = new
                            break
                        else:
                            new -= 1
                else: # if none of the above
                    corner = False
                    for j in range(marker+1, new):
                        if j in corners: # 1 horizontal 1 vertical
                            final = 0
                            for l in range(marker, new+1):
                                if l%moves[i] == 0 and not l in markers:
                                    final = l
                            if final > 0:
                                player[player.index(marker)] = final
                                markers[markers.index(marker)] = final
                            corner = True
                    if not corner:
                        player[player.index(marker)] = new
                        markers[markers.index(marker)] = new
                        
    if len(player) == 0:
        print("GAME OVER")
    else:
        result = ""
        player.sort()
        for i in range(len(player)-1):
            result += str(player[i])+" "
        result+= str(player[-1])
        print(result)
		
r.close()