#!/usr/bin/perl -w

use v5.16;

my %digits = (
	abcefg	=> 0,
	cf	=> 1,
	acdeg	=> 2,
	acdfg	=> 3,
	bcdf	=> 4,
	abdfg	=> 5,
	abdefg	=> 6,
	acf	=> 7,
	abcdefg	=> 8,
	abcdfg	=> 9,
);

my @perms;

sub do_perms {
	my ($t, @vals) = @_;
	if (@vals == 1) {
		push @perms, $t.$vals[0];
	} else {
		for my $i (0 .. $#vals) {
			my @v1 = @vals;
			my $u = splice @v1, $i, 1;
			do_perms("$t$u", @v1);
		}
	}
}

do_perms('', qw(a b c d e f g));

sub permute {
	my ($val, $perm) = @_;
	eval "\$val =~ y/abcdefg/$perm/";
	join('', sort split //, $val);
}

my $sum = 0;
ROW:
while (<>) {
	chomp;
	my ($inv, $outv) = split /\s+\|\s+/;
	my (@in) = sort { length $a <=> length $b } split /\s+/, $inv;
	# say "$inv => ", join(' ', @in);
	my (@out) = split /\s+/, $outv;

	PERM: for my $perm (@perms) {
		for my $i (@in) {
			my $ni = permute($i, $perm);
			next PERM if !defined $digits{$ni};
		}
		my $rv;
		for my $o (@out) {
			my $no = permute($o, $perm);
			$rv .= $digits{$no};
		}
		$sum += $rv;
		next ROW;
	}
}

say $sum;
			
		
