#!/usr/bin/perl -w

use v5.16;
use strict;

my (%speed, %time, %resttime);
my %state;
my %dist;
my %score;

while (<>) {
	my ($name, $sp, $t, $r) = /(\w+) can fly (\d+) .* for (\d+) .*rest for (\d+) /;
	$speed{$name} = $sp;
	$time{$name} = $t;
	$resttime{$name} = $r;

	$state{$name} = $t;
	$score{$name} = 0;
};


my $time = 2503;

for (1 .. $time) {
	my $max;
	for my $n (keys %speed) {
		$dist{$n} += $speed{$n} if $state{$n} > 0;
		$state{$n} = $time{$n} if --$state{$n} <= -$resttime{$n};
		$max = $dist{$n} if !$max || $max < $dist{$n};
	}

	for my $n (keys %speed) {
		$score{$n}++ if $dist{$n} == $max;
	}
}

my $maxscore;
for my $n (keys %speed) {
	$maxscore = $score{$n} if !$maxscore || $score{$n} > $maxscore;
}

say $maxscore;


