#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin>>n;
int temp,temp2;
string s="";
int lol;
for(temp=0;temp<n;temp++)
{
cin>>lol;
s+=char(lol+48);
}
cin>>k;
string target=s;
sort(target.begin(),target.end());
//cout<<target<<"\n";
queue<pair<string,int>> q;
q.push(make_pair(s,0));
map<string,bool> visited;
while(!q.empty())
{
string st=q.front().first;
int cnt=q.front().second;
q.pop();
if(visited[st]) continue;
visited[st]=true;
if(st==target)
{
cout<<cnt<<"\n";
return 0;
}
cnt++;
for(temp=0;temp<=n-k;temp++)
{
string sst=st;
int troll=0;
for(temp2=temp;temp2<temp+k;temp2++)
{
sst[temp2]=st[temp+k-troll-1];
troll++;
}
//cout<<sst<<" "<<cnt<<"\n";
q.push(make_pair(sst,cnt));
}
}
cout<<-1<<"\n";
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpCnsKCWludCBuLGs7CgljaW4+Pm47CglpbnQgdGVtcCx0ZW1wMjsKCXN0cmluZyBzPSIiOwoJaW50IGxvbDsKCWZvcih0ZW1wPTA7dGVtcDxuO3RlbXArKykKCXsKCQljaW4+PmxvbDsKCQlzKz1jaGFyKGxvbCs0OCk7Cgl9CgljaW4+Pms7CglzdHJpbmcgdGFyZ2V0PXM7Cglzb3J0KHRhcmdldC5iZWdpbigpLHRhcmdldC5lbmQoKSk7CgkvL2NvdXQ8PHRhcmdldDw8IlxuIjsKCXF1ZXVlPHBhaXI8c3RyaW5nLGludD4+IHE7CglxLnB1c2gobWFrZV9wYWlyKHMsMCkpOwoJbWFwPHN0cmluZyxib29sPiB2aXNpdGVkOwoJd2hpbGUoIXEuZW1wdHkoKSkKCXsKCQlzdHJpbmcgc3Q9cS5mcm9udCgpLmZpcnN0OwoJCWludCBjbnQ9cS5mcm9udCgpLnNlY29uZDsKCQlxLnBvcCgpOwoJCWlmKHZpc2l0ZWRbc3RdKQljb250aW51ZTsKC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