#!/usr/bin/perl -w

use v5.38;

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

say $sum;

