#!/usr/bin/perl -w

use strict;
use v5.30;

# my $in = 3004953;
my $in = shift;

my @elves = (1 .. $in);
my $now = 0;
while (@elves > 1) {
	my $steal = $now + int(@elves/2);
	$steal -= @elves if $steal > $#elves;
	say "now $now, steal $steal, total ", scalar @elves, ": ", join(' ', @elves);
	splice(@elves, $steal, 1);
	$now++ if $steal > $now;
	$now = 0 if $now > $#elves;
}
say $elves[0];
