#!/usr/bin/perl -w

use v5.30;
use strict;

my ($players, $last) = @ARGV;

my @l = (0);
my $cur = 0;

my %score;
for (1 .. $last) {
	my $player = $_ % $players;
	if ($_ % 23 == 0) {
		$score{$player} += $_;
		$cur -= 7;
		$cur += @l if $cur < 0;
		my ($val) = splice @l, $cur, 1;
		$score{$player} += $val;
	} else {
		my $dst = @l > 1 ? ($cur + 2) % @l : $cur + 1;
		$dst = @l if $dst == 0;
		splice @l, $dst, 0, $_;
		$cur = $dst;
	}
	say $_ if $_ % 100000 == 0;
	# say join(' ', $cur, @l);
}

use List::Util qw(max);
say max values %score;
	
	
