#!/usr/bin/env perl
use strict;
use warnings;

my %max = (red => 12, green => 13, blue => 14);
my $s = 0;
line: while (<ARGV>) {
    chomp;
    my ($game, $rounds) = ($_ =~ /Game (\d+): (.*)/);
    # print($game . "\n");
    my @rounds = split("; ", $rounds);
    for my $r (@rounds) {
        my @vals = split(", ", $r);
        for my $v (@vals) {
            my ($n, $c) = split(" ", $v);
            if ($n > $max{$c}) {
                next line;
            }
        }
    }
    $s += $game;
}
print("$s\n");