#!/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));

my $sum = 0;
ROW:
while (<>) {
	chomp;
	my ($inv, $outv) = split /\s+\|\s+/;
	my (@in) = split /\s+/, $inv;
	my (@out) = split /\s+/, $outv;

	PERM:
	for my $perm (@perms) {
		for my $i (@in) {
			my $ni = $i;
			eval "\$ni =~ y/abcdefg/$perm/";
			my $sorted = join('', sort split //, $ni);
			# say "i=$i ni=$ni sorted=$sorted";
			next PERM if !defined $digits{$sorted};
		}
		my $rv = '';
		for my $o (@out) {
			my $no = $o;
			eval "\$no =~ y/abcdefg/$perm/";
			my $nsorted = join('', sort split //, $no);
			$rv .= $digits{$nsorted};
		}
		$sum += $rv;
		next ROW;
	}
}

say $sum;
			
		
