#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; vector a; bool dasap(vector &ans) { for (int i = 0; i < ans.size()- 1; i++) { if(a[i] > a[i + 1]) return false; } return true; } void solve() { cin >> n; a.resize(n); for (int i = 0; i < n; i++) cin >> a[i]; cin >> k; int ans = 0; for (int it = 0; it <= 10000; it++) { for (int i = 0; i < n - k; i++) { if(a[i] > a[i + k]) { swap(a[i], a[k + i]); ans++; } } } if(!dasap(a)) cout << -1 << '\n'; else cout << ans << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); solve(); return 0; }