#!/usr/bin/perl -w

use strict;

local $/ = "\n\n";
my @ranges;
for (split /\n/, <>) {
	my (@cls) = /\A(.*): (\d+)-(\d+) or (\d+)-(\d+)/;
	print "$2:$3,$4:$5.\n";
	push @ranges, [$2, $3], [$4, $5];
}

$_ = <>;
my @your = /(\d+)/g;
print "Your:", join("|", @your), "\n";

my $sum = 0;
# my @nearby;
for (split /\n/, <>) {
	next if !/\d/;
	my @n = /(\d+)/g;
	# print "nearby:", join("|", @n), "\n";
	# push @nearby, \@n;
	NUM:
	for my $num (@n) {
		for my $r (@ranges) {
			if ($num >= $r->[0] && $num <= $r->[1]) {
				next NUM;
			}
		}
		print "$num is invalid\n";
		$sum += $num;
	}
}

print "sum=$sum\n";
