This page is still under construction.

Parts of this page are still being built. What you see may change.

King's Task

Interview

Time limit3sMemory limit512 MB

Summary
Given a permutation of 1 to 2n, find the fewest swaps of adjacent pairs or of the two halves needed to sort it, or report that sorting is impossible.
Level

Medium6 of 10

Topics
Graph, BFS, Simulation, Implementation
Solved
No attempts yet

Problem

A brave knight came to the king and asked for permission to marry the princess. The king knew the knight was brave, but he also wanted to know whether he was smart enough. So he gave him the following task.

There is a permutation pip_i of the numbers from 1 to 2n2n. You can perform two types of operations.

  1. Swap p1p_1 and p2p_2, p3p_3 and p4p_4, ..., p2n−1p_{2n-1} and p2np_{2n}.
  2. Swap p1p_1 and pn+1p_{n+1}, p2p_2 and pn+2p_{n+2}, ..., pnp_{n} and p2np_{2n}.

The task is to find the minimum number of operations needed to sort the given permutation.

The knight was not that smart, but he was charming, so the princess asks you to help him solve the king's task.

Input

The first line contains the integer nn (1≤n≤10001\le n\le 1000). The second line contains 2n2n integers pip_i, the permutation of the numbers from 1 to 2n2n.

Output

Print one integer, the minimum number of operations needed to sort the permutation. If the permutation cannot be sorted with these operations, print −1-1.

Hint

In the first example, the permutation can be sorted in three operations:

  1. Perform operation 1: 3,6,5,2,1,43, 6, 5, 2, 1, 4.
  2. Perform operation 2: 2,1,4,3,6,52, 1, 4, 3, 6, 5.
  3. Perform operation 1: 1,2,3,4,5,61, 2, 3, 4, 5, 6.

Examples3

  1. Example 1

    Input
    3
    6 3 2 5 4 1
    
    Expected output
    3
    
  2. Example 2

    Input
    2
    3 4 2 1
    
    Expected output
    -1
    
  3. Example 3

    Input
    4
    1 2 3 4 5 6 7 8
    
    Expected output
    0