logo AlgoBeat OnlineJudge
登录 注册

#212984. [NERC 2020] King's Task

内存限制:512 MiB 时间限制:3000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

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 of numbers from to . You can make two types of operations.

  • Swap and , and , ..., and .
  • Swap and , and , ..., and .

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.

输入格式

The first line contains the integer (). The second line contains integers --- the permutation of numbers from 1 to .

输出格式

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

3
6 3 2 5 4 1

样例输出 1

3

样例输入 2

2
3 4 2 1

样例输出 2

-1

样例输入 3

4
1 2 3 4 5 6 7 8

样例输出 3

0

数据范围与提示

In the first example, you can sort the permutation in three operations:

  • Make operation 1: .
  • Make operation 2: .
  • Make operation 1: .