#!/usr/bin/perl -w

use v5.38;
use List::Util qw(sum);

my $sum;
while (<>) {
	my @seq = reverse /-?\d+/g;
	my @rest;
	while (grep $_, @seq) {
		for my $i (0 .. $#seq-1) {
			$seq[$i] = $seq[$i+1]-$seq[$i];
		}
		push @rest, pop @seq;
	}
	$sum += sum @rest;
}

say $sum;

