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

Swap $p_1$ and $p_2$, $p_3$ and $p_4$, ..., $p_{2n-1}$ and $p_{2n}$.

Swap $p_1$ and $p_{n+1}$, $p_2$ and $p_{n+2}$, ..., $p_{n}$ and $p_{2n}$.

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$ ($1\le n\le 1000$). The second line contains $2n$ integers $p_i$ — the permutation of numbers from 1 to $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$.


-----Examples-----

Input
3
6 3 2 5 4 1
Output
3
Input
2
3 4 2 1
Output
-1
Input
4
1 2 3 4 5 6 7 8
Output
0


-----Note-----

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

Make operation 1: $3, 6, 5, 2, 1, 4$.

Make operation 2: $2, 1, 4, 3, 6, 5$.

Make operation 1: $1, 2, 3, 4, 5, 6$.

Source: codeforces | Difficulty: EASY
Tags: Graph algorithms

Implementation

Complete search


Solutions:
def swap1(arr1):
	for i in range(0, len(arr1) - 1, 2):
		(arr1[i], arr1[i + 1]) = (arr1[i + 1], arr1[i])
	return arr1

def swap2(arr):
	for i in range(0, len(arr) // 2):
		(arr[i], arr[i + len(arr) // 2]) = (arr[i + len(arr) // 2], arr[i])
	return arr
n = int(input())
per = list(map(int, input().split()))
per1 = [] + per
mas = list(range(1, 2 * n + 1, 1))
i = 0
while per != mas and per1 != mas:
	if i > 2 * n:
		i = -1
		break
	if i % 2 == 0:
		per = swap1(per)
		per1 = swap2(per1)
	else:
		per = swap2(per)
		per1 = swap1(per1)
	i += 1
print(i)


from sys import stdin, stdout
from collections import defaultdict as dd
import math
input = stdin.readline
flush = stdout.flush

def o2(l):
	b = l[len(l) // 2:].copy() + l[:len(l) // 2].copy()
	return b

def o1(l):
	b = []
	for i in range(1, len(l) // 2 + 1):
		b.append(l[2 * i - 1])
		b.append(l[2 * i - 2])
	return b
tc = 1
for _ in range(tc):
	n = int(input())
	p = list(map(int, input().split()))
	ans = float('inf')
	x = p.copy()
	x.sort()
	if x == p:
		ans = -1
	q = p.copy()
	for i in range(n):
		if i % 2 == 0:
			q = o2(q)
		else:
			q = o1(q)
		if q == x:
			ans = min(ans, i)
			break
	for i in range(n):
		if i % 2 == 0:
			p = o1(p)
		else:
			p = o2(p)
		if p == x:
			ans = min(ans, i)
			break
	if ans == float('inf'):
		ans = -2
	print(ans + 1)


import sys
import math
from math import factorial, inf, gcd
from heapq import *
from functools import *
from itertools import *
from collections import *
from typing import *
from bisect import *
import random
sys.setrecursionlimit(10 ** 5)

def rarray():
	return [int(i) for i in input().split()]

def is_sorted(a):
	for i in range(1, len(a)):
		if a[i] <= a[i - 1]:
			return False
	return True

def g1(a):
	ans = []
	for i in range(0, len(a), 2):
		ans.append(a[i + 1])
		ans.append(a[i])
	return ans

def g2(a):
	n = len(a) // 2
	return a[n:] + a[:n]

def f1(a, f):
	for i in range(1000):
		if f:
			a = g1(a)
		else:
			a = g2(a)
		if is_sorted(a):
			return i + 1
		f = not f
	return inf
t = 1
for ii in range(t):
	n = int(input())
	a = rarray()
	if is_sorted(a):
		print(0)
	else:
		ans = min(f1(a, True), f1(a, False))
		print(ans if ans != inf else -1)


def rec(copy, t):
	count = 0
	while copy != list(sorted(arr)):
		if copy == arr and count:
			return -1
		if count % 2 == t:
			for i in range(n):
				(copy[i], copy[i + n]) = (copy[i + n], copy[i])
		else:
			for i in range(0, 2 * n, 2):
				(copy[i], copy[i + 1]) = (copy[i + 1], copy[i])
		count += 1
	return count
n = int(input())
arr = [int(x) for x in input().split()]
res1 = rec(arr.copy(), 0)
res2 = rec(arr.copy(), 1)
if res1 > -1 and res2 > -1:
	print(min(res1, res2))
else:
	print(max(res1, res2))


import sys
read = sys.stdin.readline
n = int(input())
a = [int(_) for _ in read().split()]
bound = 1000 * 2 + 1
inf = 10 ** 10

def solve(a):
	init = a[:]
	i = 0
	while True:
		if a == list(range(1, 2 * n + 1)):
			return i
		if i % 2 == 0:
			for j in range(0, 2 * n, 2):
				(a[j], a[j + 1]) = (a[j + 1], a[j])
		else:
			a = a[n:] + a[:n]
		if a == init:
			return inf
		i += 1
	return inf
ans = min(solve(a[:]), 1 + solve(a[n:] + a[:n]))
if ans >= inf:
	ans = -1
print(ans)


import sys

def p1(ls):
	for i in range(1, n + 1):
		temp = ls[2 * i - 1]
		ls[2 * i - 1] = ls[2 * i - 2]
		ls[2 * i - 2] = temp
	return ls

def p2(ls):
	ls1 = ls[n:] + ls[0:n]
	ls = ls1
	return ls

def check(ls, flag):
	if ls == sorted(ls):
		flag = 1
	return flag
n = int(input())
ls = []
count = 0
flag = 0
ls = list(map(int, input().split()))
lsc = ls.copy()
if check(ls, flag):
	print(count)
	sys.exit()
while True:
	ls = p1(ls)
	count += 1
	if check(ls, flag):
		flag = check(ls, flag)
		break
	if ls == lsc:
		break
	ls = p2(ls)
	count += 1
	if check(ls, flag):
		flag = check(ls, flag)
		break
	if ls == lsc:
		break
count1 = count
count = 0
flag1 = 0
ls.clear()
ls = lsc
while True:
	ls = p2(ls)
	count += 1
	if check(ls, flag1):
		flag1 = check(ls, flag1)
		break
	if ls == lsc:
		break
	ls = p1(ls)
	count += 1
	if check(ls, flag1):
		flag1 = check(ls, flag1)
		break
	if ls == lsc:
		break
count2 = count
if flag == 0 and flag1 == 0:
	print(-1)
else:
	print(min(count1, count2))


n = int(input())
a1 = [int(k) for k in input().split()]
b = list(range(1, 2 * n + 1))
a2 = list(a1)
z = 0
if a1 == b:
	print(0)
else:
	for i in range(2 * n):
		if a1 == b or a2 == b:
			print(i)
			break
		else:
			for j in range(n):
				if i % 2 == 0:
					(a1[2 * j], a1[2 * j + 1]) = (a1[2 * j + 1], a1[2 * j])
					(a2[j], a2[n + j]) = (a2[n + j], a2[j])
				else:
					(a2[2 * j], a2[2 * j + 1]) = (a2[2 * j + 1], a2[2 * j])
					(a1[j], a1[n + j]) = (a1[n + j], a1[j])
	else:
		print(-1)


n = int(input())
m = list(map(int, input().split()))
ms = sorted(m)
k = n
count0 = 0
count1 = 0
m1 = m.copy()
while k > 0:
	if m != ms:
		ppp = 0
		if m == ms:
			break
		for i in range(0, 2 * n, 2):
			(m[i], m[i + 1]) = (m[i + 1], m[i])
		count0 += 1
		if m == ms:
			break
		m = m[n:] + m[:n]
		count0 += 1
		if m == ms:
			break
		m1 = m1[n:] + m1[:n]
		count1 += 1
		if m1 == ms:
			break
		for i in range(0, 2 * n, 2):
			(m1[i], m1[i + 1]) = (m1[i + 1], m1[i])
		count1 += 1
		if m1 == ms:
			break
	else:
		break
	k -= 1
if m == ms:
	print(count0)
elif m1 == ms:
	print(count1)
else:
	print(-1)


def swapA(B):
	for i in range(0, 2 * n, 2):
		swapval = B[i]
		B[i] = B[i + 1]
		B[i + 1] = swapval
	return B

def swapB(C):
	L = C[0:n]
	C[0:n] = C[n:2 * n]
	C[n:2 * n] = L
	return C
n = int(input())
array = list(map(int, input().split()))
A = [i for i in range(1, 2 * n + 1)]
B = A[:]
count = 0
count1 = 0
flag = False
for i in range(2 * 2 * n):
	if A == array:
		flag = True
		break
	A = swapA(A)
	count += 1
	if A == array:
		flag = True
		break
	A = swapB(A)
	count += 1
	if A == array:
		flag = True
		break
for i in range(2 * 2 * n):
	if B == array:
		flag = True
		break
	B = swapB(B)
	count1 += 1
	if B == array:
		flag = True
		break
	B = swapA(B)
	count1 += 1
	if B == array:
		flag = True
		break
cnt = min(count, count1)
if flag == False:
	print(-1)
else:
	print(cnt)


import math, sys, bisect, heapq, os
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
from functools import lru_cache
int1 = lambda x: int(x) - 1

def input():
	return sys.stdin.readline().rstrip('\r\n')
aj = lambda : list(map(int, input().split()))

def list3d(a, b, c, d):
	return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
	print(['NO', 'YES'][c])

def y(c):
	print(['no', 'yes'][c])

def Yy(c):
	print(['No', 'Yes'][c])

def solve():

	def f1(B):
		for i in range(1, len(B), 2):
			(B[i], B[i - 1]) = (B[i - 1], B[i])
		return B

	def f2(B):
		i = 0
		j = len(B) // 2
		while j < len(B):
			(B[i], B[j]) = (B[j], B[i])
			i += 1
			j += 1
		return B
	(n,) = aj()
	A = aj()
	S = sorted(A)
	x = A.copy()
	y = f1(A)
	for i in range(1030):
		if S == x:
			print(i)
			exit(0)
		if S == y:
			print(i + 1)
			exit(0)
		if i % 2 == 0:
			x = f2(x)
			y = f2(y)
		else:
			x = f1(x)
			y = f1(y)
	print(-1)
try:
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
except:
	pass
solve()


n = int(input())
arr = list(map(int, input().split()))
arr2 = [x for x in arr]

def one(carr):
	for i in range(0, 2 * n, 2):
		tem = carr[i]
		carr[i] = carr[i + 1]
		carr[i + 1] = tem

def two(carr):
	for i in range(n):
		tem = carr[i]
		carr[i] = carr[i + n]
		carr[i + n] = tem

def test(carr):
	for i in range(2 * n):
		if carr[i] != i + 1:
			return False
	return True

def calc(carr, add0, add1):
	add = [add0, add1]
	use = -1
	bad = False
	for i in range(2 * n + 1):
		if carr[add[0] * i % (2 * n)] == 1:
			use = i
			break
	if use != -1:
		for i in range(2 * n):
			if carr[(i + add[i % 2] * use) % (2 * n)] != i + 1:
				bad = True
				break
		if not bad:
			return 2 * use
	return -1

def main():
	global n, arr, arr2
	if test(arr):
		print(0)
		return
	if n % 2 == 0:
		one(arr)
		if test(arr):
			print(1)
			return
		two(arr2)
		if test(arr2):
			print(1)
			return
		two(arr)
		if test(arr):
			print(2)
			return
		one(arr2)
		if test(arr2):
			print(2)
			return
	else:
		out = 100000
		tem = calc(arr, n - 1, n + 1)
		if tem != -1:
			out = min(out, tem)
		two(arr)
		tem = calc(arr, n - 1, n + 1)
		if tem != -1:
			out = min(out, tem + 1)
		tem = calc(arr2, n + 1, n - 1)
		if tem != -1:
			out = min(out, tem)
		one(arr2)
		tem = calc(arr2, n + 1, n - 1)
		if tem != -1:
			out = min(out, tem + 1)
		if out < 100000:
			print(out)
			return
	print(-1)
	return
main()


n = int(input())
arr = list(map(int, input().split()))
arr1 = arr[:]
l = [i for i in range(1, 2 * n + 1)]
d = 0
f = 0
k = 0
ans = 10 ** 20
while True:
	if arr == l:
		ans = min(ans, d)
		k = 0
		break
	if f % 2 == 0:
		for i in range(0, 2 * n, 2):
			c = arr[i]
			arr[i] = arr[i + 1]
			arr[i + 1] = c
	else:
		arr = arr[n:] + arr[:n]
	d += 1
	f += 1
	if d > 2 * n:
		k = 1
		print(-1)
		break
if k == 0:
	arr = arr1[:]
	d = 0
	f = 1
	while True:
		if arr == l:
			ans = min(ans, d)
			k = 0
			break
		if f % 2 == 0:
			for i in range(0, 2 * n, 2):
				c = arr[i]
				arr[i] = arr[i + 1]
				arr[i + 1] = c
		else:
			arr = arr[n:] + arr[:n]
		d += 1
		f += 1
		if d > 2 * n:
			k = 1
			print(-1)
			break
	if k == 0:
		print(ans)


n = int(input())
a = (*map(int, input().split()),)
c = 9000000000.0
m = 2 * n
for j in [0, 1]:
	b = list(a)
	d = 0
	while b[-1] < m:
		b = [b[n:] + b[:n], [b[i ^ 1] for i in range(m)]][j]
		j ^= 1
		d += 1
	if b < list(range(1, m + 2)):
		c = min(d, c)
print([-1, c][c < 9000000000.0])


def check(a: list) -> bool:
	return all([a[i] == i + 1 for i in range(len(a))])

def swapa(a: list, n: int):
	for i in range(n):
		(a[2 * i], a[2 * i + 1]) = (a[2 * i + 1], a[2 * i])

def swapb(a: list, n: int):
	for i in range(n):
		(a[i], a[i + n]) = (a[i + n], a[i])

def get_answera(a: list, n: int) -> int:
	if check(a):
		return 0
	ans = 0
	for i in range(2 * n):
		if i & 1:
			swapa(a, n)
		else:
			swapb(a, n)
		ans += 1
		if check(a):
			return ans
	return 2 * n + 1

def get_answerb(a: list, n: int) -> int:
	if check(a):
		return 0
	ans = 0
	for i in range(2 * n):
		if i & 1:
			swapb(a, n)
		else:
			swapa(a, n)
		ans += 1
		if check(a):
			return ans
	return 2 * n + 1

def main():
	n = int(input())
	a = [int(x) for x in input().split()]
	import copy
	b = copy.deepcopy(a)
	ans = min(get_answera(a, n), get_answerb(b, n))
	if ans == 2 * n + 1:
		ans = -1
	print(ans)
main()


import sys
from collections import defaultdict
memo = {}

def search(cur_op, nums):
	global n, target
	state = tuple(nums)
	if state in memo:
		return memo[state]
	memo[state] = float('inf')
	l = []
	for i in range(0, 2 * n - 1, 2):
		l.append(nums[i + 1])
		l.append(nums[i])
	new_list = [*nums[n:], *nums[:n]]
	memo[state] = min(memo[state], search(cur_op + 1, new_list) + 1)
	memo[state] = min(memo[state], search(cur_op + 1, l) + 1)
	return memo[state]
n = int(sys.stdin.readline().strip())
nums = list(map(int, sys.stdin.readline().strip().split()))
target = sorted(nums)
memo[tuple(target)] = 0
search(0, nums)
operation = memo[tuple(nums)]
if operation == float('inf'):
	print(-1)
else:
	print(operation)


import math
from collections import *
import math

def solve():
	n = int(input())
	a = [int(i) for i in input().split()]
	if a == sorted(a):
		print(0)
		return
	b = a.copy()
	(c, d) = (0, 0)
	for i in range(max(2 * n, 4)):
		if i % 2 == 0:
			for j in range(0, 2 * n - 1, 2):
				(b[j], b[j + 1]) = (b[j + 1], b[j])
		else:
			for j in range(n):
				(b[j], b[n + j]) = (b[n + j], b[j])
		if b == sorted(b):
			c = i + 1
			break
	b = a.copy()
	for i in range(max(2 * n, 4)):
		if i % 2 != 0:
			for j in range(0, 2 * n - 1, 2):
				(b[j], b[j + 1]) = (b[j + 1], b[j])
		else:
			for j in range(n):
				(b[j], b[n + j]) = (b[n + j], b[j])
		if b == sorted(b):
			d = i + 1
			break
	if c == 0 and d == 0:
		print(-1)
	else:
		print(min(c, d))
solve()


def op1(arr, n):
	for i in range(0, 2 * n, 2):
		(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])

def op2(arr, n):
	for i in range(n):
		(arr[i], arr[i + n]) = (arr[i + n], arr[i])
n = int(input())
arr = [int(d) for d in input().split(' ')]
arr1 = [i + 1 for i in range(2 * n)]
arr2 = [i + 1 for i in range(2 * n)]
b = True
step = 0
for i in range(2 * n):
	if arr1 == arr or arr2 == arr:
		print(min(step, 2 * n - step))
		exit()
	if b:
		op1(arr1, n)
		op2(arr2, n)
		b = not b
	else:
		op1(arr2, n)
		op2(arr1, n)
		b = not b
	step += 1
print(-1)


n = int(input())
m = input().split()
u = []
for i in range(2 * n):
	u.append(int(m[i]))
ww = u[0:]
s = sorted(u)
num1 = 1
num2 = 1
theFlag = False
if u == s:
	print(0)
else:
	w = 0
	c = 0
	x = u
	flag = False
	g = 0
	r = u
	while True:
		if w % 2 == 0:
			a = u[0:n]
			u[0:n] = u[n:2 * n]
			u[n:2 * n] = a
			f1 = True
		else:
			for i in range(n):
				a = u[2 * i]
				u[2 * i] = u[2 * i + 1]
				u[2 * i + 1] = a
			f2 = True
		w += 1
		c += 1
		if u == s:
			break
		g += 1
		if g == 3000:
			print(-1)
			flag = True
			theFlag = True
			break
	if theFlag == False:
		if flag == False:
			num1 = c
		u = []
		u = ww
		w = 1
		c = 0
		x = ww
		flag = False
		g = 0
		r = ww
		while True:
			if w % 2 == 0:
				a = u[0:n]
				u[0:n] = u[n:2 * n]
				u[n:2 * n] = a
				f1 = True
			else:
				for i in range(n):
					a = u[2 * i]
					u[2 * i] = u[2 * i + 1]
					u[2 * i + 1] = a
				f2 = True
			w += 1
			c += 1
			if u == s:
				break
			g += 1
		if flag == False:
			num2 = c
		if flag == False:
			if num1 <= num2:
				print(num1)
			else:
				print(num2)


n = int(input())
y = list(map(int, input().split()))
l = y
a = 0
x = 0
flag = 0
while x <= 2 * n:
	if l == sorted(l):
		a = 1
		break
	if flag == 0:
		t = []
		for i in range(1, 2 * n, 2):
			t.append(l[i])
			t.append(l[i - 1])
		flag = 1
		l = t
		x += 1
	else:
		b = l[:n]
		c = l[n:]
		l = c + b
		flag = 0
		x += 1
if a == 0:
	print(-1)
else:
	l = y
	a = 0
	x1 = 0
	flag = 0
	while x1 <= 2 * n:
		if l == sorted(l):
			print(min(x1, x))
			a = 1
			break
		if flag == 1:
			t = []
			for i in range(1, 2 * n, 2):
				t.append(l[i])
				t.append(l[i - 1])
			flag = 0
			l = t
			x1 += 1
		else:
			b = l[:n]
			c = l[n:]
			l = c + b
			flag = 1
			x1 += 1


n = int(input())
a = list(map(int, input().split()))
a1 = a[:]
(c, x) = (0, 0)
b = sorted(a)
while a != b and c <= 2 * n:
	if x == 0:
		for i in range(0, 2 * n, 2):
			(a[i], a[i + 1]) = (a[i + 1], a[i])
		c += 1
		x = 1
	else:
		for i in range(n):
			(a[i], a[n + i]) = (a[n + i], a[i])
		c += 1
		x = 0
(c1, x) = (0, 0)
while a1 != b and c1 <= 2 * n:
	if x == 0:
		for i in range(n):
			(a1[i], a1[n + i]) = (a1[n + i], a1[i])
		c1 += 1
		x = 1
	else:
		for i in range(0, 2 * n, 2):
			(a1[i], a1[i + 1]) = (a1[i + 1], a1[i])
		c1 += 1
		x = 0
if min(c, c1) < 2 * n:
	print(min(c, c1))
else:
	print(-1)


import os, sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
try:
	(sys.stdin, sys.stdout) = (open('in.txt', 'r'), open('out.txt', 'w'))
except:
	pass
ii1 = lambda : int(sys.stdin.readline().strip())
is1 = lambda : sys.stdin.readline().strip()
iia = lambda : list(map(int, sys.stdin.readline().strip().split()))
isa = lambda : sys.stdin.readline().strip().split()
mod = int(1000000000.0 + 7)
from copy import deepcopy

def op1(aa, n):
	for i in range(n):
		(aa[i], aa[i + n]) = (aa[i + n], aa[i])

def op2(aa, n):
	for i in range(0, 2 * n, 2):
		(aa[i], aa[i + 1]) = (aa[i + 1], aa[i])

def main():
	n = ii1()
	arr = iia()
	temp = deepcopy(arr)
	ans = []
	lis = list(range(1, 2 * n + 1))
	if temp == lis:
		return 0
	for i in range(max(2 * n, 4)):
		op1(temp, n)
		if temp == lis:
			ans.append(2 * i + 1)
			break
		op2(temp, n)
		if temp == lis:
			ans.append(2 * i + 2)
			break
	temp = arr
	for i in range(max(2 * n, 4)):
		op2(temp, n)
		if temp == lis:
			ans.append(2 * i + 1)
			break
		op1(temp, n)
		if temp == lis:
			ans.append(2 * i + 2)
			break
	if ans:
		return min(ans)
	return -1
print(main())


n = int(input().strip())
nums = list(map(int, input().strip().split(' ')))

def adj(arr):
	for i in range(n):
		tmp = arr[2 * i]
		arr[2 * i] = arr[2 * i + 1]
		arr[2 * i + 1] = tmp
	return arr

def half(arr):
	for i in range(n):
		tmp = arr[i]
		arr[i] = arr[i + n]
		arr[i + n] = tmp
	return arr

def check(arr):
	for i in range(2 * n):
		if arr[i] != i + 1:
			return False
	return True

def solve(nums):
	poss = True
	for i in range(1, n):
		if (nums[2 * i] - nums[0]) % 2 != 0:
			poss = False
			break
	if not poss:
		print(-1)
		return 0
	if n % 2 == 0:
		cnt = 0
		if nums[0] % 2 == 0:
			cnt += 1
			nums = adj(nums)
		if nums[0] != 1:
			cnt += 1
			nums = half(nums)
		if check(nums):
			print(cnt)
		else:
			print(-1)
		return 0
	cnt = 0
	pt = 0
	while cnt < 2 * n:
		if pt == nums[pt] - 1:
			break
		pt = nums[pt] - 1
		if cnt % 2 == 0:
			nums = half(nums)
		else:
			nums = adj(nums)
		cnt += 1
	if check(nums):
		print(min(cnt, 2 * n - cnt))
	else:
		print(-1)
	return 0
solve(nums)


def split(list):
	return [char for char in list]

def per1(n, a):
	b = a
	for x in range(n):
		(a[2 * x], a[2 * x + 1]) = (b[2 * x + 1], b[2 * x])
	return a

def per2(n, a):
	b = a
	for x in range(n):
		(a[x], a[x + n]) = (b[x + n], b[x])
	return a
n = int(input())
a = list(map(int, input().split()))
a1 = sorted(a)
i = 0
count = 0
if len(a) % 4 == 0:
	for q in range(4):
		if a != a1:
			if i == 0:
				per1(n, a)
				i = 1
			else:
				per2(n, a)
				i = 0
			count = count + 1
		else:
			break
	if count < 4:
		print(min(count, 4 - count))
	else:
		print(-1)
else:
	for q in range(len(a)):
		if a != a1:
			if i == 0:
				per1(n, a)
				i = 1
			else:
				per2(n, a)
				i = 0
			count = count + 1
		else:
			break
	if count < len(a):
		print(min(count, len(a) - count))
	else:
		print(-1)


def getNum(nums):
	return int(''.join(map(str, nums)))

def op1(n, nums):
	for i in range(0, 2 * n, 2):
		(nums[i], nums[i + 1]) = (nums[i + 1], nums[i])
	return nums

def op2(n, nums):
	for i in range(0, n, 1):
		(nums[i], nums[i + n]) = (nums[i + n], nums[i])
	return nums

def solve(n, nums):
	moves = 0
	sortedNums = sorted(nums[:])
	list1 = nums[:]
	list2 = nums[:]
	while list1[:] != sortedNums and list2 != sortedNums:
		list1 = op1(n, list1[:])
		list2 = op2(n, list2[:])
		moves += 1
		if list1[:] == sortedNums or list2[:] == sortedNums:
			break
		list1 = op2(n, list1[:])
		list2 = op1(n, list2[:])
		moves += 1
		if moves > n:
			return -1
	return moves
t = int(input())
ts = list(map(int, input().split(' ')))
print(solve(t, ts))


def main():
	n = int(input())
	list_p = list(map(int, input().split()))
	result = sorted(list_p)
	list_should_be_visited = [list_p]
	list_dist = [0]
	list_visited = list()
	while len(list_should_be_visited) != 0:
		permutation_current = list_should_be_visited.pop(0)
		dist_current = list_dist.pop(0)
		if permutation_current == result:
			print(dist_current)
			return
		permutation_1 = [None for _ in range(2 * n)]
		for i in range(n):
			(permutation_1[2 * i], permutation_1[2 * i + 1]) = (permutation_current[2 * i + 1], permutation_current[2 * i])
		permutation_2 = [None for _ in range(2 * n)]
		for i in range(n):
			(permutation_2[i], permutation_2[n + i]) = (permutation_current[n + i], permutation_current[i])
		if permutation_1 == result or permutation_2 == result:
			print(dist_current + 1)
			return
		else:
			if permutation_1 not in list_visited and permutation_1 not in list_should_be_visited:
				list_should_be_visited.append(permutation_1)
				list_dist.append(dist_current + 1)
			if permutation_2 not in list_visited and permutation_2 not in list_should_be_visited:
				list_should_be_visited.append(permutation_2)
				list_dist.append(dist_current + 1)
		list_visited.append(permutation_current)
	print(-1)
main()


def swap1(l):
	for i in range(0, len(l), 2):
		a = l[i]
		l[i] = l[i + 1]
		l[i + 1] = a

def swap2(l):
	for i in range(0, len(l) // 2):
		a = l[i]
		l[i] = l[i + len(l) // 2]
		l[i + len(l) // 2] = a
n = int(input())
l = list(map(int, input().split()))
original = l.copy()
s = l.copy()
s.sort()
count = 0
while True:
	if s == l:
		break
	swap2(l)
	count += 1
	if original == l:
		print('-1')
		count = -1
		break
	if s == l:
		break
	swap1(l)
	count += 1
	if original == l:
		print('-1')
		count = -1
		break
c = 0
l = original.copy()
if count == -1:
	exit(0)
while True:
	if s == l:
		break
	swap1(l)
	c += 1
	if original == l:
		print('-1')
		break
	if s == l:
		break
	swap2(l)
	c += 1
	if original == l:
		print('-1')
		break
print(min(count, c))


n = None
INF = 1000000000.0

def calc(cur, dest, op, start, first=True):
	global n
	if cur == dest:
		return 0
	if cur == start and (not first):
		return INF
	if op == 0:
		nxt = cur ^ 1
	elif cur < n:
		nxt = cur + n
	else:
		nxt = cur - n
	return 1 + calc(nxt, dest, op ^ 1, start, False)

def read():
	n = int(input())
	array = list(map(int, input().split()))
	return (n, array)

def main():
	global n
	(n, array) = read()
	first = array[0]
	start_0 = calc(0, first - 1, 0, 0)
	start_1 = calc(0, first - 1, 1, 0)
	if start_0 < start_1:
		first_op = 0
		cost = start_0
	else:
		first_op = 1
		cost = start_1
	if cost >= INF:
		print('-1')
		return
	have_answer = True
	for (i, el) in enumerate(array):
		cur_cost = calc(i, el - 1, first_op, i)
		if cur_cost != cost:
			have_answer = False
			break
	if not have_answer:
		print(-1)
	else:
		print(cost)
main()


import sys
sys.setrecursionlimit(10000)
n = int(input())
pp = list(map(int, input().split()))

def op1(p):
	for i in range(n):
		(p[2 * i], p[2 * i + 1]) = (p[2 * i + 1], p[2 * i])
	return p

def op2(p):
	for i in range(n):
		(p[i], p[i + n]) = (p[i + n], p[i])
	return p

def rec(p, i):
	global R
	s = p.copy()
	p1 = op1(p)
	tp1 = tuple(p1)
	if not tp1 in R or R[tp1] > i + 1:
		R[tp1] = i + 1
		rec(p1, i + 1)
	p2 = op2(s)
	tp2 = tuple(p2)
	if not tp2 in R or R[tp2] > i + 1:
		R[tp2] = i + 1
		rec(p2, i + 1)
p = [i + 1 for i in range(2 * n)]
R = {}
R[tuple(p)] = 0
rec(p, 0)
if tuple(pp) in R:
	print(R[tuple(pp)])
else:
	print(-1)


def jum(x, n):
	for i in range(0, n, 2):
		p = x[i]
		x[i] = x[i + 1]
		x[i + 1] = p
	return x

def shift(x, n):
	x = x[n // 2:] + x[:n // 2]
	return x
n = int(input())
a = list(map(int, input().split()))
req = list(range(1, 2 * n + 1))
if n % 2 == 0:
	k = 4
else:
	k = 2 * n
i = 0
while i < k and a != req:
	if i & 1 == 1:
		a = shift(a, 2 * n)
	else:
		a = jum(a, 2 * n)
	i = i + 1
if a == req:
	print(min(i, k - i))
else:
	print(-1)


n = int(input())
l = list(map(int, input().split()))
sorted_l = sorted(l)

def swap(arr):
	swapped = []
	for i in range(0, 2 * n, 2):
		swapped.append(arr[i + 1])
		swapped.append(arr[i])
	return swapped

def realoc(arr):
	realoc = []
	for i in range(0, 2 * n):
		realoc.append(arr[(i + n) % (2 * n)])
	return realoc
if l == sorted_l:
	print(0)
else:
	count_swap = count_realoc = 0
	arr = swap(l)
	flag_swap = False
	while True:
		count_swap += 1
		if arr == sorted_l:
			break
		if arr == l:
			count_swap = -1
			break
		if flag_swap:
			arr = swap(arr)
		else:
			arr = realoc(arr)
		flag_swap = not flag_swap
	if count_swap != -1:
		arr = realoc(l)
		flag_realoc = False
		while True:
			count_realoc += 1
			if arr == sorted_l:
				break
			if flag_realoc:
				arr = realoc(arr)
			else:
				arr = swap(arr)
			flag_realoc = not flag_realoc
	print(min(count_swap, count_realoc))


n = int(input())
l = list(map(int, input().split()))
l2 = l.copy()
c = l.copy()
o = l.copy()
c.sort()
flag = 1
(cnt, var) = (0, 0)
while c != l:
	if flag:
		cnt += 1
		flag = 1 - flag
		for i in range(2 * n - 1):
			if i % 2 == 0:
				temp = l[i]
				l[i] = l[i + 1]
				l[i + 1] = temp
	else:
		cnt += 1
		flag = 1 - flag
		for i in range(n):
			temp = l[i]
			l[i] = l[n + i]
			l[n + i] = temp
	if o == l:
		break
cnt2 = cnt
cnt = 0
flag = 0
while c != l2:
	if flag:
		cnt += 1
		flag = 1 - flag
		for i in range(2 * n - 1):
			if i % 2 == 0:
				temp = l2[i]
				l2[i] = l2[i + 1]
				l2[i + 1] = temp
	else:
		cnt += 1
		flag = 1 - flag
		for i in range(n):
			temp = l2[i]
			l2[i] = l2[n + i]
			l2[n + i] = temp
	if o == l2:
		break
if c == l or c == l2:
	print(min(cnt2, cnt))
else:
	print('-1')


from __future__ import division, print_function
import math
import os
import sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def o1(l, n):
	for i in range(0, n + n, 2):
		(l[i], l[i + 1]) = (l[i + 1], l[i])
	return l

def o2(l, n):
	for i in range(0, n):
		(l[i], l[n + i]) = (l[n + i], l[i])
	return l

def main():
	n = int(input())
	l = list(map(int, input().split()))
	a1 = 0
	a2 = 0
	k1 = k2 = True
	l1 = list(l)
	l1.sort()
	l2 = list(l)
	while l != l1:
		if a1 % 2 == 0:
			l = o1(l, n)
		else:
			l = o2(l, n)
		a1 += 1
		if a1 > 10000:
			k1 = False
			break
	while l2 != l1:
		if a2 % 2 == 0:
			l2 = o2(l2, n)
		else:
			l2 = o1(l2, n)
		a2 += 1
		if a2 > 10000:
			k2 = False
			break
	if k1 and k2:
		print(min(a1, a2))
	else:
		print(-1)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


def op1(arra, n):
	for i in range(0, 2 * n, 2):
		(arra[i], arra[i + 1]) = (arra[i + 1], arra[i])

def op2(arra, n):
	for j in range(n):
		(arra[j], arra[j + n]) = (arra[j + n], arra[j])
n = int(input())
ar = list(map(int, input().split()))
arr1 = [i for i in range(1, 2 * n + 1)]
arr2 = [i for i in range(1, 2 * n + 1)]
count = 0
test = True
for i in range(2 * n):
	if ar == arr1 or ar == arr2:
		print(count)
		break
	if test:
		op1(arr1, n)
		op2(arr2, n)
		test = not test
	else:
		op1(arr2, n)
		op2(arr1, n)
		test = not test
	count += 1
else:
	print(-1)


from itertools import chain
import math

def _is_sorted(seq):
	return all((seq[i - 1] <= seq[i] for i in range(1, len(seq))))

def _iter_states(p_seq, swap_halfs_first):
	p_seq = tuple(p_seq)
	n = len(p_seq) // 2
	assert len(p_seq) == 2 * n
	yield p_seq
	if swap_halfs_first:
		p_seq = p_seq[n:] + p_seq[:n]
		yield p_seq
	del swap_halfs_first
	for i in range(1024):
		p_seq = chain.from_iterable(zip(p_seq[1::2], p_seq[0::2]))
		p_seq = tuple(p_seq)
		yield p_seq
		p_seq = p_seq[n:] + p_seq[:n]
		yield p_seq

def _check_min_cost(p_seq, swap_halfs_first):
	cost = 0
	for state in _iter_states(p_seq, swap_halfs_first):
		if _is_sorted(state):
			return cost
		cost += 1
	return math.inf

def f(p_seq):
	p_seq = tuple(p_seq)
	return min(_check_min_cost(p_seq, swap_halfs_first=False), _check_min_cost(p_seq, swap_halfs_first=True))

def read_ints():
	return map(int, input().split())
(n,) = read_ints()
p_seq = tuple(read_ints())
assert len(p_seq) == 2 * n
del n
result = f(p_seq)
if math.isinf(result):
	result = -1
print(result)


n = int(input())
l = list(map(int, input().split()))
l1 = l.copy()
l2 = l.copy()
(c, c1) = (0, 0)
ans = 0
f = True
while l != l1 or f:
	f = False
	if l1 == sorted(l):
		break
	if c % 2 == 0:
		for i in range(1, 2 * n, 2):
			(l1[i], l1[i - 1]) = (l1[i - 1], l1[i])
	else:
		for i in range(n):
			(l1[i], l1[n + i]) = (l1[n + i], l1[i])
	c += 1
f = True
while l != l2 or f:
	f = False
	if l2 == sorted(l):
		break
	if c1 % 2 != 0:
		for i in range(1, 2 * n, 2):
			(l2[i], l2[i - 1]) = (l2[i - 1], l2[i])
	else:
		for i in range(n):
			(l2[i], l2[n + i]) = (l2[n + i], l2[i])
	c1 += 1
if l == l1 and c != 0:
	print(-1)
else:
	print(min(c, c1))


n = int(input())
arr = map(int, input().split())
arr = list(arr)
arr2 = arr.copy()
count = 0
count2 = 0
max_steps = 0
if n % 2 == 0:
	max_steps = 4
else:
	max_steps = 2 * n
if arr != sorted(arr):
	while arr != sorted(arr):
		if count == max_steps:
			count = -1
			break
		for i in range(0, 2 * n - 1, 2):
			temp = arr[i]
			arr[i] = arr[i + 1]
			arr[i + 1] = temp
		count += 1
		if arr == sorted(arr):
			break
		for i in range(0, n):
			temp = arr[i]
			arr[i] = arr[i + n]
			arr[i + n] = temp
		count += 1
if arr2 != sorted(arr):
	while arr2 != sorted(arr2):
		if count2 == max_steps:
			count2 = -1
			break
		for i in range(0, n):
			temp = arr2[i]
			arr2[i] = arr2[i + n]
			arr2[i + n] = temp
		count2 += 1
		if arr2 == sorted(arr):
			break
		for i in range(0, 2 * n - 1, 2):
			temp = arr2[i]
			arr2[i] = arr2[i + 1]
			arr2[i + 1] = temp
		count2 += 1
if count <= count2:
	print(count)
else:
	print(count2)


import sys

class kings_task:

	def __init__(self):
		self.occured_states = []

	def check_valid(self, inp_arr, res_arr):
		(left_steps, right_steps) = (0, 0)
		if inp_arr not in self.occured_states:
			self.occured_states.append(inp_arr)
			temp_state = inp_arr.copy()
			for i in range(0, len(inp_arr) - 1, 2):
				(temp_state[i], temp_state[i + 1]) = (temp_state[i + 1], temp_state[i])
			if temp_state == res_arr:
				return 1
			else:
				left_steps += self.check_valid(temp_state, res_arr)
			temp_state = inp_arr.copy()
			(temp_state[:int(len(temp_state) / 2)], temp_state[int(len(temp_state) / 2):len(temp_state)]) = (temp_state[int(len(temp_state) / 2):len(temp_state)], temp_state[:int(len(temp_state) / 2)])
			if temp_state == res_arr:
				return 1
			else:
				right_steps += self.check_valid(temp_state, res_arr)
			left_steps += 1 if left_steps != sys.maxsize else 0
			right_steps += 1 if right_steps != sys.maxsize else 0
			return min(left_steps, right_steps)
		else:
			return sys.maxsize
n = int(input())
arr = [int(i) for i in input().split()]
task_obj = kings_task()
output_arr = arr.copy()
output_arr.sort()
min_steps = task_obj.check_valid(arr, output_arr) if arr != output_arr else 0
print(min_steps if min_steps != sys.maxsize else -1, end='')


from sys import stdin
input = stdin.readline

def is_sorted(a, n):
	for i in range(1, n):
		if a[i] < a[i - 1]:
			return False
	return True

def operation1(a, n):
	ans = []
	for i in range(n):
		if i % 2 == 0:
			ans.append(a[i + 1])
		else:
			ans.append(a[i - 1])
	return ans

def operation2(a, n):
	ans = a[n // 2:] + a[:n // 2]
	return ans
n = int(input())
p = [int(x) for x in input().split()]
f = p[0]
ans = -1
i = 0
while f != p[0] or i == 0:
	if ans == -1 and is_sorted(p, 2 * n):
		ans = i
	if i % 2 == 0:
		p = operation1(p, 2 * n)
	else:
		p = operation2(p, 2 * n)
	i += 1
if ans != -1:
	print(min(ans, i - ans))
else:
	print(-1)


def rev(above, dp, i, j, n):
	for k in range(n):
		(dp[i][j + k], dp[i][j + n + k]) = (above[n + k], above[k])

def swap(above, dp, i, j, n):
	for k in range(0, 2 * n, 2):
		(dp[i][j + k], dp[i][j + k + 1]) = (above[k + 1], above[k])
n = int(input())
if n % 2:
	size = n
else:
	size = n // 2
abovel = [i for i in range(1, 2 * n + 1)]
abover = abovel[:]
dp = [[0 for _ in range(4 * n)] for _ in range(size)]
for i in range(size):
	if not i % 2:
		swap(abovel, dp, i, 0, n)
		rev(abover, dp, i, 2 * n, n)
	else:
		rev(abovel, dp, i, 0, n)
		swap(abover, dp, i, 2 * n, n)
	abovel = dp[i][:2 * n]
	abover = dp[i][2 * n:]
p = list(map(int, input().split()))
if p == [i for i in range(1, 2 * n + 1)]:
	print(0)
else:
	for i in range(size):
		if p == dp[i][:2 * n] or p == dp[i][2 * n:]:
			print(i + 1)
			break
	else:
		print(-1)


def check(l):
	for i in range(2 * n):
		if l[i] != i + 1:
			return 0
	return 1
n = int(input())
o = tuple(map(int, input().split()))
l = list(o)
k = 0
flag = 0
while k <= n:
	if check(l):
		break
	else:
		k += 1
		if flag == 0:
			for i in range(n):
				t = l[i]
				l[i] = l[i + n]
				l[i + n] = t
			flag = 1
		else:
			for i in range(0, 2 * n, 2):
				t = l[i]
				l[i] = l[i + 1]
				l[i + 1] = t
			flag = 0
ans = -1
if check(l):
	ans = k
l = list(o)
k = 0
flag = 1
while k <= n + 1:
	if check(l):
		break
	else:
		k += 1
		if flag == 0:
			for i in range(n):
				t = l[i]
				l[i] = l[i + n]
				l[i + n] = t
			flag = 1
		else:
			for i in range(0, 2 * n, 2):
				t = l[i]
				l[i] = l[i + 1]
				l[i + 1] = t
			flag = 0
if check(l) == 0 and ans == -1:
	print(-1)
elif check(l) == 0:
	print(ans)
elif ans == -1:
	print(k)
else:
	print(min(ans, k))


n = int(input())
arr = list(map(int, input().split()))
steps = 2 * n if n % 2 else 4
ans = -1
for i in range(steps):
	if arr == sorted(arr):
		ans = i
		break
	if i % 2:
		for j in range(0, n):
			(arr[j], arr[j + n]) = (arr[j + n], arr[j])
	else:
		for j in range(0, 2 * n, 2):
			(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
print(min(ans, steps - ans))


n = int(input())
a = list(map(int, input().split()))
b = a.copy()
boo = False
res = 10 ** 7
if a == sorted(a):
	res = min(0, res)
	boo = True
if 1:
	ans = 0
	for i in range(n):
		ans += 1
		if i % 2 == 0:
			for j in range(0, 2 * n, 2):
				(a[j], a[j + 1]) = (a[j + 1], a[j])
			if a == sorted(a):
				res = min(ans, res)
				boo = True
				break
		else:
			for j in range(n):
				(a[j], a[j + n]) = (a[j + n], a[j])
			if a == sorted(a):
				res = min(ans, res)
				boo = True
				break
ans1 = 10 ** 7
if 1:
	ans1 = 0
	a = []
	a = b.copy()
	for i in range(n):
		ans1 += 1
		if i % 2 == 0:
			for j in range(n):
				(a[j], a[j + n]) = (a[j + n], a[j])
			if a == sorted(a):
				res = min(ans1, res)
				boo = True
				break
		else:
			for j in range(0, 2 * n, 2):
				(a[j], a[j + 1]) = (a[j + 1], a[j])
			if a == sorted(a):
				res = min(ans1, res)
				boo = True
				break
if boo:
	print(res)
else:
	print(-1)


def swap1(p1, n):
	for i in range(0, 2 * n, 2):
		(p1[i], p1[i + 1]) = (p1[i + 1], p1[i])
		i += 1

def swap2(p2, n):
	(p2[0:n], p2[n:]) = (p2[n:], p2[0:n])

def check(p3, n):
	max = -1
	for i in range(2 * n):
		if max > int(p3[i]):
			return False
		if max < int(p3[i]):
			max = int(p3[i])
	return True

def main():
	n = int(input())
	m = input().split()
	m2 = m.copy()
	ans = 0
	for i in range(2 * n):
		if check(m, n) or check(m2, n):
			print(ans)
			return
		if i % 2 == 0:
			swap1(m, n)
			swap2(m2, n)
		else:
			swap1(m2, n)
			swap2(m, n)
		ans += 1
	print(-1)
	return
main()


import copy
n = int(input())
tmp = input()
num = [int(_) for _ in tmp.split(' ')]
(FAC, MOD) = (171, int(1e+18))
vis = {}

def cal_hash(nums):
	ret = 0
	for i in nums:
		ret = (ret * FAC + i) % MOD
	return ret
ans_idx = num[0] - 1
ans_hash = cal_hash([_ + 1 for _ in range(2 * n)])
(head, tail) = (0, 1)
queue = [(num, 0, 0, -1)]
vis[cal_hash(num)] = 1

def op1(nums, idx):
	ret = copy.deepcopy(nums)
	for i in range(0, n):
		(ret[i * 2 + 1], ret[i * 2]) = (ret[i * 2], ret[i * 2 + 1])
	return (ret, idx ^ 1)

def op2(nums, idx):
	tmp1 = nums[n:]
	tmp1.extend(nums[:n])
	return (tmp1, idx - n if idx >= n else n + idx)
tmp2 = cal_hash(num)
vis[tmp2] = 1
if tmp2 == ans_hash:
	print(0)
	exit(0)
while head < tail:
	(cur, cnt, idx, last) = queue[head]
	if cnt > 2 * n:
		break
	head += 1
	if idx == ans_idx:
		if cal_hash(cur) == ans_hash:
			print(cnt)
			exit(0)
		break
	if last != 1:
		(o1, has) = op1(cur, idx)
		if has not in vis:
			queue.append((o1, cnt + 1, has, 1))
			vis[has] = 1
			tail += 1
	if last != 2:
		(o2, has) = op2(cur, idx)
		if has not in vis:
			queue.append((o2, cnt + 1, has, 2))
			vis[has] = 1
			tail += 1
print(-1)


def main():
	n = int(input())
	n *= 2
	arr = readIntArr()

	def perform1():
		for i in range(1, n, 2):
			temp = arr[i]
			arr[i] = arr[i - 1]
			arr[i - 1] = temp

	def perform2():
		for i in range(n // 2):
			temp = arr[i]
			arr[i] = arr[i + n // 2]
			arr[i + n // 2] = temp

	def check():
		for i in range(n):
			if arr[i] != i + 1:
				return False
		return True
	ok = False
	oneNext = True
	ans = -1
	if n // 2 % 2 == 0:
		for steps in range(4):
			if check():
				ok = True
				break
			if oneNext:
				perform1()
			else:
				perform2()
			oneNext = not oneNext
		if ok:
			ans = min(steps, 4 - steps)
	else:
		for steps in range(n):
			if check():
				ok = True
				break
			if oneNext:
				perform1()
			else:
				perform2()
			oneNext = not oneNext
		if ok:
			ans = min(steps, n - steps)
	print(ans)
	return
import sys
input = sys.stdin.buffer.readline

def oneLineArrayPrint(arr):
	print(' '.join([str(x) for x in arr]))

def multiLineArrayPrint(arr):
	print('\n'.join([str(x) for x in arr]))

def multiLineArrayOfArraysPrint(arr):
	print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))

def readIntArr():
	return [int(x) for x in input().split()]

def makeArr(defaultVal, dimensionArr):
	dv = defaultVal
	da = dimensionArr
	if len(da) == 1:
		return [dv for _ in range(da[0])]
	else:
		return [makeArr(dv, da[1:]) for _ in range(da[0])]

def queryInteractive(x, y):
	print('? {} {}'.format(x, y))
	sys.stdout.flush()
	return int(input())

def answerInteractive(ans):
	print('! {}'.format(ans))
	sys.stdout.flush()
inf = float('inf')
MOD = 10 ** 9 + 7
for _abc in range(1):
	main()


n = int(input())
a = list(map(int, input().split()))
z = 0
m = a.copy()
c = 0
while a[0] != 1 and m[0] != 1:
	for i in range(0, 2 * n, 2):
		(a[i], a[i + 1]) = (a[i + 1], a[i])
	c += 1
	if a[0] == 1:
		break
	a = a[n:] + a[:n]
	c += 1
	m = m[n:] + m[:n]
	z += 1
	if m[0] == 1:
		break
	for i in range(0, 2 * n, 2):
		(m[i], m[i + 1]) = (m[i + 1], m[i])
	z += 1
for i in range(2 * n - 1):
	if a[i] < a[i + 1]:
		pass
	else:
		c = -1
	if m[i] < m[i + 1]:
		pass
	else:
		z = -1
	if z == -1 and c == -1:
		break
if z == -1 and c == -1:
	print(c)
elif z == -1 and c >= 0:
	print(c)
elif z >= 0 and c == -1:
	print(z)
else:
	print(min(z, c))


import sys

def op1(n, x):
	for i in range(0, n):
		u = 2 * i
		v = 2 * i + 1
		t1 = x[u]
		x[u] = x[v]
		x[v] = t1
	return x

def op2(n, x):
	for i in range(0, n):
		u = i
		v = i + n
		t1 = x[u]
		x[u] = x[v]
		x[v] = t1
	return x

def cmp(n, a, b):
	for i in range(2 * n):
		if a[i] != b[i]:
			return False
	return True

def main():
	n = int(input())
	m = 2 * n
	x = [i for i in range(m)]
	a = list(map(int, input().split()))
	for i in range(m):
		a[i] = a[i] - 1
	steps = 0
	if cmp(n, a, x):
		print(steps)
		return 0
	ans = m * 3
	for i in range(m):
		if i % 2 == 0:
			x = op1(n, x)
		if i % 2 == 1:
			x = op2(n, x)
		if cmp(n, a, x):
			steps = i + 1
			ans = min(steps, ans)
	x = [i for i in range(m)]
	for i in range(m):
		if i % 2 == 1:
			x = op1(n, x)
		if i % 2 == 0:
			x = op2(n, x)
		if cmp(n, a, x):
			steps = i + 1
			ans = min(steps, ans)
	if ans <= m:
		print(ans)
	else:
		print(-1)
input = sys.stdin.readline
main()


def ii():
	return int(input())

def li():
	return [int(i) for i in input().split()]

def op1(a, n):
	for i in range(1, 2 * n, 2):
		(a[i], a[i + 1]) = (a[i + 1], a[i])

def op2(a, n):
	for i in range(1, n + 1):
		(a[i], a[n + i]) = (a[n + i], a[i])

def check(a):
	for i in range(1, len(a)):
		if a[i] != i:
			return 0
	return 1
n = ii()
a = li()
b = [-1]
a.insert(0, -1)
for i in range(1, 2 * n + 1):
	b.append(a[i])
ans = -1
for i in range(n + 1):
	if check(a) == 1:
		ans = i
		break
	if i % 2 == 0:
		op1(a, n)
	else:
		op2(a, n)
ans1 = -1
for i in range(n + 1):
	if check(b) == 1:
		ans1 = i
		break
	if i % 2 == 1:
		op1(b, n)
	else:
		op2(b, n)
if ans == -1 and ans1 == -1:
	print(-1)
elif ans == -1 and ans1 != -1:
	print(ans1)
elif ans != -1 and ans1 == -1:
	print(ans)
else:
	print(min(ans, ans1))


def func(x, t, n):
	if t == 1:
		for i in range(0, 2 * n - 1, 2):
			(x[i], x[i + 1]) = (x[i + 1], x[i])
	if t == 2:
		x = x[n:] + x[:n]
	return x
for u in range(1):
	n = int(input())
	x = [int(w) for w in input().split()]
	y = sorted(x)
	z = x[:]
	w = x[:]
	if x == y:
		print(0)
	else:
		if n % 2:
			s = 2 * n
		else:
			s = 4
		t = 1
		f = False
		a1 = 0
		for i in range(s):
			a1 += 1
			if t == 1:
				x = func(x, t, n)
				t = 2
			else:
				x = func(x, t, n)
				t = 1
			if x == z:
				a1 = -1
				f = True
				break
			if x == y:
				break
		t = 2
		f = False
		a2 = 0
		for i in range(s):
			a2 += 1
			if t == 1:
				w = func(w, t, n)
				t = 2
			else:
				w = func(w, t, n)
				t = 1
			if w == z:
				a2 = -1
				f = True
				break
			if w == y:
				break
		print(min(a1, a2))


from sys import stdin, stdout
from math import floor, gcd, fabs, factorial, fmod, sqrt, inf, log
from collections import defaultdict as dd, deque
from heapq import merge, heapify, heappop, heappush, nsmallest
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def inp():
	return stdin.readline().strip()

def iinp():
	return int(inp())

def out(var, end='\n'):
	stdout.write(str(var) + '\n')

def outa(*var, end='\n'):
	stdout.write(' '.join(map(str, var)) + end)

def lmp():
	return list(mp())

def mp():
	return map(int, inp().split())

def smp():
	return map(str, inp().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(m, val) for j in range(n)]

def remadd(x, y):
	return 1 if x % y else 0

def ceil(a, b):
	return (a + b - 1) // b

def isprime(x):
	if x <= 1:
		return False
	if x in (2, 3):
		return True
	if x % 2 == 0:
		return False
	for i in range(3, int(sqrt(x)) + 1, 2):
		if x % i == 0:
			return False
	return True

def rev(arr):
	n = len(arr)
	return arr[n // 2:] + arr[:n // 2]

def shuff(arr):
	n = len(arr)
	arr1 = l1d(n)
	for i in range(n):
		if i % 2 == 0:
			arr1[i + 1] = arr[i]
		else:
			arr1[i - 1] = arr[i]
	return arr1
n = iinp()
arr = lmp()
sarr = sorted(arr)
if sarr == arr:
	print(0)
else:
	a1 = arr.copy()
	a2 = arr.copy()
	flg = False
	for i in range(1, n + 1):
		tmp = a1.copy()
		a1 = rev(a2)
		a2 = shuff(tmp)
		if sarr in [a1, a2]:
			flg = True
			break
	print(i if flg else -1)


from collections import defaultdict
import bisect
import sys
import math
import os
import random
import re

def load_sys():
	return sys.stdin.readlines()

def load_local():
	with open('input.txt', 'r') as f:
		input = f.readlines()
	return input

def type1(n, arr):
	ans = []
	for i in range(0, 2 * n, 2):
		ans.append(arr[i + 1])
		ans.append(arr[i])
	return ans

def type2(n, arr):
	return arr[n:] + arr[:n]

def f(n, arr):
	if arr == sorted(arr):
		return 0
	ans = 1 << 32
	mx = 4 if n % 2 == 0 else 2 * n
	A = arr[:]
	for i in range(mx):
		A = type1(n, A) if i % 2 == 0 else type2(n, A)
		if A == sorted(A):
			ans = min(ans, i + 1)
	A = arr[:]
	for i in range(mx):
		A = type2(n, A) if i % 2 == 0 else type1(n, A)
		if A == sorted(A):
			ans = min(ans, i + 1)
	return ans if ans != 1 << 32 else -1
input = load_sys()
(n, arr) = (int(input[0]), [int(x) for x in input[1].split(' ')])
print(f(n, arr))


import math as m

def p1(arr):
	n = len(arr)
	new = []
	for i in range(0, n, 2):
		new += [arr[i + 1], arr[i]]
	return new

def p2(arr):
	n = len(arr) // 2
	return arr[n:] + arr[:n]

def isSorted(arr):
	prev = arr[0]
	for i in arr[1:]:
		if i < prev:
			return False
		prev = i
	return True

def logic(arr):
	n = len(arr)
	if isSorted(arr):
		return 0
	a = arr[:]
	c = 0
	while not isSorted(a):
		if c == n:
			c += 1
			break
		c += 1
		a = p1(a)
		if isSorted(a):
			break
		c += 1
		a = p2(a)
	d = 0
	while not isSorted(arr):
		if d == n:
			d += 1
			break
		d += 1
		arr = p2(arr)
		if isSorted(arr):
			break
		d += 1
		arr = p1(arr)
	x = min(c, d)
	if x > n:
		return -1
	return x
input()
arr = [int(x) for x in input().split()]
print(logic(arr))


def sorted(permutation):
	for i in range(0, len(permutation) - 1):
		if permutation[i] > permutation[i + 1]:
			return False
	return True

def move1(permutation):
	result = [0] * len(permutation)
	for i in range(0, len(permutation), 2):
		result[i] = permutation[i + 1]
		result[i + 1] = permutation[i]
	return result

def move2(permutation):
	n = len(permutation)
	result = [0] * n
	m = n // 2
	for i in range(0, m):
		result[i] = permutation[m + i]
		result[m + i] = permutation[i]
	return result

def search(permutation, n=0):
	sort = permutation
	for i in range(n, len(permutation)):
		if sorted(sort):
			return i
		elif i % 2 == 0:
			sort = move1(sort)
		else:
			sort = move2(sort)
	return -1

def main(permutation):
	res1 = search(permutation, n=0)
	res2 = search(permutation, n=1)
	if res2 > 0:
		res2 -= 1
	if res1 < 0 and res2 < 0:
		return -1
	if res1 < 0 or res2 < 0:
		return max(res1, res2)
	else:
		return min(res1, res2)
import sys
for line in sys.stdin:
	numbers = [int(x) for x in line.rstrip().split()]
print(main(numbers))


n = int(input())
ls = list(map(int, input().split()))
ideal = list(range(1, 2 * n + 1))
arr = [ideal]
new = []
old = ideal
j = 1
while new != ideal:
	if j % 2 == 0:
		new = old[n:] + old[:n]
		old = new
		arr.append(new)
		j += 1
	else:
		new = []
		for i in range(0, n):
			new.append(old[2 * i + 1])
			new.append(old[2 * i])
		old = new
		arr.append(new)
		j += 1
if ls in arr:
	if arr.index(ls) > len(arr) / 2:
		print(len(arr) - arr.index(ls) - 1)
	else:
		print(arr.index(ls))
else:
	print(-1)


import sys
input = sys.stdin.readline

def inp():
	return int(input())

def st():
	return input().rstrip('\n')

def lis():
	return list(map(int, input().split()))

def ma():
	return map(int, input().split())
t = 1
while t:
	t -= 1
	n = inp()
	a = lis()
	c = a[:]
	mo = 0
	b = sorted(a)
	mo1 = 0
	while 1:
		if b == a:
			break
		for i in range(n):
			(a[i], a[i + n]) = (a[i + n], a[i])
		mo += 1
		if b == a:
			break
		if c == a:
			break
		for i in range(0, 2 * n, 2):
			(a[i], a[i + 1]) = (a[i + 1], a[i])
		mo += 1
		if b == a:
			break
		if c == a:
			break
	a = c[:]
	while 1:
		if b == a:
			break
		for i in range(0, 2 * n, 2):
			(a[i], a[i + 1]) = (a[i + 1], a[i])
		mo1 += 1
		if b == a:
			break
		if c == a:
			print(-1)
			exit(0)
		for i in range(n):
			(a[i], a[i + n]) = (a[i + n], a[i])
		mo1 += 1
		if b == a:
			break
		if c == a:
			print(-1)
			exit(0)
	print(min(mo, mo1))


n = input()
line = input()
nums = line.split(' ')

def isSorted(arr):
	lastNum = int(arr[0])
	for i in arr:
		if int(i) < lastNum:
			return False
		lastNum = int(i)
	return True

def op1(arr):
	for i in range(0, len(arr) - 1, 2):
		temp = arr[i]
		arr[i] = arr[i + 1]
		arr[i + 1] = temp
	return arr

def op2(arr):
	n = int(len(arr) / 2)
	return arr[n:] + arr[0:n]
count = 0
arr = nums[:]
last_op = 1
is_sort = False
final_count = 9999
for i in range(len(nums)):
	if isSorted(arr):
		is_sort = True
		final_count = count
		break
	if last_op == 2:
		arr = op1(arr)
		last_op = 1
		count += 1
		continue
	if last_op == 1:
		arr = op2(arr)
		last_op = 2
	count += 1
count2 = 0
arr = nums[:]
last_op = 2
for i in range(len(nums)):
	if isSorted(arr):
		is_sort = True
		final_count = min(count2, final_count)
		break
	if last_op == 2:
		arr = op1(arr)
		last_op = 1
		count2 += 1
		continue
	if last_op == 1:
		arr = op2(arr)
		last_op = 2
	count2 += 1
if is_sort:
	print(final_count)
else:
	print(-1)


def pare_reverse(m, n):
	for i in range(n):
		(m[2 * i], m[2 * i + 1]) = (m[2 * i + 1], m[2 * i])

def half_reverse(m, n):
	for i in range(n):
		(m[i], m[n + i]) = (m[n + i], m[i])
n = int(input())
m = list(map(int, input().split()))
m_copy = [m[i] for i in range(len(m))]
m_copy_2 = [m[i] for i in range(len(m))]
s = sorted(m)
i = 0
while m_copy != s:
	if i % 2 == 0:
		pare_reverse(m_copy, n)
	else:
		half_reverse(m_copy, n)
	i += 1
	if m_copy == m:
		print(-1)
		exit()
j = 0
while m_copy_2 != s:
	if j % 2 == 0:
		half_reverse(m_copy_2, n)
	else:
		pare_reverse(m_copy_2, n)
	j += 1
print(min(i, j))


n = int(input())
l = list(map(int, input().split()))
flag = 0
ans = []
for i in range(2 * n):
	ans.append(i + 1)
if l[0] == 1:
	if ans != l:
		flag = 1
	if flag == 0:
		print(0)
elif n % 2 == 0:
	if l[0] == 2:
		for i in range(n):
			(ans[2 * i], ans[2 * i + 1]) = (ans[2 * i + 1], ans[2 * i])
		if ans != l:
			flag = 1
		if flag == 0:
			print(1)
	elif l[0] == n + 1:
		for i in range(n):
			(ans[i], ans[n + i]) = (ans[n + i], ans[i])
		if ans != l:
			flag = 1
		if flag == 0:
			print(1)
	else:
		for i in range(n):
			(ans[2 * i], ans[2 * i + 1]) = (ans[2 * i + 1], ans[2 * i])
		for i in range(n):
			(ans[i], ans[n + i]) = (ans[n + i], ans[i])
		if ans != l:
			flag = 1
		if flag == 0:
			print(2)
else:
	i = 0
	c = 0
	while 1:
		if l[i] == 1:
			break
		c += 1
		i += 1
		if l[i] == 1:
			break
		c += 1
		i += n
		if l[i] == 1:
			break
		c += 1
		i += 1
		if l[i] == 1:
			break
		c += 1
		i -= n
	f = 0
	while f < c:
		if f < c:
			f += 1
			for i in range(n):
				(ans[2 * i], ans[2 * i + 1]) = (ans[2 * i + 1], ans[2 * i])
		if f < c:
			f += 1
			for i in range(n):
				(ans[i], ans[n + i]) = (ans[n + i], ans[i])
		if f < c:
			f += 1
			for i in range(n):
				(ans[2 * i], ans[2 * i + 1]) = (ans[2 * i + 1], ans[2 * i])
		if f < c:
			f += 1
			for i in range(n):
				(ans[i], ans[n + i]) = (ans[n + i], ans[i])
	if ans != l:
		flag = 1
	if flag == 0:
		print(min(c, 2 * n - c))
if flag == 1:
	print(-1)


import sys, functools, collections, bisect, math, heapq
input = sys.stdin.readline
sys.setrecursionlimit(200000)
mod = 10 ** 9 + 7

def issort(arr):
	for i in range(1, len(arr)):
		if arr[i] < arr[i - 1]:
			return False
	return True
for _ in range(1):
	n = int(input())
	arr = list(map(int, input().strip().split()))
	arrc = arr.copy()
	if issort(arr):
		print(0)
		continue
	if n % 2:
		for z in range(n):
			for i in range(0, 2 * n, 2):
				(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
			if issort(arr):
				x = 2 * z + 1
				if x <= n:
					print(x)
				else:
					print(2 * n - x)
				break
			for i in range(n):
				(arr[i], arr[i + n]) = (arr[i + n], arr[i])
			if issort(arr):
				x = 2 * z + 2
				if x <= n:
					print(x)
				else:
					print(2 * n - x)
				break
		else:
			print(-1)
			continue
	else:
		for z in range(3):
			for i in range(0, 2 * n, 2):
				(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
			if issort(arr):
				x = 2 * z + 1
				if x <= 2:
					print(x)
				else:
					print(4 - x)
				break
			for i in range(n):
				(arr[i], arr[i + n]) = (arr[i + n], arr[i])
			if issort(arr):
				x = 2 * z + 2
				if x <= 2:
					print(x)
				else:
					print(4 - x)
				break
		else:
			print(-1)
			continue


import time
n = int(input())
p = list(map(int, input().split()))

def opa(p):
	pp = [p[i + 1 - 2 * (i % 2)] for i in range(len(p))]
	return pp

def opb(p):
	n = len(p) // 2
	pp = p[n:]
	pp.extend(p[:n])
	return pp
goal = list(range(1, 2 * n + 1))
if p == goal:
	print(0)
else:
	runn = 0
	(pa, pb) = (p, p)
	while runn < n:
		(pa, pb) = (opa(pb), opb(pa))
		runn += 1
		if pa == goal or pb == goal:
			print(runn)
			runn = n + 1
	if runn == n:
		print(-1)


input()
n = [int(n, 0) for n in input().split()]
m = n[:]
on = n[:]
m.sort()
count = -1
size = 0

def same(m, n):
	for i in range(len(m)):
		if m[i] != n[i]:
			return False
	return True

def swap(l, mode=0):
	if mode == 0:
		for i in range(0, len(l), 2):
			t = l[i]
			l[i] = l[i + 1]
			l[i + 1] = t
	else:
		for i in range(0, len(l) // 2):
			t = l[i]
			l[i] = l[len(l) // 2 + i]
			l[len(l) // 2 + i] = t
while True:
	if count == -1 and same(m, n):
		count = size
	swap(n, size & 1)
	size += 1
	if same(on, n):
		break
if count < 0:
	print(-1)
else:
	print(min(size - count, count))


def pone(a):
	for i in range(0, len(a), 2):
		(a[i], a[i + 1]) = (a[i + 1], a[i])
	return a

def ptwo(a):
	for i in range(0, len(a) // 2):
		(a[i], a[len(a) // 2 + i]) = (a[len(a) // 2 + i], a[i])
	return a

def onetwo(n, l):
	for i in range(4 * n):
		if i % 2 == 0:
			l = pone(l)
			if l == ans:
				return i + 1
		else:
			l = ptwo(l)
			if l == ans:
				return i + 1
	return -1

def twoone(n, l):
	for i in range(4 * n):
		if i % 2 == 0:
			l = ptwo(l)
			if l == ans:
				return i + 1
		else:
			l = pone(l)
			if l == ans:
				return i + 1
	return -1
n = int(input())
l = list(map(int, input().split()))
ans = [x for x in range(1, 2 * n + 1)]
if ans == l:
	print(0)
else:
	ans1 = onetwo(n, l[:])
	ans2 = twoone(n, l[:])
	if ans1 == -1 or ans2 == -1:
		print(-1)
	else:
		print(min(ans1, ans2))


from sys import maxsize
N = int(input())
org_arr = list(map(int, input().split()))
sorted_arr = list(range(1, 2 * N + 1))
cost = maxsize
pos = False
for swap in range(2):
	arr = org_arr.copy()
	tmp_cost = 0
	while arr[0] != 1:
		if swap:
			arr = arr[N:] + arr[:N]
		else:
			for idx in range(0, 2 * N - 1, 2):
				(arr[idx], arr[idx + 1]) = (arr[idx + 1], arr[idx])
		swap = not swap
		tmp_cost += 1
	if arr == sorted_arr:
		pos = True
		cost = min(tmp_cost, cost)
if pos:
	print(cost)
else:
	print(-1)


n = int(input())
li = list(map(int, input().split()))
sli = list(range(1, 2 * n + 1))
ans = float('inf')
m = sli[-1]
for j in range(2):
	ali = li[:]
	cnt = 0
	while ali[-1] != m:
		if j:
			for i in range(0, 2 * n, 2):
				(ali[i], ali[i + 1]) = (ali[i + 1], ali[i])
		else:
			ali = ali[n:] + ali[:n]
		j = not j
		cnt += 1
	if ali == sli:
		ans = min(cnt, ans)
if ans == float('inf'):
	print(-1)
else:
	print(ans)


import copy
n = int(input())
l = list(map(int, input().split()))
ls = sorted(l)
la = l.copy()
cnt = 0
while l != ls:
	if cnt >= max(2 * n, 4):
		print(-1)
		break
	elif l[0] < l[n]:
		for i in range(0, 2 * n, 2):
			a = l[i]
			b = l[i + 1]
			l[i] = b
			l[i + 1] = a
		cnt += 1
	else:
		for i in range(n):
			a = l[i]
			b = l[n + i]
			l[i] = b
			l[i + n] = a
		cnt += 1
else:
	print(cnt)


from sys import stdin, stdout
from collections import Counter, deque
import math
from copy import deepcopy

def gcd(a, b):
	if b == 0:
		return a
	else:
		return gcd(b, a % b)

def SieveOfEratosthenes(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 1
	prime[0] = False
	prime[1] = False
	ans = []
	for p in range(n + 1):
		if prime[p]:
			ans.append(p)
	return ans
coprime1 = {6: 100003, 5: 10007, 4: 1009, 3: 101, 2: 11, 1: 2, 7: 1000003, 8: 10000019, 9: 100000007}
coprime2 = {8: 10000169, 9: 100000049, 7: 1000033, 6: 100019, 5: 10009, 4: 1013, 3: 103, 2: 13, 1: 3}

def writ(ss):
	stdout.write(str(ss) + '\n')
mod = 10 ** 9 + 7
n = int(stdin.readline())
arr = list(map(int, stdin.readline().split()))
arr1 = sorted(arr)
tr = arr1.index(arr[0])
ans = 0
st = [[0]]
vis = [False] * (2 * n)
vis[0] = True
while st:
	temp = []
	while st:
		lis = st.pop()
		ele = lis[-1]
		if ele == tr:
			break
		if ele % 2 == 0:
			if vis[ele + 1] == False:
				temp.append(lis + [ele + 1])
				vis[ele + 1] = True
		elif vis[ele - 1] == False:
			temp.append(lis + [ele - 1])
			vis[ele - 1] = True
		if ele < n:
			if vis[ele + n] == False:
				temp.append(lis + [ele + n])
				vis[ele + n] = True
		elif vis[ele - n] == False:
			temp.append(lis + [ele - n])
			vis[ele - n] = True
	if ele == tr:
		break
	st = temp
	ans += 1
arn = deepcopy(arr)
lis.pop(0)
curr = 0
for i in range(len(lis)):
	if abs(curr - lis[i]) > 1:
		for j in range(n):
			temp = arn[j]
			arn[j] = arn[j + n]
			arn[j + n] = temp
	else:
		for j in range(0, 2 * n, 2):
			temp = arn[j]
			arn[j] = arn[j + 1]
			arn[j + 1] = temp
	curr = lis[i]
if arn == arr1:
	print(ans)
else:
	print(-1)


n = int(input())
p = list(map(int, input().split()))
q = p[:]
p1 = p[:]
r = p[:]
q.sort()
i1 = 0
while p != q:
	if i1 % 2 == 0:
		for j in range(0, 2 * n, 2):
			temp = p[j + 1]
			p[j + 1] = p[j]
			p[j] = temp
	else:
		p = p[n:] + p[0:n]
	i1 += 1
	if p == r:
		break
i2 = 0
while p1 != q:
	if i2 % 2 == 1:
		for j in range(0, 2 * n, 2):
			temp = p1[j + 1]
			p1[j + 1] = p1[j]
			p1[j] = temp
	else:
		p1 = p1[n:] + p1[0:n]
	i2 += 1
	if p1 == r:
		break
i1 = min(i1, i2)
if n % 2 == 0 and i1 == 4:
	print('-1')
elif n % 2 == 1 and i1 == 2 * n:
	print('-1')
else:
	print(i1)


n = int(input())
matriz = list(map(int, input().split()))
listaA = []
listaB = []
for i in range(n * 2):
	listaA.append(i + 1)
	listaB.append(i + 1)
flag = True
counter = 0
for i in range(2 * n):
	if matriz == listaA or matriz == listaB:
		print(counter)
		exit()
	if flag == True:
		for i in range(0, 2 * n, 2):
			(listaA[i], listaA[i + 1]) = (listaA[i + 1], listaA[i])
		for i in range(n):
			(listaB[i], listaB[i + n]) = (listaB[i + n], listaB[i])
		flag = not flag
	else:
		for i in range(0, 2 * n, 2):
			(listaB[i], listaB[i + 1]) = (listaB[i + 1], listaB[i])
		for i in range(n):
			(listaA[i], listaA[i + n]) = (listaA[i + n], listaA[i])
		flag = not flag
	counter += 1
print(-1)


import copy
n = int(input())
A = list(map(int, input().split()))
G = sorted(A)

def change1(A):
	for i in range(n):
		j = 2 * i
		a = A[j]
		b = A[j + 1]
		A[j] = b
		A[j + 1] = a
	return A

def change2(A):
	a = A[0:n]
	b = A[n:]
	A[0:n] = b
	A[n:] = a
	return A
A2 = copy.copy(A)
count1 = 0
for i in range(1000):
	A3 = change1(A2)
	count1 += 1
	if A3 == G:
		break
	A4 = change2(A3)
	count1 += 1
	if A4 == G:
		break
	A2 = A4
A2 = copy.copy(A)
count2 = 0
for i in range(1000):
	A3 = change2(A2)
	count2 += 1
	if A3 == G:
		break
	A4 = change1(A3)
	count2 += 1
	if A4 == G:
		break
	A2 = A4
if A == G:
	print(0)
elif A2 != G:
	print(-1)
else:
	print(min(count1, count2))


import sys
import collections
from collections import Counter, deque
import itertools
import math
import timeit
import random

def sieve(n):
	if n < 2:
		return list()
	prime = [True for _ in range(n + 1)]
	p = 3
	while p * p <= n:
		if prime[p]:
			for i in range(p * 2, n + 1, p):
				prime[i] = False
		p += 2
	r = [2]
	for p in range(3, n + 1, 2):
		if prime[p]:
			r.append(p)
	return r

def divs(n, start=1):
	divisors = []
	for i in range(start, int(math.sqrt(n) + 1)):
		if n % i == 0:
			if n / i == i:
				divisors.append(i)
			else:
				divisors.extend([i, n // i])
	return divisors

def divn(n, primes):
	divs_number = 1
	for i in primes:
		if n == 1:
			return divs_number
		t = 1
		while n % i == 0:
			t += 1
			n //= i
		divs_number *= t

def flin(d, x, default=-1):
	left = right = -1
	for i in range(len(d)):
		if d[i] == x:
			if left == -1:
				left = i
			right = i
	if left == -1:
		return (default, default)
	else:
		return (left, right)

def ceil(n, k):
	return n // k + (n % k != 0)

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(map(int, input().split()))

def lcm(a, b):
	return abs(a * b) // math.gcd(a, b)

def prr(a, sep=' '):
	print(sep.join(map(str, a)))

def dd():
	return collections.defaultdict(int)

def ddl():
	return collections.defaultdict(list)

def minmax(a, b):
	return (a, b) if a < b else (b, a)
n = ii()
d = li()
t = d[:]
ds = sorted(d)

def f(p, l, m):
	if p % 2:
		for i in range(n):
			(l[2 * i], l[2 * i + 1]) = (l[2 * i + 1], l[2 * i])
			(m[i], m[n + i]) = (m[n + i], m[i])
	else:
		for i in range(n):
			(m[2 * i], m[2 * i + 1]) = (m[2 * i + 1], m[2 * i])
			(l[i], l[n + i]) = (l[n + i], l[i])
if d[0] == 1 and d == ds:
	exit(print(0))
for k in range(2 * n):
	f(k, d, t)
	if d[0] == 1 and d == ds or (t[0] == 1 and t == ds):
		exit(print(k + 1))
print(-1)


import sys
from collections import defaultdict
import os
import sys
from io import BytesIO, IOBase
from types import GeneratorType
from collections import defaultdict
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
sys.setrecursionlimit(2 * 10 ** 5)

def op1(ind):
	if ind % 2 == 0:
		return ind - 1
	else:
		return ind + 1

def op2(ind):
	if ind > n:
		return ind - n
	else:
		return ind + n

def res(curr, ser):
	for j in ser:
		if j == '1':
			curr = op1(curr)
		else:
			curr = op2(curr)
	return curr

def fun(curr, arr, req):
	global ans
	if curr == req:
		ans.append(''.join(arr))
		return arr
	vis[curr] = 1
	p1 = op1(curr)
	p2 = op2(curr)
	if not vis[p1]:
		arr2 = []
		for j in arr:
			arr2.append(j)
		arr2.append(str(1))
		fun(p1, arr2, req)
	if not vis[p2]:
		arr2 = []
		for j in arr:
			arr2.append(j)
		arr2.append(str(2))
		fun(p2, arr2, req)
for _ in range(1):
	n = int(input())
	b = list(map(int, input().split()))
	ans = []
	vis = defaultdict(lambda : 0)
	d = dict()
	for j in range(2 * n):
		d[b[j]] = j + 1
	fun(d[1], [], 1)
	val = defaultdict(lambda : 0)
	poss = 1
	try:
		ans1 = ans[0]
	except:
		ans1 = -1
	try:
		ans2 = ans[1]
	except:
		ans2 = -1
	for i in range(2, 2 * n + 1):
		if ans1 != -1:
			if res(d[i], ans1) == i:
				pass
			else:
				ans1 = -1
		if ans2 != -1:
			if res(d[i], ans2) == i:
				pass
			else:
				ans2 = -1
		if ans1 == -1 and ans2 == -1:
			poss = 0
			break
	if not poss:
		print(-1)
	else:
		mi = float('inf')
		if ans1 != -1:
			mi = len(ans1)
		if ans2 != -1:
			mi = min(mi, len(ans2))
		print(mi)


from sys import stdin
from copy import deepcopy

def solve():
	var_1 = deepcopy(mas)
	var_2 = deepcopy(mas)
	ans = float('inf') if ans_mas != mas else 0
	for i in range(1, 2 * n + 1):
		for j in range(0, 2 * n, 2):
			(var_1[j], var_1[j + 1]) = (var_1[j + 1], var_1[j])
		for j in range(n):
			(var_2[j], var_2[n + j]) = (var_2[n + j], var_2[j])
		if var_1 == ans_mas or var_2 == ans_mas:
			ans = min(ans, i)
		(var_1, var_2) = (var_2, var_1)
	if ans == float('inf'):
		print(-1)
	else:
		print(ans)
n = int(stdin.readline())
mas = [int(s) for s in stdin.readline().split()]
ans_mas = sorted(mas)
solve()


def big_swap(num: list):
	return num[len(num) // 2:] + num[:len(num) // 2]

def alt_swap(num: list):
	alt_num = num.copy()
	for i in range(0, len(alt_num) - 1, 2):
		(alt_num[i], alt_num[i + 1]) = (alt_num[i + 1], alt_num[i])
	return alt_num
n = int(input())
nums = list(map(int, input().split()))
count_a = 0
count_b = 0
copy_nums = nums.copy()
ref_nums = nums.copy()
while copy_nums != sorted(copy_nums):
	if copy_nums == sorted(ref_nums):
		break
	copy_nums = big_swap(copy_nums)
	count_b += 1
	if copy_nums == sorted(copy_nums):
		break
	if copy_nums == ref_nums:
		count_b = -1
		break
	copy_nums = alt_swap(copy_nums)
	count_b += 1
	if copy_nums == sorted(copy_nums):
		break
	if copy_nums == ref_nums:
		count_b = -1
		break
while nums != sorted(nums):
	if nums == sorted(ref_nums):
		break
	nums = alt_swap(nums)
	count_a += 1
	if nums == sorted(nums):
		break
	if nums == ref_nums:
		count_b = -1
		break
	nums = big_swap(nums)
	count_a += 1
	if nums == sorted(nums):
		break
	if nums == ref_nums:
		count_b = -1
		break
print(min(count_a, count_b))


def sigma(x):
	if x % 2 == 1:
		return x + 1
	else:
		return x - 1

def tau(x, n):
	if x <= n:
		return x + n
	if x > n:
		return x - n
n = int(input())
eingabe = [int(x) for x in input().split()]
ziel = sorted(eingabe)
anzahl = []
for i in range(2 * n - 1):
	durchlauf = 0
	wert1 = eingabe[i]
	wert2 = eingabe[i]
	zielwert = ziel[i]
	if wert1 == zielwert:
		anzahl.append(0)
	else:
		while durchlauf < 2 * n:
			durchlauf += 1
			if durchlauf % 2 != 0:
				wert1 = sigma(wert1)
				wert2 = tau(wert2, n)
			else:
				wert1 = tau(wert1, n)
				wert2 = sigma(wert2)
			if wert1 == zielwert or wert2 == zielwert:
				anzahl.append(durchlauf)
				break
		if wert1 != zielwert and wert2 != zielwert or (anzahl != [] and durchlauf != anzahl[0]):
			print(str(-1))
			anzahl = []
			break
if anzahl != []:
	print(anzahl[0])


n = int(input())
x = input()
a = x.split(' ')
count1 = 0
count2 = 0
for i in range(len(a)):
	a[i] = int(a[i])

def p1(l):
	for j in range(0, len(l) - 1, 2):
		(l[j], l[j + 1]) = (l[j + 1], l[j])
	return l

def p2(l):
	for j in range(0, n):
		(l[j], l[n + j]) = (l[n + j], l[j])
	return l

def check(l):
	for j in range(2 * n - 1, 0, -1):
		if l[j] < l[j - 1]:
			return False
	return True

def small(l):
	return min(l)

def do(l):
	global count1, count2
	temp = l.copy()
	while count1 <= max(2 * n, 4):
		if not check(l):
			count1 += 1
			l = p1(l)
		else:
			break
		if not check(l):
			count1 += 1
			l = p2(l)
		else:
			break
	l = temp.copy()
	while count2 <= max(2 * n, 4):
		if not check(l):
			count2 += 1
			l = p2(l)
		else:
			break
		if not check(l):
			count2 += 1
			l = p1(l)
		else:
			break
if not check(a):
	do(a)
if count1 and count2 > 2 * n:
	print(-1)
else:
	print(min(count1, count2))


def a(list):
	newlist = list[int(len(list) / 2):] + list[:int(len(list) / 2)]
	return newlist

def b(list):
	newlist = []
	for i in range(int(len(list) / 2)):
		for j in range(2):
			newlist.append(list[2 * i + (1 - j)])
	return newlist

def startingWithA(list):
	count = 0
	while True:
		sorted = list.copy()
		sorted.sort()
		if list == sorted:
			return count
		list = a(list)
		count += 1
		if list == sorted:
			return count
		list = b(list)
		count += 1
		if count > len(list):
			return -1

def startingWithB(list):
	count = 0
	while True:
		sorted = list.copy()
		sorted.sort()
		if list == sorted:
			return count
		list = b(list)
		count += 1
		if list == sorted:
			return count
		list = a(list)
		count += 1
		if count > len(list):
			return -1
n = int(input())
p = list(map(int, input().split()))
print(min(startingWithA(p), startingWithB(p)))


n = int(input())
l = list(map(int, input().split()))
b1 = False
b2 = False
l1 = l[:]
k1 = 0
k2 = 0
for i in range(2 * n):
	if i % 2 == 0:
		for j in range(0, 2 * n, 2):
			(l1[j], l1[j + 1]) = (l1[j + 1], l1[j])
	else:
		l1 = l1[n:] + l1[:n]
	if all((l1[i] <= l1[i + 1] for i in range(len(l1) - 1))):
		b1 = True
		k1 = i + 1
		break
l1 = l[:]
for i in range(2 * n):
	if i % 2 == 1:
		for j in range(0, 2 * n, +2):
			(l1[j], l1[j + 1]) = (l1[j + 1], l1[j])
	else:
		l1 = l1[n:] + l1[:n]
	if all((l1[i] <= l1[i + 1] for i in range(len(l1) - 1))):
		b2 = True
		k2 = i + 1
		break
if all((l[i] <= l[i + 1] for i in range(len(l) - 1))):
	print(0)
elif b1 or b2:
	print(min(k1, k2))
else:
	print(-1)


def operation1(p):
	for i in range(0, len(p), 2):
		(p[i], p[i + 1]) = (p[i + 1], p[i])

def operation2(p):
	return p[len(p) // 2:] + p[:len(p) // 2]
n = int(input())
permutations = []
p = list(map(int, input().split()))
sorted_p = [i + 1 for i in range(2 * n)]
counter = 0
solution = float('inf')
while True:
	permutations.append(p[:])
	if p == sorted_p:
		solution = counter
		break
	if counter % 2 == 0:
		operation1(p)
	else:
		p = operation2(p)
	if p in permutations:
		break
	counter += 1
p = permutations[0]
permutations = []
counter = 0
while True:
	permutations.append(p[:])
	if p == sorted_p:
		solution = min(solution, counter)
		break
	if counter % 2 == 0:
		p = operation2(p)
	else:
		operation1(p)
	if p in permutations:
		break
	counter += 1
if solution != float('inf'):
	print(solution)
else:
	print(-1)


def o1(l, n):
	for i in range(0, n + n, 2):
		(l[i], l[i + 1]) = (l[i + 1], l[i])
	return l

def o2(l, n):
	for i in range(0, n):
		(l[i], l[n + i]) = (l[n + i], l[i])
	return l

def main():
	n = int(input())
	l = list(map(int, input().split()))
	a1 = 0
	a2 = 0
	k1 = k2 = True
	l1 = list(l)
	l1.sort()
	l2 = list(l)
	while l != l1:
		if a1 % 2 == 0:
			l = o1(l, n)
		else:
			l = o2(l, n)
		a1 += 1
		if a1 > 4000:
			k1 = False
			break
	while l2 != l1:
		if a2 % 2 == 0:
			l2 = o2(l2, n)
		else:
			l2 = o1(l2, n)
		a2 += 1
		if a2 > 4000:
			k2 = False
			break
	if k1 and k2:
		print(min(a1, a2))
	else:
		print(-1)
main()


from __future__ import division, print_function
from fractions import Fraction
import sys
import os
from io import BytesIO, IOBase
from functools import cmp_to_key
from heapq import *
from math import gcd, factorial, floor, ceil, sqrt, log2
from copy import deepcopy
from collections import deque
from bisect import bisect_left as bl
from bisect import bisect_right as br
from bisect import bisect
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
from types import GeneratorType

def iterative(f, stack=[]):

	def wrapped_func(*args, **kwargs):
		if stack:
			return f(*args, **kwargs)
		to = f(*args, **kwargs)
		while True:
			if type(to) is GeneratorType:
				stack.append(to)
				to = next(to)
				continue
			stack.pop()
			if not stack:
				break
			to = stack[-1].send(to)
		return to
	return wrapped_func

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def out(var):
	sys.stdout.write(str(var))

def lis():
	return list(map(int, inp().split()))

def stringlis():
	return list(map(str, inp().split()))

def sep():
	return map(int, inp().split())

def strsep():
	return map(str, inp().split())

def testcase(t):
	for pp in range(t):
		solve(pp)

def google(p):
	print('Case #' + str(p) + ': ', end='')

def lcm(a, b):
	return a * b // gcd(a, b)

def modInverse(b):
	g = gcd(b, mod)
	if g != 1:
		return -1
	else:
		return pow(b, mod - 2, mod)

def power(x, y, p):
	y %= p - 1
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
inf = pow(10, 20)
mod = 10 ** 9 + 7

def op1(a, pos):
	n = len(a) // 2
	for i in range(n):
		if pos == 2 * i:
			newpos = 2 * i + 1
		elif pos == 2 * i + 1:
			newpos = 2 * i
		(a[2 * i], a[2 * i + 1]) = (a[2 * i + 1], a[2 * i])
	return (a, newpos)

def op2(a, pos):
	n = len(a) // 2
	for i in range(n):
		if pos == i:
			newpos = i + n
		elif pos == i + n:
			newpos = i
		(a[i], a[i + n]) = (a[i + n], a[i])
	return (a, newpos)

def solve(case):
	n = int(inp())
	a = lis()
	n *= 2
	ans = 0
	for i in range(1):
		pos = -1
		for j in range(n):
			if a[j] == i + 1:
				pos = j
		correctpos = i
		if pos == correctpos:
			if a == sorted(a):
				print(0)
			else:
				print(-1)
			return
		new = deepcopy(a)
		f = False
		cnt = 0
		for j in range(n):
			if j % 2:
				(new, pos) = op1(new, pos)
			else:
				(new, pos) = op2(new, pos)
			cnt += 1
			if pos == correctpos:
				f = True
				break
		pos = -1
		for j in range(n):
			if a[j] == i + 1:
				pos = j
		new2 = deepcopy(a)
		cnt2 = 0
		for j in range(n):
			if j % 2:
				(new2, pos) = op2(new2, pos)
			else:
				(new2, pos) = op1(new2, pos)
			cnt2 += 1
			if pos == correctpos:
				f = True
				break
		if f:
			if cnt < cnt2:
				if new == sorted(new):
					print(cnt)
				elif new2 == sorted(new2):
					print(cnt2)
				else:
					print(-1)
			elif new2 == sorted(new2):
				print(cnt2)
			elif new == sorted(new):
				print(cnt)
			else:
				print(-1)
		else:
			print(-1)
testcase(1)


from collections import defaultdict, deque, Counter
from sys import stdin, stdout
from heapq import heappush, heappop
import math
import io
import os
import math
import bisect

def isPrime(x):
	for i in range(2, x):
		if i * i > x:
			break
		if x % i == 0:
			return False
	return True

def ncr(n, r, p):
	num = den = 1
	for i in range(r):
		num = num * (n - i) % p
		den = den * (i + 1) % p
	return num * pow(den, p - 2, p) % p

def primeFactors(n):
	l = []
	while n % 2 == 0:
		l.append(2)
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			l.append(int(i))
			n = n / i
	if n > 2:
		l.append(n)
	return list(set(l))

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def digits(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def ceil(n, x):
	if n % x == 0:
		return n // x
	return n // x + 1

def mapin():
	return map(int, input().split())

def help(x, y):
	if x == y:
		if x[0] == x[1]:
			return 0
		return 1
	elif x == list(reversed(y)):
		return -1
	return 2

def check(z, l):
	for j in z:
		if j == 1:
			for i in range(0, len(l), 2):
				(l[i], l[i + 1]) = (l[i + 1], l[i])
		else:
			for i in range(0, len(l) // 2):
				(l[i], l[i + len(l) // 2]) = (l[i + len(l) // 2], l[i])
	if l == sorted(l):
		return len(z)
	else:
		return -1
n = int(input())
l = [int(x) for x in input().split()]
ll = []
for i in range(len(l)):
	ll.append(l[i])
curr = l.index(1) + 1
x = 1
z = []
while curr != 1:
	if x == 1:
		if curr & 1 == 0:
			curr -= 1
		else:
			curr += 1
		z.append(1)
		x = 2
	else:
		if curr <= n:
			curr += n
		else:
			curr -= n
		z.append(2)
		x = 1
x = 2
zz = []
curr = l.index(1) + 1
while curr != 1:
	if x == 1:
		if curr & 1 == 0:
			curr -= 1
		else:
			curr += 1
		zz.append(1)
		x = 2
	else:
		if curr <= n:
			curr += n
		else:
			curr -= n
		zz.append(2)
		x = 1
a = check(z, l)
b = check(zz, ll)
if a == -1 and b == -1:
	print(-1)
else:
	print(min(a, b))


import math, sys
sys.setrecursionlimit(10 ** 6)
n = int(input())
inp_arr = list(map(int, input().split()))
x = [i for i in inp_arr]
y = [i for i in inp_arr]

def verify(arr):
	for i in range(1, 2 * n):
		if arr[i] < arr[i - 1]:
			return False
	return True

def helper(arr, flag, allo):
	if verify(arr):
		return 0
	elif arr == x:
		if allo == 1:
			return math.inf
		allo += 1
	if flag:
		for i in range(n):
			(arr[i], arr[i + n]) = (arr[i + n], arr[i])
	else:
		for i in range(0, 2 * n, 2):
			(arr[i], arr[i + 1]) = (arr[i + 1], arr[i])
	return 1 + helper(arr, 1 - flag, allo)
x = min(helper(inp_arr, 0, 0), helper(y, 1, 0))
print(-1) if x == math.inf else print(x)


n = input()
asu1 = [int(k) for k in input().split()]

def op1(a):
	for i in range(0, len(a), 2):
		temp = a[i]
		a[i] = a[i + 1]
		a[i + 1] = temp
	return a

def op2(a):
	for i in range(0, len(a) // 2):
		temp = a[i]
		a[i] = a[i + len(a) // 2]
		a[i + len(a) // 2] = temp
	return a

def function(b, one):
	l = 0
	original = b.copy()
	sorted = original.copy()
	sorted.sort()
	while b != sorted:
		l += 1
		if one:
			b = op2(b)
			one = False
		else:
			b = op1(b)
			one = True
		if b == original:
			return -1
	return l
(c, d) = (function(asu1.copy(), False), function(asu1, True))
if c == -1 and d == -1:
	print(-1)
elif c == -1:
	print(d)
elif d == -1:
	print(c)
else:
	print(min(c, d))


def op1(thelist, n):
	for i in range(0, 2 * n - 1, 2):
		temp = thelist[i + 1]
		thelist[i + 1] = thelist[i]
		thelist[i] = temp
	return thelist

def op2(thelist, n):
	for i in range(n):
		temp2 = thelist[i]
		thelist[i] = thelist[n + i]
		thelist[n + i] = temp2
	return thelist
num = int(input())
mylist = list(map(int, input().split()))
sortedlist = list(range(1, 2 * num + 1))
first = mylist.copy()
second = mylist.copy()
steps = 0
if mylist == sortedlist:
	print(0)
else:
	while steps < num:
		first = op1(first, num)
		second = op2(second, num)
		(first, second) = (second, first)
		steps += 1
		if first == sortedlist or second == sortedlist:
			print(steps)
			steps = num + 1
	if steps == num:
		print(-1)


n = int(input())
b = list(map(int, input().split()))
c = sorted(b)
r = 0
while b != c:
	if r > 3 * n:
		r = -1
		break
	elif b[0] < b[n]:
		for i in range(0, 2 * n, 2):
			(b[i], b[i + 1]) = (b[i + 1], b[i])
		r += 1
	else:
		for i in range(0, n):
			(b[i], b[n + i]) = (b[n + i], b[i])
		r += 1
print(r)


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import random
import re
import sys
import time
import string
from typing import List, Mapping
sys.setrecursionlimit(999999)
n = int(input())
arr = list(map(int, input().split()))
s = set()
q = collections.deque()
q.append([arr, 0])
while q:
	(aas, cnt) = q.popleft()
	if aas[0] == 1:
		if aas == list(range(1, 2 * n + 1)):
			print(cnt)
		else:
			print(-1)
		break
	if aas[0] in s:
		continue
	s.add(aas[0])
	at1 = aas[n:] + aas[:n]
	q.append([at1, cnt + 1])
	at2 = [0] * (2 * n)
	(at2[0::2], at2[1::2]) = (aas[1::2], aas[0::2])
	q.append([at2, cnt + 1])
else:
	print(-1)


def op1(arr):
	for i in range(0, 2 * n, 2):
		aa = arr[i]
		arr[i] = arr[i + 1]
		arr[i + 1] = aa
	k = ''.join(list(map(str, arr)))
	if d.get(k) == None:
		d[k] = 0
		return True
	else:
		return False

def op2(arr):
	for i in range(0, n):
		aa = arr[i]
		arr[i] = arr[i + n]
		arr[i + n] = aa
	k = ''.join(list(map(str, arr)))
	if d.get(k) == None:
		d[k] = 0
		return True
	else:
		return False
n = int(input())
arr = list(map(int, input().split()))
ar = [a for a in arr]
br = [a for a in arr]
a = 0
d = {}
ar.sort()
while ar != arr:
	if op1(arr):
		a += 1
	else:
		a = -1
		break
	if ar == arr:
		break
	if op2(arr):
		a += 1
	else:
		a = -1
		break
arr = [b for b in br]
b = 0
d = {}
while ar != arr:
	if op2(arr):
		b += 1
	else:
		b = -1
		break
	if ar == arr:
		break
	if op1(arr):
		b += 1
	else:
		b = -1
		break
if b == -1 and a == -1:
	print(-1)
elif b == -1 and a != -1:
	print(a)
elif a == -1 and b != -1:
	print(b)
else:
	print(min(a, b))


N = int(input())
P = list(map(int, input().split()))
x = P.index(1)
Y = []
for i in range(N * 2):
	if x == 0:
		break
	if (i ^ 1) & 1:
		Y.append(0)
		x ^= 1
	else:
		Y.append(1)
		if x < N:
			x += N
		else:
			x -= N
x = P.index(1)
Q = P[:]
Z = []
if x < N:
	x += N
else:
	x -= N
Z.append(1)
for i in range(N * 2):
	if x == 0:
		break
	if (i ^ 1) & 1:
		Z.append(0)
		x ^= 1
	else:
		Z.append(1)
		if x < N:
			x += N
		else:
			x -= N
for i in range(len(Y)):
	if Y[i]:
		for j in range(N):
			(P[j], P[j + N]) = (P[j + N], P[j])
	else:
		for j in range(0, N << 1, 2):
			(P[j], P[j + 1]) = (P[j + 1], P[j])
ANS = 10 ** 9
if sorted(P) == P:
	ANS = min(ANS, len(Y))
for i in range(len(Z)):
	if Z[i]:
		for j in range(N):
			(Q[j], Q[j + N]) = (Q[j + N], Q[j])
	else:
		for j in range(0, N << 1, 2):
			(Q[j], Q[j + 1]) = (Q[j + 1], Q[j])
if sorted(Q) == Q:
	ANS = min(ANS, len(Z))
if ANS == 10 ** 9:
	print(-1)
else:
	print(ANS)


import collections
import string
import math
import copy
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')
mo = int(1000000000.0 + 7)

def exgcd(a, b):
	if not b:
		return (1, 0)
	(y, x) = exgcd(b, a % b)
	y -= a // b * x
	return (x, y)

def getinv(a, m):
	(x, y) = exgcd(a, m)
	return -1 if x == 1 else x % m

def comb(n, b):
	res = 1
	b = min(b, n - b)
	for i in range(b):
		res = res * (n - i) * getinv(i + 1, mo) % mo
	return res % mo

def quickpower(a, n):
	res = 1
	while n:
		if n & 1:
			res = res * a % mo
		n >>= 1
		a = a * a % mo
	return res

def dis(a, b):
	return abs(a[0] - b[0]) + abs(a[1] - b[1])

def getpref(x):
	if x > 1:
		return x * (x - 1) >> 1
	else:
		return 0

def orafli(upp):
	primes = []
	marked = [False for i in range(upp + 3)]
	prvs = [i for i in range(upp + 3)]
	for i in range(2, upp):
		if not marked[i]:
			primes.append(i)
		for j in primes:
			if i * j >= upp:
				break
			marked[i * j] = True
			prvs[i * j] = j
			if i % j == 0:
				break
	return (primes, prvs)

def lower_ord(c: str) -> int:
	return ord(c) - 97

def upper_ord(c: str) -> int:
	return ord(c) - 65

def read_list():
	return [int(i) for i in input().split()]

def read_int():
	s = input().split()
	if len(s) == 1:
		return int(s[0])
	else:
		return map(int, s)

def ask(s):
	print(f'? {s}', flush=True)

def answer(s):
	print(f'{s}', flush=True)

def transfer(f1, f2):
	ret = []
	for (p, i) in enumerate(f1):
		ret.append(f2[i])
	return ret

def chktrans(f1, f2):
	ret = []
	for (p, i) in enumerate(f1):
		if f1[p] != f2[i]:
			return False
		else:
			ret.append(f2[i])
	return ret
import random

def solve():
	n = read_int()
	l = read_list()
	sl = sorted(l)
	if l == sl:
		print(0)
		return
	l1 = list(l)

	def swap1(ll):
		for i in range(n):
			(ll[i << 1], ll[i << 1 | 1]) = (ll[i << 1 | 1], ll[i << 1])
		return ll

	def swap2(ll):
		for i in range(n):
			(ll[i], ll[n + i]) = (ll[n + i], ll[i])
		return ll
	ans = 1145141919810
	ca1 = 1
	sta = 0
	swap1(l1)
	while l1 != l:
		if l1 == sl:
			ans = min(ca1, ans)
			break
		if sta:
			swap1(l1)
		else:
			swap2(l1)
		ca1 += 1
		sta ^= 1
	l2 = list(l)
	swap2(l2)
	ca2 = 1
	sta = 1
	while l2 != l:
		if l2 == sl:
			ans = min(ca2, ans)
			break
		if sta:
			swap1(l2)
		else:
			swap2(l2)
		ca2 += 1
		sta ^= 1
	if ans != 1145141919810:
		print(ans)
	else:
		print(-1)
solve()


def op1(l, n):
	for i in range(n):
		(l[2 * i], l[2 * i + 1]) = (l[2 * i + 1], l[2 * i])
	return l

def op2(l, n):
	for i in range(n):
		(l[i], l[n + i]) = (l[n + i], l[i])
	return l
n = int(input())
perms = [int(x) for x in input().split(' ')]
sorted_p = list(range(1, 2 * n + 1))
ans = 0
found = False
is_1 = True
tmp = perms.copy()
for _ in range(2 * n):
	if sorted_p == tmp:
		found = True
		break
	tmp = op1(tmp, n) if is_1 else op2(tmp, n)
	is_1 = not is_1
	ans += 1
if not found:
	print(-1)
else:
	new_ans = 0
	is_1 = False
	tmp = perms.copy()
	for _ in range(2 * n):
		if sorted_p == tmp:
			break
		tmp = op1(tmp, n) if is_1 else op2(tmp, n)
		is_1 = not is_1
		new_ans += 1
	print(min(ans, new_ans))


import sys
import math
import heapq
import bisect
from collections import Counter
from collections import defaultdict
from io import BytesIO, IOBase
import string

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		import os
		self.os = os
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None
		self.BUFSIZE = 8192

	def read(self):
		while True:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = self.os.read(self._fd, max(self.os.fstat(self._fd).st_size, self.BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			self.os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def get_int():
	return int(input())

def get_ints():
	return list(map(int, input().split(' ')))

def get_int_grid(n):
	return [get_ints() for _ in range(n)]

def get_str():
	return input().strip()

def get_strs():
	return get_str().split(' ')

def flat_list(arr):
	return [item for subarr in arr for item in subarr]

def yes_no(b):
	if b:
		return 'YES'
	else:
		return 'NO'

def binary_search(good, left, right, delta=1, right_true=False):
	limits = [left, right]
	while limits[1] - limits[0] > delta:
		if delta == 1:
			mid = sum(limits) // 2
		else:
			mid = sum(limits) / 2
		if good(mid):
			limits[int(right_true)] = mid
		else:
			limits[int(~right_true)] = mid
	if good(limits[int(right_true)]):
		return limits[int(right_true)]
	else:
		return False

def prefix_sums(a):
	p = [0]
	for x in a:
		p.append(p[-1] + x)
	return p

def solve_k():
	n = get_int()
	p = get_ints()
	p_sorted = [i + 1 for i in range(2 * n)]
	best_steps = float('inf')
	for rep in range(2):
		q = p[:]

		def swap1(q):
			for i in range(n):
				(q[2 * i], q[2 * i + 1]) = (q[2 * i + 1], q[2 * i])
			return q

		def swap2(q):
			for i in range(n):
				(q[i], q[n + i]) = (q[n + i], q[i])
			return q
		swaps = [swap1, swap2]
		cnt = 0
		while True:
			if q == p_sorted:
				best_steps = min(best_steps, cnt)
				break
			q = swaps[(rep + cnt) % 2](q)
			cnt += 1
			if q == p:
				break
	if best_steps == float('inf'):
		return -1
	else:
		return best_steps
print(solve_k())


def op1(a):
	i = 0
	while i < len(a):
		(a[i], a[i + 1]) = (a[i + 1], a[i])
		i += 2

def op2(a):
	for i in range(len(a) // 2):
		(a[i], a[i + len(a) // 2]) = (a[i + len(a) // 2], a[i])
n = int(input())
count = 0
f1 = 1
f2 = 1
a = list(map(int, input().split()))
itr = 0
while itr <= n and a != sorted(a):
	if a[0] > a[len(a) // 2]:
		op2(a)
		count += 1
		f2 = 0
		f1 = 1
	if f1 == 1 and a != sorted(a):
		op1(a)
		f2 = 1
		f1 = 0
		count += 1
	elif f2 == 1 and a != sorted(a):
		op2(a)
		f1 = 1
		f2 = 0
		count += 1
	itr += 1
if a != sorted(a):
	print(-1)
else:
	print(count)


def doOpt1(lst, n):
	for i in range(0, 2 * n, 2):
		(lst[i], lst[i + 1]) = (lst[i + 1], lst[i])

def doOpt2(lst, n):
	for i in range(n):
		(lst[i], lst[i + n]) = (lst[i + n], lst[i])
n = int(input())
theList = list(map(int, input().split()))
sortedList = [i + 1 for i in range(2 * n)]
ans = float('inf')
for opt in range(2):
	step = 0
	temList = theList[:]
	while temList[-1] != sortedList[-1]:
		if opt:
			doOpt1(temList, n)
		else:
			doOpt2(temList, n)
		opt = not opt
		step += 1
	if temList == sortedList:
		ans = min(ans, step)
if ans == float('inf'):
	print(-1)
else:
	print(ans)


n = int(input())
s1 = list(map(int, input().split()))
s = list(s1)
x = s.index(1)
c = 0
while True:
	if x != 0:
		if x + 1 < n:
			if x % 2 != 0:
				i = 0
				while i < 2 * n - 1:
					(s[i], s[i + 1]) = (s[i + 1], s[i])
					i = i + 2
				x = x - 1
			else:
				i = 0
				while i < n:
					(s[i], s[i + n]) = (s[i + n], s[i])
					i += 1
				x = x + n
		elif x + 1 > n + 1:
			if x % 2 == 0:
				i = 0
				while i < n:
					(s[i], s[i + n]) = (s[i + n], s[i])
					i += 1
				x = x - n
			else:
				i = 0
				while i < 2 * n - 1:
					(s[i], s[i + 1]) = (s[i + 1], s[i])
					i = i + 2
				x = x - 1
		elif x + 1 == n:
			if n % 2 == 0:
				i = 0
				while i < 2 * n - 1:
					(s[i], s[i + 1]) = (s[i + 1], s[i])
					i = i + 2
				x = x - 1
			else:
				i = 0
				while i < 2 * n - 1:
					(s[i], s[i + 1]) = (s[i + 1], s[i])
					i = i + 2
				x = x + 1
		elif x + 1 == n + 1:
			i = 0
			while i < n:
				(s[i], s[i + n]) = (s[i + n], s[i])
				i += 1
			x = x - n
		c += 1
	else:
		break
f = 0
for i in range(2 * n):
	if s[i] != i + 1:
		print(-1)
		f = 1
		break
if f == 0:
	s = list(s1)
	x = s.index(1)
	c1 = 0
	while True and c1 < c:
		if x != 0:
			if x + 1 < n:
				if x % 2 == 0:
					i = 0
					while i < 2 * n - 1:
						(s[i], s[i + 1]) = (s[i + 1], s[i])
						i = i + 2
					x = x + 1
				else:
					i = 0
					while i < n:
						(s[i], s[i + n]) = (s[i + n], s[i])
						i += 1
					x = x + n
			elif x + 1 > n + 1:
				if x % 2 != 0:
					i = 0
					while i < n:
						(s[i], s[i + n]) = (s[i + n], s[i])
						i += 1
					x = x - n
				else:
					i = 0
					while i < 2 * n - 1:
						(s[i], s[i + 1]) = (s[i + 1], s[i])
						i = i + 2
					x = x + 1
			elif x + 1 == n:
				if n % 2 == 0:
					i = 0
					while i < 2 * n - 1:
						(s[i], s[i + 1]) = (s[i + 1], s[i])
						i = i + 2
					x = x - 1
				else:
					i = 0
					while i < 2 * n - 1:
						(s[i], s[i + 1]) = (s[i + 1], s[i])
						i = i + 2
					x = x + 1
			elif x + 1 == n + 1:
				i = 0
				while i < n:
					(s[i], s[i + n]) = (s[i + n], s[i])
					i += 1
				x = x - n
			c1 += 1
		else:
			break
	print(min(c, c1))


n = int(input())
arr = [int(i) for i in input().split()]
srt = arr.copy()
arr2 = arr.copy()
srt.sort()
ans1 = 1000000000.0
ans2 = 1000000000.0
flag = True
fl2 = True
fl3 = True
if srt == arr:
	print(0)
	exit(0)
for i in range(2 * n):
	if srt == arr:
		fl2 = not fl2
		ans1 = i
		break
	if flag:
		flag = not flag
		for j in range(0, 2 * n, 2):
			(arr[j], arr[j + 1]) = (arr[j + 1], arr[j])
	else:
		flag = not flag
		for j in range(0, n, 1):
			(arr[j], arr[n + j]) = (arr[n + j], arr[j])
flag = False
for i in range(2 * n):
	if srt == arr2:
		fl3 = not fl3
		ans2 = i
		break
	if flag:
		flag = not flag
		for j in range(0, 2 * n, 2):
			(arr2[j], arr2[j + 1]) = (arr2[j + 1], arr2[j])
	else:
		flag = not flag
		for j in range(0, n, 1):
			(arr2[j], arr2[n + j]) = (arr2[n + j], arr2[j])
if fl2 and fl3:
	print(-1)
else:
	print(min(ans2, ans1))


def swap1(A):
	for i in range(len(A) // 2):
		b = A[2 * i]
		A[2 * i] = A[2 * i + 1]
		A[2 * i + 1] = b
	return A

def swap2(A):
	n = len(A) // 2
	for i in range(n):
		b = A[n + i]
		A[n + i] = A[i]
		A[i] = b
	return A

def is_sorted(A):
	for i in range(len(A)):
		if A[i] != i + 1:
			return False
	return True

def sol():
	n = int(input())
	st = input()
	A = st.split(' ')
	m = 2 * n
	for i in range(m):
		A[i] = int(A[i])
	A2 = A.copy()
	ans = -1
	if is_sorted(A):
		print(0)
		return 0
	for i in range(n):
		if i % 2 == 0:
			A = swap1(A)
			A2 = swap2(A2)
			if is_sorted(A):
				print(i + 1)
				return i + 1
			if is_sorted(A2):
				print(i + 1)
				return i + 1
		else:
			A = swap2(A)
			A2 = swap1(A2)
			if is_sorted(A):
				print(i + 1)
				return i + 1
			if is_sorted(A2):
				print(i + 1)
				return i + 1
	print(ans)
	return ans
sol()


def op1(n, array):
	for x in range(n):
		(array[2 * x], array[2 * x + 1]) = (array[2 * x + 1], array[2 * x])

def op2(n, array):
	for x in range(n):
		(array[x], array[n + x]) = (array[n + x], array[x])

def kingsTask(n, array):
	ans = sorted(array)
	if ans == array:
		return 0
	if n % 2 == 0:
		f = 0
		arri = array.copy()
		for x in range(3):
			if x % 2 == 0:
				op1(n, array)
			else:
				op2(n, array)
			if array == ans:
				f = 1
				break
		for y in range(3):
			if y % 2 == 0:
				op2(n, arri)
			else:
				op1(n, arri)
			if arri == ans:
				f = 1
				break
		if f == 1:
			return min(x, y) + 1
		return -1
	else:
		for x in range(2 * n - 1):
			if x % 2 == 0:
				op1(n, array)
			else:
				op2(n, array)
			if array == ans:
				return min(x + 1, 2 * n - (x + 1))
		return -1
n = int(input())
l = list(map(int, input().split()))
print(kingsTask(n, l))