K-sorting
Sorting involves arranging a given array of numbers (or other objects) in either ascending or descending order. There are numerous variations of this task, many of which have highly efficient algorithms. A key parameter of these algorithms is the number of swaps needed to sort the array elements.
In this problem, we explore a variant called -sorting. In -sorting, you can perform a -swap, which allows you to swap the values of two array elements whose indices differ by exactly . For instance, consider the array [, , , , ] with . This array can be sorted in ascending order with two operations: after the first swap, the array becomes [, , , , ], and after the second swap, it becomes [, , , , ].
Given an array of integers , your task is to find the minimum number of -swaps required to sort this array in non-decreasing order.
Input
The first line contains an integer (). The second line contains integers ( for all from to ). The third line contains an integer ().
Output
If the array can be sorted in non-decreasing order using the described operations, output the minimum number of -swaps needed. If it is not possible to sort the array this way, output .