var input = require('fs').readFileSync('/dev/stdin', 'utf8'); var lines = input.split("\n"); var n = parseInt(lines[0].split(" ")[0]); var k = parseInt(lines[0].split(" ")[1]); var A = lines[1].split(" "); var a = []; A.map(function(e) { a.push(parseInt(e)); }); var tmp; var count = 0; for (var i = 0; i < n; i++) { if (i === (n - 1)) { count = isSorted(a) ? count : -1; console.log(count); process.exit(); } if (a[i] > a[i + k]) { tmp = a[i]; a[i] = a[i + k]; a[i + k] = tmp; i = -1; count++; } } function isSorted(arr) { for (var i = 0; i < arr.length; i++) { if (arr[i] > arr[i+1]) { return false; } } return true; }