using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
static long[] LList => ReadLine().Split().Select(long.Parse).ToArray();
public static void Main()
{
Solve();
}
static void Solve()
{
var c = LList;
var (n, m) = ((int)c[0], c[1]);
var p = NList;
var ft = new FenwickTree(n + 2);
var rev = 0L;
for (var i = 0; i < n; ++i)
{
rev += ft.Sum(n + 1) - ft.Sum(p[i]);
ft.Add(p[i], 1);
}
var d = 0L;
if (rev != 0) d = (m + rev - 1) / m;
if (m * d % 2 == rev % 2) WriteLine(m * d);
else if (m % 2 == 1) WriteLine(m * (d + 1));
else WriteLine(-1);
}
class FenwickTree
{
int size;
long[] tree;
public FenwickTree(int size)
{
this.size = size;
tree = new long[size + 2];
}
public void Add(int index, int value)
{
++index;
for (var x = index; x <= size; x += (x & -x)) tree[x] += value;
}
/// 先頭からindexまでの和(include index)
public long Sum(int index)
{
++index;
var sum = 0L;
for (var x = index; x > 0; x -= (x & -x)) sum += tree[x];
return sum;
}
/// Sum(x) >= value となる最小のxを求める
// 各要素は非負であること
// sizeが2べきでない場合に正しく動く?
public int LowerBound(long value)
{
if (value < 0) return -1;
var x = 0;
var b = 1;
while (b * 2 <= size) b <<= 1;
for (var k = b; k > 0; k >>= 1)
{
if (x + k <= size && tree[x + k] < value)
{
value -= tree[x + k];
x += k;
}
}
return x;
}
}
}