#!/usr/bin/perl

use v5.30;
use strict;

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

my %seen;
my $steps;
my $another;
while (1) {
	my $key = join(',', @banks);
	last if defined $another && $another eq $key;
	if ($seen{$key}++ && !defined $another) {
		$another = $key;
		$steps = 0;
	}
	$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;
