D. King’s Task
The brave Knight came to the King and asked permission to marry the princess. The King knew that the Knight was brave, but he also wanted to know if he was smart enough. So he asked him to solve the following task.
There is a permutation p i p_i pi of numbers from 1 to 2 n 2n 2n. You can make two types of operations.
- Swap p 1 p_1 p1 and p 2 p_2 p2, p 3 p_3 p3 and p 4 p_4 p4, …, p 2 n − 1 p_{2n-1} p2n−1 and p 2 n p_{2n} p2n.
- Swap p 1 p_1 p1 and p n + 1 p_{n+1} pn+1, p 2 p_2 p2 and p n + 2 p_{n+2} pn+2, …, p n p_{n} pn and p 2 n p_{2n} p2n.
The task is to find the minimal number of operations required to sort the given permutation.
The Knight was not that smart actually, but quite charming, so the princess asks you to help him to solve the King’s task.
Input
The first line contains the integer n n n ( 1 ≤ n ≤ 1000 1\le n\le 1000 1≤n≤1000). The second line contains 2 n 2n 2n integers p i p_i pi — the permutation of numbers from 1 to 2 n 2n 2n.
Output
Print one integer — the minimal number of operations required to sort the permutation. If it is impossible to sort the permutation using these operations, print − 1 -1 −1.
Example
Input
3
6 3 2 5 4 1
Output
3
Input
2
3 4 2 1
Output
-1
```## Input
```cpp
4
1 2 3 4 5 6 7 8
Output
0
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2010,INF=0x3f3f3f3f,mod=1e9+7;
typedef pair<int,int> PII;
int T=1;
int a[N];
int n;
void f1(){
for(int i=1;i<=n*2;i+=2) swap(a[i],a[i+1]);
}
void f2(){
for(int i=1;i<=n;i++) swap(a[i],a[i+n]);
}
int judge(){
int cnt=0;
for(int i=1;i<=n*2;i++) {
if(a[i]!=i) return false;
}
return true;
}
void solve(){
cin>>n;
for(int i=1;i<=n*2;i++) cin>>a[i];
if(judge()){
cout<<0<<endl;
return;
}
f1();
if(judge()){
cout<<1<<endl;
return;
}
f1();
f2();
if(judge()){
cout<<1<<endl;
return;
}
f2();
for(int i=1;i<=n*2;i++){
if(i%2) f2();
else f1();
if(judge()){
if(i>n)cout<<2*n-i<<endl;
else cout<<i<<endl;
break;
}
}
if(judge()) return;
cout<<-1<<endl;
}
signed main(){
while(T--){
solve();
}
return 0;
}