#!/usr/bin/perl -w

use v5.30;
use strict;

my %prereqs;
my %steps;
while (<>) {
	my ($src, $dst) = /(\w) must be.* (\w) can/;
	$prereqs{$dst}->{$src} = 1;
	$steps{$src} = ord($src)-ord('A')+61;
	$steps{$dst} = ord($dst)-ord('A')+61;
}

my $time = 0;
while (%steps) {
	$time++;
	my @avail = sort grep { keys %{ $prereqs{$_} } == 0 } keys %steps;
	for my $step (splice @avail, 0, 5) {
		next if --$steps{$step} > 0;
		delete $steps{$step};
		for my $p (keys %prereqs) {
			delete $prereqs{$p}->{$step};
		}
	}
}

say $time;

