#!/usr/bin/perl -w

use v5.38;

my %cards = map { /(\S+)/g } <>;

sub rank {
	my %hist;
	$hist{$_}++ for split //, shift;
	my %rh;
	$rh{$_}++ for values %hist;
	return 1 if $rh{5};
	return 2 if $rh{4};
	return 3 if $rh{3} && $rh{2};
	return 4 if $rh{3};
	return 5 if $rh{2} && $rh{2} == 2;
	return 6 if $rh{2};
	return 7;
}

sub val {
	my $val = shift;
	$val =~ y/AKQJT98765432/ABCDEFGHIJKLM/;
	return rank($val).$val;
}

my $sum;
my $i;
for my $card (sort { val($b) cmp val($a) } keys %cards) {
	# say "$card $i ", val($card);
	$sum += $cards{$card} * ++$i;
}

say $sum;

