import pprint

def check_board(board):
    for line in board:
        if not sum(line):
            return True
    
    for col in range(5):
        if not sum([board[0][col], board[1][col], board[2][col], board[3][col], board[4][col]]):
            return True

    return False

def rem_num(board, num):
    for i, line in enumerate(board):
        for j, n in enumerate(line):
            if n == num:
                board[i][j] = 0

with open("input.txt", 'r') as f:
    nums = list(map(int, f.readline().strip().split(',')))
    boards = []
    board = []

    # print(nums)

    ls = f.read().split('\n')
    for l in ls:
        if l == '':
            if board:
                boards.append(board)
            board = []
        else:
            board.append(list(map(int, [e for e in l.strip().split(' ') if e])))
        
        l = f.readline()

    won = [0] * len(boards)
    # print(won)

    # pprint.pprint(boards)

    for n in nums:
        for i, b in enumerate(boards):
            rem_num(b, n)
            if check_board(b):
                if sum(won) == 0:
                    s = sum([e for l in b for e in l])
                    print(f"Part 1:{s*n}")
                    won[i] = 1
                elif sum(won) == (len(boards) - 1) and won[i] == 0:
                    s = sum([e for l in b for e in l])
                    print(f"Part 2:{s*n}")
                    exit()
                else:
                    won[i] = 1

        

    # pprint.pprint(boards)