#!/usr/bin/perl -w

use v5.30;
use strict;

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

my %prev = ( 0 => 0 );
my %next = ( 0 => 0 );
my $cur = 0;

my %score;
for (1 .. $last) {
	my $player = $_ % $players;
	if ($_ % 23 == 0) {
		$score{$player} += $_;
		$cur = $prev{$cur} for 1 .. 7;
		$score{$player} += $cur;
		$next{$prev{$cur}} = $next{$cur};
		$prev{$next{$cur}} = $prev{$cur};
		$cur = $next{$cur};
	} else {
		$cur = $next{$cur};
		my $n = $next{$cur};
		$next{$cur} = $_;
		$prev{$_} = $cur;
		$prev{$n} = $_;
		$next{$_} = $n;
		$cur = $_;
	}
	say $_ if $_ % 100000 == 0;
	# say join(' ', $cur, @l);
}

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