#!/usr/bin/perl -w

use v5.36;
use strict;

my $i = 0;
my @list = map { chomp; [$i++, $_] } <>;

for my $idx (0 .. $#list) {
	my $i = 0;
	$i++ while $list[$i][0] != $idx;
	my $item = splice @list, $i, 1;
	my $dest = ($item->[1] + $i) % @list;
	splice @list, $dest, 0, $item;
}

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

say $sum;
