#include using namespace std; const int mod = 1e9+7; const int maxn = 1e5 + 1; #define pii pair #define ll long long #define pq priority_queue #define se second #define fi first int n, k; struct node{ int id, val; }; vector a; void solve() { cin >> n; a.resize(n); for (int i = 0; i < n; i++){ cin >> a[i].val; a[i].id = i; } sort(a.begin(), a.end(), [](const node &x, const node &y) { return x.val < y.val; }); cin >> k; bool ok = true; int ans = 0; for (int i = 0; i < n; i++) { if((a[i].id - i)%k != 0) { ok = false; break; } ans += abs((a[i].id - i)/k); } if(!ok) cout << -1 << '\n'; else cout << ans/2 << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); solve(); return 0; }