#include <bits/stdc++.h>
#define debug cout<<"masuk\n";
using namespace std;
int main()
{
int n,k;
scanf("%d",&n);
int a[n];
int temp,temp2,temp3;
for(temp=0;temp<n;temp++)
{
scanf("%d",&a[temp]);
}
scanf("%d",&k);
vector<vector<int>> adj(n);
for(temp=0;temp<n;temp++)
{
int cnt=1;
for(temp2=0;temp2<n;temp2++)
{
if((temp2>=temp)&&(temp2<temp+k)&&(temp+k-cnt<n))
{
adj[temp2].push_back(temp+k-cnt);
cnt++;
}
else
{
adj[temp2].push_back(temp2);
}
}
}
/*for(temp=0;temp<n;temp++)
{
printf("%d contains : ",temp);
for(temp2=0;temp2<adj[temp].size();temp2++)
{
cout<<adj[temp][temp2]<<" ";
}
cout<<"\n";
}*/
int cnt=0;
for(temp=0;temp<n;temp++)
{
int source[n];
bool visited[n];
for(temp2=temp;temp2<n;temp2++)
{
if(a[temp2]==temp+1) break;
}
if(temp2==temp)
{
continue;
}
int start=temp2;
for(temp2=0;temp2<n;temp2++)
{
source[temp2]=-1;
visited[temp2]=false;
}
int pos=start;
queue<int> q;
q.push(pos);
while(!q.empty())
{
pos=q.front();
q.pop();
visited[pos]=true;
if(pos==temp) break;
for(temp2=0;temp2<adj[pos].size();temp2++)
{
int thenode=adj[pos][temp2];
bool cokor=true;
for(int tmp=temp;tmp<n;tmp++)
{
int check=adj[tmp][temp2];
if(check<temp) cokor=false;
}
if((!visited[thenode])&&(cokor))
{
source[thenode]=pos;
q.push(thenode);
}
}
}
if(source[temp]==-1)
{
cout<<-1<<"\n";
return 0;
}
vector<int> ans;
pos=temp;
while(source[pos]!=-1)
{
ans.push_back(pos);
pos=source[pos];
}
pos=start;
cnt+=ans.size();
for(temp2=ans.size()-1;temp2>=0;temp2--)
{
for(temp3=0;temp3<adj[pos].size();temp3++)
{
int thenode=adj[pos][temp3];
if(thenode==ans[temp2])
{
break;
}
}
int kursor=temp3;
int b[n];
for(temp3=0;temp3<n;temp3++) b[temp3]=a[temp3];
for(temp3=0;temp3<n;temp3++)
{
a[temp3]=b[adj[temp3][kursor]];
}
pos=ans[temp2];
}
//for(temp3=0;temp3<n;temp3++) cout<<a[temp3]<<" "; cout<<"\n";
}
cout<<cnt<<"\n";
}