King‘s Task

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.

  1. 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} p2n1 and p 2 n p_{2n} p2n.
  2. 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 1n1000). 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值