#!/usr/bin/perl

use v5.30;
use strict;

chomp (my @banks = split /\s+/, <>);

my %seen;
my $steps;
while (1) {
	my $key = join(',', @banks);
	last if $seen{$key};
	$steps++;
	my ($max, $maxi);
	for my $i (0 .. $#banks) {
		if ($banks[$i] > $max) {
			$max = $banks[$i];
			$maxi = $i;
		}
	}
	$banks[$maxi] = 0;
	while ($max--) {
		$maxi = 0 if ++$maxi > $#banks;
		$banks[$maxi]++;
	}
}
say $steps;
