#!/usr/bin/perl -w

use v5.36;
use strict;

chomp(my @list = <>);
my @perm = ( 0 .. $#list );

for my $idx (0 .. $#list) {
	my $move = $list[$idx];
	my $pos = 0;
	$pos++ while $perm[$pos] != $idx;
	splice(@perm, $pos, 1);
	my $dst = ($pos + $move) % @perm;
	splice(@perm, $dst, 0, $idx);
}

my @ordered = map { $list[$perm[$_]] } 0 .. $#list;

my $i = 0;
$i++ while $ordered[$i] != 0;
my $sum;
for (1 .. 3) {
	$i += 1000;
	$i %= @ordered;
	$sum += $ordered[$i];
}

say $sum;
