#!/usr/bin/perl -w

use v5.16;

$/ = "\n\n";

my @drawn = split/[,\s]/, <>;
my @boards = <>;

for my $d (@drawn) {
	for my $board (@boards) {
		$board =~ s/\b$d\b/' ' x length $d/e;
		if ($board =~ / {14}/
			|| $board =~ /\s{3}(?:.{12}\s{3}){4}/xms) {
			$board =~ s/(?<=\d)\s+(?=\d)/+/gxms;
			say eval "$d*($board)";
			exit;
		}
	}
}

