#!/usr/bin/perl -w

use v5.38;

my $sum;
my @scores;
while (<>) {
	my ($have, $win) = /: (.*) \| (.*)/;
	my %is_win = map { $_ => 1 } ($win =~ /(\d+)/g);
	push @scores, scalar grep { $is_win{$_} } ($have =~ /(\d+)/g);
}

my @cards = (1) x @scores;
while (@scores) {
	my $n = shift @scores;
	my $c = shift @cards;
	$sum += $c;
	next if !$n;
	$cards[$_] += $c for 0 .. $n-1;
}
	
say $sum;
