#!/usr/bin/perl -w

use v5.36;
use strict;
use experimental 'multidimensional';

my $sum;
chomp (my @bags = <>);

while (my @a = splice(@bags, 0, 3)) {
	for my $c (split(//, $a[0])) {
		next if $a[1] !~ /$c/;
		next if $a[2] !~ /$c/;
		$sum += ord($c) - ord('A') + 27 if $c =~ /[A-Z]/;
		$sum += ord($c) - ord('a') +  1 if $c =~ /[a-z]/;
		say $c;
		last;
	}
}
say $sum;
