#!/usr/bin/perl

use v5.30;
use strict;

use List::Util qw(max);

my %layers = map { /(\d+): (\d+)/ } <>;
my $max_l = max keys %layers;

my $delay = -1;
DELAY:
while (1) {
	$delay++;
	for my $pos (0 .. $max_l) {
		next if !defined $layers{$pos};
		next if ($pos+$delay) % (2 * $layers{$pos} - 2);
		next DELAY;
	}
	last;
}

say $delay;

