#!/usr/bin/perl -w

use v5.16;
use Data::Dumper;

my (@m, @v);
while (<>) {
	my @row = /-?\d+/g;
	push @m, [ @row ];
	push @v, [ 0, 0, 0];
}

for (1 .. 1000) {
	for my $m1 (0 .. $#m) {
	for my $m2 (0 .. $#m) {
		next if $m1 == $m2;
		for my $c (0 .. 2) {
			$v[$m1][$c]++ if $m[$m1][$c] < $m[$m2][$c];
			$v[$m1][$c]-- if $m[$m1][$c] > $m[$m2][$c];
		}
	} }
	for my $m1 (0 .. $#m) {
		for my $c (0 .. 2) {
			$m[$m1][$c] += $v[$m1][$c];
		}
	}
	print Dumper \@m, \@v;
}

my $sum;
for my $m1 (0 .. $#m) {
	my ($pot, $kin);
	for my $c (0 .. 2) {
		$pot += abs($m[$m1][$c]);
		$kin += abs($v[$m1][$c]);
	}
	$sum += $pot*$kin;
}

say $sum;
		


