# Anthony Lekan
# Bergen County Academies
# Intermediate

# used for applying rule #9
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],
]

FINAL_POSITION = 52

PRIME_NUMBERS = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47]
PERFECT_SQUARES = [9,16,25,36,49]

def get_position(marker):
	for row in range(len(BOARD)):
		for col in range(len(BOARD[row])):
			if BOARD[row][col] == marker:
				return [row, col]

	return [0, 0]

def is_next_move_horizontal(marker, marker_col):
	next_marker = marker+1
	[next_marker_row, next_marker_col] = get_position(next_marker)

	return abs(next_marker_col-marker_col) == 1

def is_vertical_horizontal_move(og_marker, new_marker):
	[og_marker_row, og_marker_col] = get_position(og_marker)
	[new_marker_row, new_marker_col] = get_position(new_marker)

	row_delta = abs(new_marker_row - og_marker_row)

	return is_next_move_horizontal(og_marker, og_marker_col) and row_delta >= 1

# apply rules to move
def move(marker, amount, occupied_locations):
	new_location = marker+amount

	# keep current position - rule #4
	if new_location in occupied_locations:
		return marker

	# rule #7
	if new_location in PRIME_NUMBERS:
		for i in range(6):
			if new_location+1 in occupied_locations:
				break

			new_location += 1

	elif new_location in PERFECT_SQUARES:
		for i in range(6):
			if new_location-1 in occupied_locations or new_location == 0:
				break

			new_location -= 1
	else:

		# horizontal/vertical movement
		if is_vertical_horizontal_move(marker, new_location):
			new_location = marker

			for i in range(1, amount+1):
				new_marker = marker+i

				if new_marker % amount == 0 and not new_marker in occupied_locations:
					new_location = new_marker

	# remove from board - rule #6
	if new_location == FINAL_POSITION:
		return None
	elif new_location > FINAL_POSITION:
		return marker

	return new_location


# return new locations of markers
def run_round(player_markers, opponent_markers, die_roll):
	player_markers.sort()
	current_marker = player_markers.pop(0)

	new_marker = move(current_marker, die_roll, player_markers+opponent_markers)

	new_markers = []

	if not new_marker is None:
		new_markers.append(new_marker)

	return new_markers + player_markers


# create a new board
# execute each move on the markers with the die_roll
# finally return the player_markers
def run_game(player_markers, opponent_markers, die_rolls):
	round = 0

	for roll in die_rolls:
		round += 1
		player_markers = run_round(player_markers, opponent_markers, roll)

	return player_markers


# parse input & print output
def main():

	with open('4int_sampledata.txt') as file:

		for line in file:
			game_info = line.strip().split(' ')

			opponent_markers = [int(x) for x in [game_info[0], game_info[1], game_info[2]]]
			player_markers = [int(x) for x in [game_info[3], game_info[4], game_info[5]]]

			die_rolls = []

			for i in range(1, int(game_info[6])+1):
				die_rolls.append(int(game_info[6+i]))

			# print(opponent_markers, player_markers, die_rolls)
			result = run_game(player_markers, opponent_markers, die_rolls)

			result.sort()

			if(len(result) > 0):
				print(' '.join([str(x) for x in result]))
			else:
				print('GAME OVER')


main()