<?php
error_reporting(-1);
$total=30; //number of people
$skip=5; //couplets
$key=$skip;
$M=$skip; //stopper. once reached M exit loop
$arrayOfStudents=range(1,$total,1);
end($arrayOfStudents);
$lastKey=key($arrayOfStudents); 
//last available student in each cycle. count($array) wont work since at the end of the cycle we have changed (decreased) array.length because of dropped students
//but this must not affect last student in the cycle (determined in the beginning of each cycle) since countdown was not interrupted or renewed after each drop

while (count($arrayOfStudents)>=$M){
  
    
    if ($key-1<=$lastKey) {
        unset($arrayOfStudents[$key-1]);
        $key+=$skip; 
       
    }
    
    elseif ($key-1>$lastKey) {
        $key=($key-1)-$lastKey;
        $arrayOfStudents=array_values($arrayOfStudents);
        end($arrayOfStudents);
        $lastKey=key($arrayOfStudents);
        unset($arrayOfStudents[$key-1]); 
        $key+=$skip;
       
    }

    
}

echo "Выигрышные места: " . implode(", ",$arrayOfStudents);
 
?>