local band = bit.band local FenwickTree = {} FenwickTree.create = function(self, n) self.n = n self.v = {} for i = 1, n do self.v[i] = 0 end end FenwickTree.add = function(self, pos, val) while pos <= self.n do self.v[pos] = self.v[pos] + val pos = pos + band(pos, -pos) end end FenwickTree.sum = function(self, r) local ret = 0 while 0 < r do ret = ret + self.v[r] r = r - band(r, -r) end return ret end FenwickTree.new = function(n) local obj = {} setmetatable(obj, {__index = FenwickTree}) obj:create(n) return obj end local ffi = require("ffi") local C = ffi.C ffi.cdef[[ long long atoll(const char*); ]] local function lltonumber(str) return C.atoll(str) end local n, m = io.read():match("(%d+) (%d+)") n = tonumber(n) m = lltonumber(m) local v = 0 local fw = FenwickTree.new(n) for i = 1, n do local p = io.read("*n") v = v + i - 1 - fw:sum(p) fw:add(p, 1) end local a = 0LL while a < 1LL * v do a = a + m end if (a - 1LL * v) % 2LL == 0LL then local ans = tostring(a):gsub("LL", "") print(ans) else if v % 2 == 1 then print(-1) else local ans = tostring(a + m):gsub("LL", "") print(ans) end end