William has an array of $n$ integers $a_1, a_2, \dots, a_n$. In one move he can swap two neighboring items. Two items $a_i$ and $a_j$ are considered neighboring if the condition $|i - j| = 1$ is satisfied.

William wants you to calculate the minimal number of swaps he would need to perform to make it so that the array does not contain two neighboring items with the same parity.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows.

The first line of each test case contains an integer $n$ $(1 \le n \le 10^5)$ which is the total number of items in William's array.

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ $(1 \le a_i \le 10^9)$ which are William's array.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.


-----Output-----

For each test case output the minimal number of operations needed or $-1$ if it is impossible to get the array to a state when no neighboring numbers have the same parity.


-----Examples-----

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


-----Note-----

In the first test case the following sequence of operations would satisfy the requirements:

swap(2, 3). Array after performing the operation: $[6, 1, 6]$

In the second test case the array initially does not contain two neighboring items of the same parity.

In the third test case the following sequence of operations would satisfy the requirements:

swap(3, 4). Array after performing the operation: $[1, 1, 2, 1, 2, 2]$

swap(2, 3). Array after performing the operation: $[1, 2, 1, 1, 2, 2]$

swap(4, 5). Array after performing the operation: $[1, 2, 1, 2, 1, 2]$

In the fourth test case it is impossible to satisfy the requirements.

In the fifth test case the following sequence of operations would satisfy the requirements:

swap(2, 3). Array after performing the operation: $[6, 3, 2, 4, 5, 1]$

swap(4, 5). Array after performing the operation: $[6, 3, 2, 5, 4, 1]$

Difficulty: EASY
Tags: Implementation

Starter code:

Solutions:
import math
import os
import sys
from math import *
from io import BytesIO, IOBase
from collections import *
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')

def prog_name():
	n = int(input())
	l = list(map(int, input().split()))
	cnte = 0
	cnto = 0
	ce = 0
	co = 0
	cnt = 0
	final = []
	for x in range(len(l)):
		if l[x] % 2 == 0:
			cnte += 1
			final.append('e')
		else:
			cnto += 1
			final.append('o')
	if abs(cnte - cnto) > 1:
		print(-1)
	else:
		wrong1 = ['e', 'o'] * (n // 2)
		wrong2 = ['o', 'e'] * (n // 2)
		we1 = []
		wo1 = []
		we2 = []
		wo2 = []
		if cnto == cnte:
			ans1 = 0
			ans2 = 0
			for x in range(len(l)):
				if final[x] != wrong1[x]:
					if wrong1[x] == 'e':
						we1.append(x)
					else:
						wo1.append(x)
				if final[x] != wrong2[x]:
					if wrong2[x] == 'e':
						we2.append(x)
					else:
						wo2.append(x)
			for y in range(len(we1)):
				ans1 += abs(we1[y] - wo1[y])
			for z in range(len(we2)):
				ans2 += abs(we2[z] - wo2[z])
			print(min(ans1, ans2))
		elif cnto > cnte:
			ans = 0
			wrong2.append('o')
			for x in range(len(l)):
				if final[x] != wrong2[x]:
					if wrong2[x] == 'e':
						we2.append(x)
					else:
						wo2.append(x)
			for z in range(len(we2)):
				ans += abs(we2[z] - wo2[z])
			print(ans)
		else:
			ans = 0
			wrong1.append('e')
			for x in range(len(l)):
				if final[x] != wrong1[x]:
					if wrong1[x] == 'e':
						we1.append(x)
					else:
						wo1.append(x)
			for y in range(len(we1)):
				ans += abs(we1[y] - wo1[y])
			print(ans)

def main():
	T = int(input())
	for unique in range(T):
		prog_name()
main()


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = 0
	c2 = 0

	def odd(a, n):
		v1 = []
		v2 = []
		for i in range(n):
			if i % 2 == 0 and a[i] % 2 == 0:
				v1.append(i)
			if i % 2 == 1 and a[i] % 2 == 1:
				v2.append(i)
		c = 0
		for i in range(len(v1)):
			c += max(v1[i], v2[i]) - min(v1[i], v2[i])
		return c

	def even(a, n):
		v1 = []
		v2 = []
		for i in range(n):
			if i % 2 == 0 and a[i] % 2 == 1:
				v1.append(i)
			if i % 2 == 1 and a[i] % 2 == 0:
				v2.append(i)
		c = 0
		for i in range(len(v1)):
			c += max(v1[i], v2[i]) - min(v1[i], v2[i])
		return c
	for i in a:
		if i & 1:
			c1 += 1
		else:
			c2 += 1
	if n % 2 == 0:
		if c1 == c2:
			print(min(odd(a, n), even(a, n)))
		else:
			print(-1)
	elif c1 + 1 == c2:
		print(even(a, n))
	elif c2 + 1 == c1:
		print(odd(a, n))
	else:
		print(-1)


for _ in range(int(input())):
	a = int(input())
	b = [int(x) % 2 for x in input().split()]
	g = [[], []]
	for i in range(a):
		g[b[i]].append(i)
	(o, e) = (len(g[1]), len(g[0]))
	if abs(o - e) > 1:
		print(-1)
		continue
	ans1 = ans2 = 0
	for i in range(0, a, 2):
		ans1 += abs(g[o > e][i // 2] - i)
	for i in range(0, a - 1, 2):
		ans2 += abs(g[o < e][i // 2] - (i + 1))
	print(min(ans1, ans2))


import sys
input = lambda : sys.stdin.readline().strip()

def get(arr, p):
	locs = [i for (i, a) in enumerate(arr) if a == p]
	goal = list(range(0, len(arr), 2))
	return sum((abs(i - j) for (i, j) in zip(locs, goal)))

def solve():
	n = int(input())
	arr = list(map(int, input().split()))
	arr = [a & 1 for a in arr]
	x1 = sum(arr)
	x0 = n - x1
	if abs(x1 - x0) > 1:
		return -1
	if n & 1:
		if x0 > x1:
			return get(arr, 0)
		else:
			return get(arr, 1)
	else:
		return min(get(arr, 0), get(arr, 1))
for _ in range(int(input())):
	print(solve())


t = int(input())
for i in range(t):
	n = int(input())
	w = [int(k) for k in input().split()]
	res = 0
	(a, b) = (0, 0)
	for j in w:
		if j % 2 == 0:
			a += 1
		else:
			b += 1
	if a == b + 1:
		(x, y) = (0, 0)
		while x < n:
			if x % 2 == 0:
				while w[y] % 2 == 1:
					y += 1
				res += abs(y - x)
				y += 1
			x += 1
		print(res)
	elif b == a + 1:
		(x, y) = (0, 0)
		while x < n:
			if x % 2 == 0:
				while w[y] % 2 == 0:
					y += 1
				res += abs(y - x)
				y += 1
			x += 1
		print(res)
	elif b == a:
		(res1, res2) = (0, 0)
		(x, y) = (0, 0)
		while x < n:
			if x % 2 == 0:
				while w[y] % 2 == 1:
					y += 1
				res1 += abs(y - x)
				y += 1
			x += 1
		(x, y) = (0, 0)
		while x < n:
			if x % 2 == 0:
				while w[y] % 2 == 0:
					y += 1
				res2 += abs(y - x)
				y += 1
			x += 1
		res = min(res1, res2)
		print(res)
	else:
		print(-1)


import sys
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	data = list(map(int, sys.stdin.readline().split()))
	(pos, idx) = ([[], []], data[0] % 2)
	for i in range(n):
		pos[data[i] % 2].append(i)
	if len(pos[0]) not in [n // 2, n // 2 + 1] or len(pos[1]) not in [n // 2, n // 2 + 1]:
		print(-1)
		continue
	(spot1, ans) = ([i for i in range(0, n, 2)], float('inf'))
	for i in range(len(pos)):
		if len(pos[i]) != len(spot1):
			continue
		temp_ans = 0
		for j in range(len(pos[i])):
			temp_ans += abs(spot1[j] - pos[i][j])
		ans = min(temp_ans, ans)
	print(ans)


import heapq, math
from collections import defaultdict, deque
from os import getcwd
from itertools import permutations
import sys, os.path
if os.path.exists('C:/Users/Dhanush/Desktop/cp/input.txt'):
	sys.stdout = open('C:/Users/Dhanush/Desktop/cp/output.txt', 'w')
	sys.stdin = open('C:/Users/Dhanush/Desktop/cp/input.txt', 'r')

def solve(res):
	i = 0
	j = 0
	f = 1
	count = 0
	temp = l[:]
	while i < n and j < n:
		if res[i] == temp[i]:
			i += 1
		else:
			j = max(j, i)
			cur = temp[i]
			while j < n and temp[j] == cur:
				j += 1
			if j == n:
				return float('inf')
			temp[i] = 1 - temp[1]
			temp[j] = 1 - temp[j]
			count += j - i
			i += 1
	return count
tc = int(input())
for _ in range(tc):
	n = int(input())
	l1 = list(map(int, input().split()))
	l = []
	(even, odd) = (0, 0)
	for i in l1:
		if i % 2 == 0:
			even += 1
			l.append(0)
		else:
			odd += 1
			l.append(1)
	if abs(even - odd) > 1:
		print(-1)
	else:
		l1 = []
		l2 = []
		f = 0
		for i in range(n):
			if f == 0:
				l1.append(0)
				l2.append(1)
			else:
				l1.append(1)
				l2.append(0)
			f = 1 - f
		print(min(solve(l1), solve(l2)))


def check(odd, even):
	if abs(len(even) - len(odd)) > 1:
		return -1
	elif len(even) == 0 or len(odd) == 0:
		return 0
	elif len(even) == len(odd):
		countEven = 0
		index = 0
		for i in range(len(even)):
			countEven += abs(even[i] - index)
			index += 2
		countOdd = 0
		index = 0
		for i in range(len(odd)):
			countOdd += abs(odd[i] - index)
			index += 2
		return min(countEven, countOdd)
	else:
		index = 1
		if len(even) > len(odd):
			index = 0
		countEven = 0
		for i in range(len(even)):
			countEven += abs(even[i] - index)
			index += 2
		return countEven
for t in range(int(input())):
	n = int(input())
	arr = input().split()
	even = []
	odd = []
	for i in range(n):
		if int(arr[i]) % 2 == 0:
			even.append(i)
		else:
			odd.append(i)
	print(check(odd, even))


def even(arr):
	ev = []
	od = []
	ans = 0
	for i in range(len(arr)):
		if i % 2 == 0:
			if arr[i] % 2 != 0:
				ev.append(i)
		elif arr[i] % 2 == 0:
			od.append(i)
	for i in range(len(ev)):
		ans += abs(ev[i] - od[i])
	return ans

def odd(arr):
	ev = []
	od = []
	ans = 0
	for i in range(len(arr)):
		if i % 2 == 0:
			if arr[i] % 2 == 0:
				od.append(i)
		elif arr[i] % 2 != 0:
			ev.append(i)
	for i in range(len(ev)):
		ans += abs(ev[i] - od[i])
	return ans
for i in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	(e, o) = (0, 0)
	for i in arr:
		if i % 2 == 0:
			e += 1
		else:
			o += 1
	if abs(e - o) > 1:
		print(-1)
	elif abs(e - o) == 0:
		p = even(arr)
		q = odd(arr)
		print(min(p, q))
	elif e > o:
		print(even(arr))
	else:
		print(odd(arr))


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split(' ')))

	def fun(arr, n):
		e = 0
		o = 0
		for i in range(n):
			if arr[i] % 2 == 0:
				e += 1
			else:
				o += 1
		if abs(e - o) > 1:
			return -1
		elif e == o:
			ex = 0
			ox = 0
			c = 0
			for i in range(n):
				if arr[i] % 2 == 0:
					ex += abs(i - c)
					c += 2
			c = 0
			for i in range(n):
				if arr[i] % 2 == 1:
					ox += abs(i - c)
					c += 2
			return min(ex, ox)
		elif e > o:
			ex = 0
			c = 0
			for i in range(n):
				if arr[i] % 2 == 0:
					ex += abs(i - c)
					c += 2
			return ex
		else:
			ox = 0
			c = 0
			for i in range(n):
				if arr[i] % 2 == 1:
					ox += abs(i - c)
					c += 2
			return ox
	print(fun(arr, n))


from sys import *
input = stdin.readline
import random
from bisect import *
import math
from collections import *
import operator
from heapq import *
from itertools import *
inf = 10 ** 18
mod = 10 ** 9 + 7

def solve(b):
	j = 0
	ans = 0
	for i in range(n):
		if j < n and a[i] % 2 == b[j] % 2:
			ans += abs(j - i)
			j += 2
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	if n == 1:
		print(0)
		continue
	for i in range(n):
		a[i] %= 2
	one = a.count(1)
	zero = a.count(0)
	if n % 2 and (one > (n + 1) // 2 or zero > (n + 1) // 2):
		print(-1)
		continue
	if n % 2 == 0 and (one > n // 2 or zero > (n + 1) // 2):
		print(-1)
		continue
	ans = inf
	b = []
	for i in range(n):
		if i % 2:
			b.append(1)
		else:
			b.append(0)
	if b.count(0) == zero and b.count(1) == one:
		ans = min(ans, solve(b))
	b = []
	for i in range(n):
		if i % 2:
			b.append(0)
		else:
			b.append(1)
	if b.count(0) == zero and b.count(1) == one:
		ans = min(ans, solve(b))
	print(ans)


def callsteps(next_position_to_move_1_into, a, n):
	steps = 0
	for i in range(n):
		if a[i] == 1:
			steps += abs(i - next_position_to_move_1_into)
			next_position_to_move_1_into += 2
	return steps
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split(' ')))
	count1 = 0
	count0 = 0
	for i in range(n):
		if a[i] % 2 == 1:
			a[i] = 1
			count1 += 1
		else:
			a[i] = 0
			count0 += 1
	if abs(count0 - count1) > 1:
		print(-1)
	elif count0 > count1:
		print(callsteps(1, a, n))
	elif count1 > count0:
		print(callsteps(0, a, n))
	else:
		print(min(callsteps(0, a, n), callsteps(1, a, n)))


def case_odd(even, odd):
	if len(even) > len(odd):
		a = even.copy()
	else:
		a = odd.copy()
	b = 0
	for (i, el) in enumerate(a):
		b += abs(el - 2 * i)
	return b

def case_even(even, odd):
	a = 0
	b = 0
	for (i, el) in enumerate(odd):
		a += abs(el - 2 * i)
	for (i, el) in enumerate(even):
		b += abs(el - 2 * i)
	return min(a, b)

def check_n_run(x, y, z, p, q):
	if x % 2 == 0:
		if y == z:
			return case_even(p, q)
		else:
			return -1
	elif abs(y - z) == 1:
		return case_odd(p, q)
	else:
		return -1

def solve():
	even = []
	odd = []
	for (i, el) in enumerate(eins):
		if el % 2 == 0:
			even.append(i)
		else:
			odd.append(i)
	return check_n_run(n, len(even), len(odd), even, odd)
t = int(input())
for cases in range(t):
	n = int(input())
	eins = input().split()
	eins = list(map(int, eins))
	print(solve())


import sys
input = sys.stdin.readline

def sv1(ls, start):
	sample = []
	for _ in ls:
		sample.append(start)
		start = 1 - start
	memo1 = []
	memo2 = []
	for i in range(len(ls)):
		if ls[i] != sample[i]:
			if ls[i] == 1:
				memo1.append(i)
			else:
				memo2.append(i)
	if not memo1:
		return 0
	ret = 0
	for i in range(0, len(memo1)):
		ret += abs(memo1[i] - memo2[i])
	return ret

def sv(ls):
	ls = [i % 2 for i in ls]
	odd = sum(ls)
	even = len(ls) - odd
	if abs(odd - even) > 1:
		return -1
	if odd > even:
		return sv1(ls, 1)
	elif odd < even:
		return sv1(ls, 0)
	else:
		return min(sv1(ls, 0), sv1(ls, 1))

def main():
	for _ in range(int(input())):
		input()
		ls = list(map(int, input().split()))
		print(sv(ls))
main()


for i in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	if n == 1:
		print('0')
		continue
	even_idx_pos = []
	odd_idx_pos = []
	even_pos = []
	for (idx, ele) in enumerate(arr):
		if ele % 2 == 0:
			even_pos.append(idx)
		if idx % 2 == 0:
			even_idx_pos.append(idx)
		else:
			odd_idx_pos.append(idx)
	res1 = 0
	res2 = 0
	if abs(len(even_pos) - (n - len(even_pos))) > 1:
		print('-1')
	elif len(even_pos) >= len(even_idx_pos) and len(even_pos) - (n - len(even_pos)) != 0:
		for i in range(len(even_pos)):
			res1 += abs(even_pos[i] - even_idx_pos[i])
		print(res1)
	elif len(even_pos) - (n - len(even_pos)) == 0:
		for i in range(len(even_pos)):
			res1 += abs(even_pos[i] - even_idx_pos[i])
		for i in range(len(even_pos)):
			res2 += abs(even_pos[i] - odd_idx_pos[i])
		print(min(res1, res2))
	else:
		for i in range(len(odd_idx_pos)):
			res2 += abs(even_pos[i] - odd_idx_pos[i])
		print(res2)


from collections import Counter, defaultdict, OrderedDict, deque
from bisect import bisect_left, bisect_right
from functools import reduce, lru_cache
from typing import List
import itertools
import math
import heapq
import string
import random
(MIN, MAX, MOD) = (-1061109567, 1061109567, 1000000007)
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')

def RL():
	return map(int, sys.stdin.readline().rstrip().split())

def RLL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def N():
	return int(input())

def solve(parity):
	cursor = 0
	for (i, x) in enumerate(a):
		if x & 1 == parity:
			yield abs(i - cursor)
			cursor += 2
for _ in range(N()):
	n = N()
	a = RLL()
	(odd, even) = (sum((1 for i in a if i & 1)), sum((1 for i in a if i & 1 ^ 1)))
	if abs(odd - even) > 1:
		print(-1)
		continue
	if odd > even:
		print(sum(solve(1)))
	elif even > odd:
		print(sum(solve(0)))
	else:
		print(min(sum(solve(1)), sum(solve(0))))


tc = int(input())
for _ in range(tc):
	n = int(input())
	arr = list(map(int, input().split()))
	(c1, c2) = (0, 0)
	c_even = 0
	c_odd = 0
	if n == 1:
		print(0)
		continue
	for i in range(len(arr)):
		if arr[i] % 2 == 0:
			c_even += 1
		else:
			c_odd += 1
	if abs(c_even - c_odd) > 1:
		print(-1)
		continue
	(ptr1, ptr2) = (0, 0)
	for i in range(len(arr)):
		if arr[i] % 2 == 0:
			c1 += abs(ptr1 - i)
			ptr1 += 2
		if arr[i] % 2 != 0:
			c2 += abs(ptr2 - i)
			ptr2 += 2
	if c_even > c_odd:
		print(c1)
	elif c_odd > c_even:
		print(c2)
	else:
		print(min(c1, c2))


t = int(input())

def odds_at_odd_pos(n, odds_pos):
	tot = 0
	j = 0
	for i in range(1, n, 2):
		tot += abs(i - odds_pos[j])
		j += 1
	return tot

def odds_at_even_pos(n, odds_pos):
	tot = 0
	j = 0
	for i in range(0, n, 2):
		tot += abs(i - odds_pos[j])
		j += 1
	return tot
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	odds_pos = []
	for i in range(n):
		if arr[i] % 2:
			odds_pos.append(i)
	odds = len(odds_pos)
	if n % 2 == 0:
		if odds == n // 2:
			print(min(odds_at_even_pos(n, odds_pos), odds_at_odd_pos(n, odds_pos)))
		else:
			print(-1)
	elif odds == n // 2:
		print(odds_at_odd_pos(n, odds_pos))
	elif odds == n // 2 + 1:
		print(odds_at_even_pos(n, odds_pos))
	else:
		print(-1)


class Dsu:

	def __init__(self, n):
		self.parent = list(range(n))
		self.rank = [1] * n

	def find(self, x):
		while x != self.parent[x]:
			self.parent[x] = self.parent[self.parent[x]]
			x = self.parent[x]
		return x

	def union(self, x, y):
		(px, py) = (self.find(x), self.find(y))
		if px == py:
			return 0
		if self.rank[py] > self.rank[px]:
			(px, py) = (py, px)
		self.parent[py] = px
		self.rank[px] += self.rank[py]
		return 1

	def get_size(self, x):
		return self.rank[self.find(x)]

class SegTree:

	def __init__(self, n, array):
		self.n = n
		self.tree = [0] * (2 * n)
		for i in range(n, 2 * n):
			self.tree[i] = array[i - n]
		for i in range(n - 1, -1, -1):
			self.tree[i] = self.tree[2 * i] + self.tree[2 * i + 1]

	def update(self, i, val):
		self.tree[i] = val
		while i:
			self.tree[i] = self.tree[i * 2] + self.tree[i * 2 + 1]
			i //= 2

	def query(self):
		pass

	def top(self):
		return self.tree[0]
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from math import ceil, floor, log, gcd, sqrt
from collections import Counter, deque
from heapq import heappush, heappop, heapify

def re():
	return stdin.readline().rstrip()

def ints():
	return map(int, stdin.readline().split())

def test(cases):
	for _ in range(cases):
		solve()
mod = 1000000007
inf = 1e+18
nl = '\n'

def solve():
	n = int(re())
	a = list(ints())
	(o, e, ocnt, ecnt) = ([], [], 0, 0)
	for i in range(n):
		if a[i] & 1:
			o.append(i)
			ocnt += 1
		else:
			e.append(i)
			ecnt += 1
	if n & 1 and abs(ocnt - ecnt) != 1:
		print(-1)
		return
	if not n & 1 and ocnt != ecnt:
		print(-1)
		return
	(ans, spot, ans2, spot2) = (0, 0, 0, 0)
	for i in range(ocnt):
		ans += abs(o[i] - spot)
		spot += 2
	for i in range(ecnt):
		ans2 += abs(e[i] - spot2)
		spot2 += 2
	if ocnt > ecnt:
		print(ans)
	elif ecnt > ocnt:
		print(ans2)
	else:
		print(min(ans, ans2))
	return
test(int(re()))


def func(arr, x):
	d = 0
	ans = 0
	n = len(arr)
	for i in range(n):
		if arr[i] != x:
			d += x - arr[i]
		ans += abs(d)
		x = 1 - x
	return ans
t = int(input())
for case in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] = a[i] % 2
	s0 = a.count(0)
	s1 = a.count(1)
	if n % 2 == 0:
		if s0 != s1:
			print(-1)
		else:
			print(min(func(a, 0), func(a, 1)))
	elif s0 == s1 + 1:
		print(func(a, 0))
	elif s1 == s0 + 1:
		print(func(a, 1))
	else:
		print(-1)


for _ in range(int(input())):
	n = int(input())
	arr = [int(x) for x in input().split()]
	odd = 0
	for i in arr:
		if i & 1:
			odd += 1
	even = n - odd
	if abs(odd - even) > 1:
		print(-1)
	else:
		ans = 0
		if n & 1:
			if odd < even:
				pos = 0
				for i in range(n):
					if arr[i] % 2 == 0:
						ans += abs(i - pos)
						pos += 2
			else:
				pos = 0
				for i in range(n):
					if arr[i] & 1:
						ans += abs(i - pos)
						pos += 2
		else:
			res1 = 0
			res2 = 0
			pos = 0
			for i in range(n):
				if arr[i] & 1:
					res1 += abs(i - pos)
					pos += 2
			pos = 0
			for i in range(n):
				if arr[i] % 2 == 0:
					res2 += abs(i - pos)
					pos += 2
			ans = min(res1, res2)
		print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	c = 0
	c1 = 0
	d = 0
	d1 = 0
	for j in l:
		if j % 2 == 0:
			c1 += abs(d - c)
			c += 1
		else:
			d1 += abs(c - d)
			d += 1
	if abs(c - d) > 1:
		print(-1)
	elif c > d:
		print(c1)
	elif d > c:
		print(d1)
	else:
		print(min(c1, d1))


from __future__ import division, print_function
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
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')
import collections, bisect, re, random, queue, itertools, statistics, math
from collections import *
from bisect import *
from itertools import *
from statistics import *
from math import *
from re import *
from queue import *

class Stack:

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

	def push(self, item):
		self.items.append(item)

	def pop(self):
		return self.items.pop()

	def empty(self):
		return self.items == []

	def size(self):
		return len(self.items)

	def at(self, idx):
		try:
			return self.items[idx]
		except:
			return -1

class priority_queue(object):

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

	def __str__(self):
		return ' '.join([str(i) for i in self.queue])

	def empty(self):
		return len(self.queue) == 0

	def insert(self, data):
		self.queue.append(data)

	def delete(self):
		try:
			max = 0
			for i in range(len(self.queue)):
				if self.queue[i] > self.queue[max]:
					max = i
			item = self.queue[max]
			del self.queue[max]
			return item
		except IndexError:
			print()
			exit()
mod = int(1000000000.0 + 7)
imin = -2147483648
imax = 2147483647
true = True
false = False
N = int(100000.0 + 9)
none = None
inp = lambda : input()
I = lambda : int(inp())
M = lambda : map(int, inp().split())
MS = lambda : map(str, inp().split())
S = lambda : list(MS())
L = lambda : list(M())
Yes = lambda : print('Yes')
No = lambda : print('No')
YES = lambda : print('YES')
NO = lambda : print('NO')
yes = lambda : print('yes')
no = lambda : print('no')
chartonum = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '0': 0}

def IO(controll):
	if controll == false:
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('uttar.txt', 'w')
	else:
		pass

def true_yes(statement):
	return YES() if statement else NO()

def binary(x):
	return bin(x).replace('0b', '')

def state(statement, one, two):
	print(one if statement else two)

def swap(a, b):
	(a, b) = (b, a)

def bintodec(n):
	return int(n, 2)

def prec(a):
	return '{0:.9f}'.format(a)

def case(t):
	print('Case #' + str(i) + ': ', end='')

def palin(s):
	return s == s[::-1]

def is_sorted(arr):
	return arr == sorted(arr)
IO(1)

def kabraji_ka_totka():
	n = I()
	arr = L()
	if n == 1:
		print(0)
		return
	even = []
	odd = []
	o_cnt = 0
	e_cnt = 0
	for i in range(n):
		if arr[i] % 2:
			o_cnt += 1
			odd.append(i)
		else:
			e_cnt += 1
			even.append(i)
	if abs(o_cnt - e_cnt) > 1:
		print(-1)
		return
	res1 = 0
	res2 = 0
	res = 0
	if n % 2:
		if len(even) > len(odd):
			for i in range(0, n, 2):
				res1 += abs(i - even[i // 2])
		else:
			for i in range(0, n, 2):
				res1 += abs(i - odd[i // 2])
		print(res1)
		return
	for i in range(0, n, 2):
		res1 += abs(i - odd[i // 2])
	for i in range(1, n, 2):
		res2 += abs(i - odd[i // 2])
	print(min(res1, res2))
for i in range(I()):
	kabraji_ka_totka()


import bisect
import collections
import functools
import heapq
import itertools
import math
import random
import sys
input = sys.stdin.readline

def ri():
	return int(input())

def rf():
	return list(map(float, input().split()))

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

def rs():
	return input().rstrip()

def out_list(x):
	return ' '.join(map(str, x))
MOD = 10 ** 9 + 7
INF = float('inf')

def solve_case():
	n = ri()
	A = rl()
	A = [a % 2 for a in A]
	res = INF
	for r in range(2):
		x = list(range(r, n, 2))
		y = [i for (i, a) in enumerate(A) if a % 2 == 0]
		if len(x) == len(y):
			cur = sum((abs(xi - yi) for (xi, yi) in zip(x, y)))
			res = min(res, cur)
	return res if res < INF else -1
T = int(input())
for test_case in range(1, T + 1):
	print(solve_case())


import sys
T = int(input())
for _ in range(T):
	n = int(input())
	a = list(map(int, sys.stdin.readline().split()))
	S = 0
	res = sys.maxsize
	x = 0
	for i in range(n):
		a[i] = a[i] % 2
	one = a.count(1)
	zero = a.count(0)
	if abs(one - zero) > 1:
		print(-1)
	elif one == zero:
		for j in range(2):
			S = 0
			x = 0
			for i in range(n):
				if a[i] == j:
					S += abs(i - x)
					x += 2
			res = min(S, res)
		print(res)
	else:
		if one > zero:
			j = 1
		else:
			j = 0
		for i in range(n):
			if a[i] == j:
				S += abs(i - x)
				x += 2
		res = min(S, res)
		print(res)


t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	p = [0, 0]
	c = [[], []]
	d = [[], []]
	for i in range(n):
		p[a[i] % 2] += 1
		c[i % 2] += [i]
		d[a[i] % 2] += [i]
	if abs(p[0] - p[1]) >= 2:
		print(-1)
		continue
	ans = 0
	if p[0] == p[1]:
		for i in range(len(d[0])):
			ans += abs(c[0][i] - d[0][i])
		for i in range(len(d[1])):
			ans += abs(c[1][i] - d[1][i])
		v = ans * 1
		ans = 0
		for i in range(len(d[0])):
			ans += abs(c[1][i] - d[0][i])
		for i in range(len(d[1])):
			ans += abs(c[0][i] - d[1][i])
		ans = min(ans, v)
	else:
		if len(d[0]) != len(c[0]):
			(c[0], c[1]) = (c[1], c[0])
		for i in range(len(d[0])):
			ans += abs(c[0][i] - d[0][i])
		for i in range(len(d[1])):
			ans += abs(c[1][i] - d[1][i])
	print(ans // 2)


import sys
import os.path
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
depth = 1000005
mod = 1000000007
lim = mod * mod
sys.setrecursionlimit(depth)

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

def mapinput():
	return map(int, input().split())
from math import inf, ceil, sqrt
from collections import defaultdict
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	evn = odd = 0
	for e in a:
		if e & 1:
			odd += 1
		else:
			evn += 1
	if n & 1:
		if abs(evn - odd) > 1:
			print(-1)
		elif evn > odd:
			posevn = []
			posodd = []
			for i in range(n):
				if i & 1 != a[i] & 1:
					if a[i] & 1:
						posodd.append(i)
					else:
						posevn.append(i)
			ans = 0
			for i in range(len(posodd)):
				ans += abs(posodd[i] - posevn[i])
			print(ans)
		else:
			posevn = []
			posodd = []
			for i in range(n):
				if i & 1 == a[i] & 1:
					if a[i] & 1:
						posodd.append(i)
					else:
						posevn.append(i)
			ans = 0
			for i in range(len(posodd)):
				ans += abs(posodd[i] - posevn[i])
			print(ans)
	elif evn != odd:
		print(-1)
	else:
		posevn = []
		posodd = []
		for i in range(n):
			if i & 1 == a[i] & 1:
				if a[i] & 1:
					posodd.append(i)
				else:
					posevn.append(i)
		ans = 0
		for i in range(len(posodd)):
			ans += abs(posodd[i] - posevn[i])
		ans1 = ans
		posevn = []
		posodd = []
		for i in range(n):
			if i & 1 != a[i] & 1:
				if a[i] & 1:
					posodd.append(i)
				else:
					posevn.append(i)
		ans = 0
		for i in range(len(posodd)):
			ans += abs(posodd[i] - posevn[i])
		ans = min(ans, ans1)
		print(ans)


def func(arr, N):
	odd = 0
	even = 0
	odds = []
	evens = []
	swaps = 0
	for i in range(N):
		if arr[i] % 2:
			odd += 1
			odds.append(i)
		else:
			even += 1
			evens.append(i)
	if N % 2 == 1:
		if odd != N // 2 and even != N // 2:
			return -1
		elif odd == N // 2:
			for i in range(N // 2):
				swaps += abs(odds[i] - (2 * i + 1))
			return swaps
		else:
			for i in range(N // 2):
				swaps += abs(evens[i] - (2 * i + 1))
			return swaps
	elif N % 2 == 0:
		if odd != N // 2:
			return -1
		else:
			ans1 = 0
			ans2 = 0
			for i in range(N // 2):
				ans1 += abs(odds[i] - (2 * i + 1))
				ans2 += abs(evens[i] - (2 * i + 1))
			return min(ans1, ans2)
N = int(input())
for _ in range(N):
	N = int(input())
	arr = list(map(int, input().split()))
	print(func(arr, N))


from sys import stdin, stdout, setrecursionlimit
from math import gcd, floor, sqrt, ceil
from collections import Counter, deque
setrecursionlimit(1000)
input = lambda : stdin.readline()
inp = lambda : int(input())
st = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
int_arr = lambda : list(map(int, input().strip().split()))
str_arr = lambda : list(map(str, input().split()))
get_str = lambda : map(str, input().strip().split())
get_int = lambda : map(int, input().strip().split())
get_float = lambda : map(float, input().strip().split())
mod = 1000000007
for _ in range(int(input())):
	n = inp()
	arr = int_arr()
	ans = float('inf')
	even = []
	for i in range(0, n):
		if arr[i] % 2 == 0:
			even.append(i)
	for i in range(0, 2):
		ind = []
		for j in range(i, n, 2):
			ind.append(j)
		if len(even) == len(ind):
			tot = 0
			for j in range(len(ind)):
				tot += abs(even[j] - ind[j])
			ans = min(ans, tot)
	print(ans if ans != float('inf') else -1)


from collections import *
from math import ceil

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

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

def imi():
	return map(int, input().split())
t = ii()
for _ in range(t):
	n = ii()
	ar = il()
	o = 0
	nep = n - n // 2
	x = 0
	y = 0
	mo = 1
	for i in range(n):
		if ar[i] % 2 == 1:
			x += abs(i - mo)
			y += abs(i - (mo - 1))
			mo += 2
			o += 1
	e = n - o
	if max(o, e) != nep:
		print(-1)
	elif o == e:
		print(min(x, y))
	elif o > e:
		print(y)
	else:
		print(x)


def solve(a, n):
	if n <= 1:
		return 0
	e = 0
	o = 0
	for i in a:
		if i % 2 == 0:
			e += 1
		else:
			o += 1
	if abs(e - o) > 1:
		return -1
	ans1 = 0
	ans2 = 0
	even = []
	odd = []
	for i in range(n):
		if a[i] % 2 == 0:
			even.append(i)
		else:
			odd.append(i)
	if len(odd) != n // 2 + n % 2:
		ans1 = float('inf')
	else:
		for i in range(len(even)):
			ans1 += abs(2 * i + 1 - even[i])
	if len(even) != n // 2 + n % 2:
		ans2 = float('inf')
	else:
		for i in range(len(odd)):
			ans2 += abs(2 * i + 1 - odd[i])
	if ans1 == ans2 == float('inf'):
		return -1
	else:
		return min(ans1, ans2)
for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	print(solve(a, n))


def main():
	n = int(input())
	for i in range(n):
		l = int(input())
		arr = list(map(int, input().split()))
		even = odd = 0
		evens = []
		odds = []
		for j in arr:
			if j % 2 == 0:
				even += 1
				evens.append(j)
			else:
				odds.append(j)
				odd += 1
		if abs(odd - even) > 1:
			print(-1)
		elif even < odd:
			pos = 0
			ans = 0
			for (p, j) in enumerate(arr):
				if j % 2 != 0:
					ans += abs(p - pos)
					pos += 2
			print(ans)
		elif even > odd:
			pos = 0
			ans = 0
			for (p, j) in enumerate(arr):
				if j & 1 == 0:
					ans += abs(p - pos)
					pos += 2
			print(ans)
		else:
			pos = 0
			ans = 0
			pos2 = 0
			ans2 = 0
			for (p, j) in enumerate(arr):
				if j & 1 == 0:
					ans += abs(p - pos)
					pos += 2
			for (p, j) in enumerate(arr):
				if j & 1 == 1:
					ans2 += abs(p - pos2)
					pos2 += 2
			print(min(ans2, ans))
main()


import sys
input = sys.stdin.readline

def main():
	N = int(input())
	A = list(map(lambda x: int(x) % 2, input().split()))
	Zero = []
	One = []
	for i in range(N):
		if A[i]:
			One.append(i)
		else:
			Zero.append(i)
	if len(Zero) < N // 2 or len(One) < N // 2:
		return print(-1)
	if N % 2:
		Ans = 0
		if len(Zero) < len(One):
			Zero = One
		for i in range(len(Zero)):
			Ans += abs(Zero[i] - i * 2)
	else:
		Ans1 = 0
		Ans2 = 0
		for i in range(N // 2):
			Ans1 += abs(Zero[i] - i * 2)
			Ans2 += abs(One[i] - i * 2)
		Ans = min(Ans1, Ans2)
	print(Ans)
T = int(input())
for _ in range(T):
	main()


import sys
input = sys.stdin.readline
INF = int(1000000000.0) + 7
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]

def solve():
	n = int(input())
	data = list(map(int, input().split()))
	even = 0
	odd = 0
	for i in data:
		if i % 2 == 0:
			even += 1
		else:
			odd += 1
	if n % 2 == 0:
		if even > n // 2 or odd > n // 2:
			print(-1)
			return
	elif even > n // 2 + 1 or odd > n // 2 + 1:
		print(-1)
		return
	if n % 2 == 0:
		result1 = 0
		result2 = 0
		cnt = 0
		for i in range(n):
			if data[i] % 2 == 0:
				result1 += abs(cnt * 2 - i)
				result2 += abs(cnt * 2 + 1 - i)
				cnt += 1
		print(min(result1, result2))
	elif even == n // 2 + 1:
		result1 = 0
		cnt = 0
		for i in range(n):
			if data[i] % 2 == 0:
				result1 += abs(cnt * 2 - i)
				cnt += 1
		print(result1)
	else:
		result1 = 0
		cnt = 0
		for i in range(n):
			if data[i] % 2 == 1:
				result1 += abs(cnt * 2 - i)
				cnt += 1
		print(result1)
t = int(input())
while t:
	t -= 1
	solve()


def main():
	t = int(input())
	n = [None for _ in range(t)]
	a = [[] for _ in range(t)]
	for i in range(t):
		n[i] = int(input())
		a[i] = list(map(int, input().split()))
	ans = [10 ** 18 for _ in range(t)]
	for i in range(t):
		odd = 0
		even = 0
		for j in range(n[i]):
			if a[i][j] % 2 == 0:
				even += 1
			else:
				odd += 1
		if abs(even - odd) >= 2:
			ans[i] = -1
			continue
		if odd >= even:
			odd_count = 0
			move_count = 0
			for j in range(n[i]):
				if a[i][j] % 2 != 0:
					if odd_count < 0:
						move_count += -odd_count
					odd_count += 1
				else:
					if odd_count > 1:
						move_count += odd_count - 1
					odd_count -= 1
			ans[i] = min(ans[i], move_count)
		if even >= odd:
			even_count = 0
			move_count = 0
			for j in range(n[i]):
				if a[i][j] % 2 == 0:
					if even_count < 0:
						move_count += -even_count
					even_count += 1
				else:
					if even_count > 1:
						move_count += even_count - 1
					even_count -= 1
			ans[i] = min(ans[i], move_count)
	for x in ans:
		print(x)
main()


loop = int(input())
while loop > 0:
	loop -= 1
	evenCount = 0
	evenIndex = [0]
	oddIndex = [0]
	n = int(input())
	arr = [0]
	rawInput = list(input().split())
	for i in range(1, n + 1):
		if int(rawInput[i - 1]) % 2 == 0:
			arr.append(True)
			evenCount += 1
			evenIndex.append(i)
		else:
			arr.append(False)
			oddIndex.append(i)
	swapsEven = 0
	swapsOdd = 0
	t = n // 2
	if n % 2 == 0 and evenCount != t:
		print(-1)
	elif n % 2 != 0 and (evenCount != t and evenCount != t + 1):
		print(-1)
	elif n % 2 == 0:
		j = 1
		for i in range(1, len(evenIndex)):
			swapsEven += abs(evenIndex[i] - j)
			j += 2
		j = 1
		for i in range(1, len(oddIndex)):
			swapsOdd += abs(oddIndex[i] - j)
			j += 2
		print(min(swapsOdd, swapsEven))
	elif len(evenIndex) > len(oddIndex):
		j = 1
		for i in range(1, len(evenIndex)):
			swapsEven += abs(evenIndex[i] - j)
			j += 2
		print(swapsEven)
	else:
		j = 1
		for i in range(1, len(oddIndex)):
			swapsOdd += abs(oddIndex[i] - j)
			j += 2
		print(swapsOdd)


import sys
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nsmallest
from math import ceil, floor, gcd, fabs, factorial, fmod, sqrt, inf
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
from decimal import Decimal
mod = pow(10, 9) + 7

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(str(var) + '\n')

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

def l():
	return list(sp())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

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

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]
for _ in range(int(data())):
	n = int(data())
	arr = l()
	for i in range(n):
		arr[i] &= 1
	(one, zero) = (arr.count(1), arr.count(0))
	if abs(one - zero) >= 2:
		out(-1)
		continue
	result = inf
	prev = -1
	ans = 0
	if n % 2 == 0 or (n % 2 == 1 and one <= zero):
		(o, e) = ([], [])
		for i in range(n):
			if i % 2 == 0 and arr[i]:
				o.append(i)
			if i % 2 == 1 and (not arr[i]):
				e.append(i)
		while o:
			ans += abs(o.pop() - e.pop())
		result = min(ans, result)
	ans = 0
	if n % 2 == 0 or (n % 2 == 1 and zero <= one):
		(o, e) = ([], [])
		for i in range(n):
			if i % 2 == 0 and (not arr[i]):
				o.append(i)
			if i % 2 == 1 and arr[i]:
				e.append(i)
		while o:
			ans += abs(o.pop() - e.pop())
		result = min(ans, result)
	out(result)


def diff(a, b):
	ans = 0
	for i in range(len(a)):
		ans += abs(a[i] - b[i])
	return ans
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	odd = [[], []]
	even = [[], []]
	for i in range(n):
		if a[i] & 1:
			if i & 1:
				odd[1].append(i)
			else:
				odd[0].append(i)
		elif i & 1:
			even[1].append(i)
		else:
			even[0].append(i)
	if abs(len(odd[0]) + len(odd[1]) - len(even[0]) - len(even[1])) <= 1:
		ans = float('inf')
		if len(odd[0]) == len(even[1]):
			ans = min(ans, diff(odd[0], even[1]))
		if len(odd[1]) == len(even[0]):
			ans = min(ans, diff(odd[1], even[0]))
		print(ans)
	else:
		print('-1')


for ii in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(o, e, oo, ee, co, x) = ([], [], [], [], 0, 0)
	for jj in range(n):
		if a[jj] % 2:
			o.append(jj)
		else:
			e.append(jj)
	if n % 2 == 0:
		if len(o) == len(e):
			for jj in range(len(o)):
				co += abs(o[jj] - x)
				x += 2
			(co1, x) = (0, 0)
			for jj in range(len(o)):
				co1 += abs(e[jj] - x)
				x += 2
			co = min(co, co1)
		else:
			co = -1
	elif abs(len(o) - len(e)) == 1:
		if len(o) > len(e):
			for jj in range(len(o)):
				co += abs(o[jj] - x)
				x += 2
		else:
			for jj in range(len(e)):
				co += abs(e[jj] - x)
				x += 2
	else:
		co = -1
	print(co)


import math

def solve():
	n = int(input())
	odds = []
	for i in range(0, n, 2):
		odds.append(i)
	evens = []
	for i in range(1, n, 2):
		evens.append(i)
	a = list(map(int, input().split()))
	for i in range(n):
		a[i] %= 2
	oneindex = []
	size1 = 0
	size0 = 0
	for i in range(n):
		if a[i] == 1:
			oneindex.append(i)
			size1 += 1
		else:
			size0 += 1
	if abs(size1 - size0) > 1:
		return -1
	elif size1 > size0:
		ans = 0
		for i in range(size1):
			ans += abs(odds[i] - oneindex[i])
		return ans
	elif size1 < size0:
		ans = 0
		for i in range(size1):
			ans += abs(evens[i] - oneindex[i])
		return ans
	elif size1 == size0:
		ans = 0
		ans1 = 0
		for i in range(size1):
			ans += abs(evens[i] - oneindex[i])
			ans1 += abs(odds[i] - oneindex[i])
		return min(ans, ans1)
for _ in range(int(input())):
	print(solve())


T = int(input())

def solve():
	n = int(input())
	arr = [int(x) for x in input().split()]
	(ch, nch) = (0, 0)
	for a in arr:
		if a % 2 == 0:
			ch += 1
		else:
			nch += 1
	if ch == nch:
		finalAns = float('inf')
		for (target, empty) in [(1, 0), (0, 0), (0, 1), (1, 1)]:
			ans = 0
			for (i, v) in enumerate(arr):
				if v % 2 == target:
					ans += abs(empty - i)
					empty += 2
			finalAns = min(finalAns, ans)
		print(finalAns)
		return
	elif abs(ch - nch) == 1:
		target = 1 if nch > ch else 0
		empty = 0
		ans = 0
		for (i, v) in enumerate(arr):
			if v % 2 == target:
				ans += abs(empty - i)
				empty += 2
		print(ans)
		return
	print(-1)
for _ in range(T):
	solve()


import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	b = []
	c = []
	for i in range(n):
		if a[i] % 2:
			b.append(i)
		else:
			c.append(i)
	ans = 0
	if abs(len(b) - len(c)) > 1:
		ans = -1
	elif abs(len(b) - len(c)) == 1:
		if len(b) > len(c):
			for i in range(n // 2 + 1):
				ans += abs(2 * i - b[i])
		else:
			for i in range(n // 2 + 1):
				ans += abs(2 * i - c[i])
	else:
		(ans1, ans2) = (0, 0)
		for i in range(n // 2):
			ans1 += abs(2 * i - b[i])
		for i in range(n // 2):
			ans2 += abs(2 * i - c[i])
		ans = min(ans1, ans2)
	print(ans)


import bisect
import heapq
import math
import collections
import sys
import copy
from functools import reduce
import decimal
from io import BytesIO, IOBase
import os
import itertools
import functools
from types import GeneratorType
sys.setrecursionlimit(10 ** 9)
decimal.getcontext().rounding = decimal.ROUND_HALF_UP
graphDict = collections.defaultdict
queue = collections.deque()

def bootstrap(f, stack=[]):

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

class Graphs:

	def __init__(self):
		self.graph = graphDict(set)

	def add_edge(self, u, v):
		self.graph[u].add(v)

	def dfs_utility(self, nodes, visited_nodes):
		visited_nodes.add(nodes)
		for neighbour in self.graph[nodes]:
			if neighbour not in visited_nodes:
				self.dfs_utility(neighbour, visited_nodes)

	def dfs(self, node):
		Visited = set()
		self.dfs_utility(i, Visited)

	def bfs(self, node, f_node):
		count = float('inf')
		visited = set()
		level = 0
		if node not in visited:
			queue.append([node, level])
			visited.add(node)
		flag = 0
		while queue:
			parent = queue.popleft()
			if parent[0] == f_node:
				flag = 1
				count = min(count, parent[1])
			level = parent[1] + 1
			for item in self.graph[parent[0]]:
				if item not in visited:
					queue.append([item, level])
					visited.add(item)
		return count if flag else -1

class Tree:

	def __init__(self, data):
		self.data = data
		self.left = None
		self.right = None

def inorder(node, lis):
	if node:
		inorder(node.left, lis)
		lis.append(node.data)
		inorder(node.right, lis)
	return lis

def leaf_node_sum(root):
	if root is None:
		return 0
	if root.left is None and root.right is None:
		return root.data
	return leaf_node_sum(root.left) + leaf_node_sum(root.right)

def hight(root):
	if root is None:
		return -1
	if root.left is None and root.right is None:
		return 0
	return max(hight(root.left), hight(root.right)) + 1

class UF:

	def __init__(self, N):
		self._id = list(range(N))
		self._count = N
		self._rank = [0] * N

	def find(self, p):
		id = self._id
		while p != id[p]:
			p = id[p] = id[id[p]]
		return p

	def count(self):
		return self._count

	def connected(self, p, q):
		return self.find(p) == self.find(q)

	def union(self, p, q):
		id = self._id
		rank = self._rank
		i = self.find(p)
		j = self.find(q)
		if i == j:
			return
		self._count -= 1
		if rank[i] < rank[j]:
			id[i] = j
		elif rank[i] > rank[j]:
			id[j] = i
		else:
			id[j] = i
			rank[i] += 1

	def __str__(self):
		return ' '.join([str(x) for x in self._id])

	def __repr__(self):
		return 'UF(' + str(self) + ')'

def rounding(n):
	return int(decimal.Decimal(f'{n}').to_integral_value())

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0), []))

def p_sum(array):
	return list(itertools.accumulate(array))
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, **kwargs):
		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 inp():
	return sys.stdin.readline().strip()

def map_inp(v_type):
	return map(v_type, inp().split())

def list_inp(v_type):
	return list(map_inp(v_type))
for _ in range(int(inp())):
	n = int(inp())
	arr = list_inp(int)
	e = []
	o = []
	for i in range(n):
		if arr[i] & 1:
			o.append(i)
		else:
			e.append(i)
	if abs(len(o) - len(e)) > 1:
		print(-1)
	else:
		first = []
		second = []
		a = 0
		b = 0
		c = 0
		d = 0
		for i in range(n):
			if i & 1:
				if a < len(e):
					first.append(e[a])
				if b < len(o):
					second.append(o[b])
				a += 1
				b += 1
			else:
				if c < len(o):
					first.append(o[c])
				if d < len(e):
					second.append(e[d])
				c += 1
				d += 1
		ans1 = 0
		if len(first) == n:
			for i in range(n):
				ans1 += abs(first[i] - i)
		else:
			ans1 = 7767564545478
		ans2 = 0
		if len(second) == n:
			for i in range(n):
				ans2 += abs(second[i] - i)
		else:
			ans2 = 4565665667677
		print(min(ans1, ans2) // 2)


for _ in range(int(input().strip())):
	n = int(input().strip())
	arr = list(map(int, input().strip().split()))
	even = 0
	even_swap = 0
	odd_swap = 0
	odd = 0
	for i in arr:
		if i % 2 == 0:
			even_swap += abs(even - odd)
			even += 1
		else:
			odd_swap += abs(even - odd)
			odd += 1
	if abs(even - odd) > 1:
		print(-1)
	elif even > odd:
		print(even_swap)
	elif odd > even:
		print(odd_swap)
	else:
		print(min(even_swap, odd_swap))


def doWork():
	n = int(input())
	a = map(int, input().split())
	a = [x % 2 for x in a]
	pos1 = [i for (i, x) in enumerate(a) if x == 0]
	pos2 = [i for (i, x) in enumerate(a) if x == 1]
	if abs(len(pos1) - len(pos2)) > 1:
		print(-1)
		return
	if len(pos1) > len(pos2):
		res = sum([abs(pos - i * 2) for (i, pos) in enumerate(pos1)])
	elif len(pos1) < len(pos2):
		res = sum([abs(pos - i * 2) for (i, pos) in enumerate(pos2)])
	else:
		res = min(sum([abs(pos - i * 2) for (i, pos) in enumerate(pos1)]), sum([abs(pos - i * 2) for (i, pos) in enumerate(pos2)]))
	print(res)
t = int(input())
for _ in range(t):
	doWork()


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))[:n]
	c = 0
	c1 = 0
	d = 0
	d1 = 0
	for j in arr:
		if j % 2 == 0:
			c1 += abs(d - c)
			c += 1
		else:
			d1 += abs(c - d)
			d += 1
	if abs(c - d) > 1:
		print(-1)
	elif c > d:
		print(c1)
	elif d > c:
		print(d1)
	else:
		print(min(c1, d1))


import sys
inputt = sys.stdin.readline
printt = sys.stdout.write
import math
import functools
from collections import deque
import heapq
from queue import PriorityQueue

def get():
	return inputt().split()

def getint():
	return int(inputt())

def getints():
	return map(int, inputt().split())

def f(start, a):
	c = [x for x in a]
	search = [0, 0]
	n = 0
	for i in range(len(c)):
		x = c[i]
		if x != start:
			search[start] = max(i, search[start])
			for j in range(search[start], len(c)):
				if c[j] == start:
					search[start] = j
					break
			n += j - i
			(c[i], c[j]) = (c[j], c[i])
		start = 1 - start
	return n
for _ in range(getint()):
	n = getint()
	a = list(getints())
	odd = 0
	b = []
	for x in a:
		if x % 2:
			odd += 1
		b.append(x % 2)
	if n % 2 == 0 and odd == n // 2:
		print(min(f(0, b), f(1, b)))
	elif n % 2 != 0 and odd == n // 2:
		print(f(0, b))
	elif n % 2 != 0 and odd == n // 2 + 1:
		print(f(1, b))
	else:
		print(-1)


from os import path
from io import BytesIO, IOBase
import sys
from heapq import heappush, heappop
from functools import cmp_to_key as ctk
from collections import deque, Counter, defaultdict as dd
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from itertools import permutations
from datetime import datetime
from math import ceil, sqrt, log, gcd

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

def si():
	return input().rstrip()

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

def li():
	return list(mi())
abc = 'abcdefghijklmnopqrstuvwxyz'
mod = 1000000007
inf = float('inf')
vow = ['a', 'e', 'i', 'o', 'u']
(dx, dy) = ([-1, 1, 0, 0], [0, 0, 1, -1])

def bo(i):
	return ord(i) - ord('0')
file = 1

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

def solve():
	for _ in range(1, ii() + 1):
		n = ii()
		a = li()
		for i in range(n):
			a[i] %= 2
		x = a.count(0)
		if x == n // 2 or x == n - n // 2:
			(x, y) = ([], [])
			for i in range(n - 1, -1, -1):
				if a[i]:
					x.append(i)
					y.append(i)
			ans = inf
			if len(x) == (n + 1) // 2:
				cnt = 0
				for i in range(n):
					if i % 2 == 0:
						cnt += abs(x[-1] - i)
						x.pop()
				ans = min(ans, cnt)
			if len(y) == n // 2:
				cnt = 0
				for i in range(n):
					if i % 2:
						cnt += abs(y[-1] - i)
						y.pop()
				ans = min(ans, cnt)
			print(ans)
			pass
		else:
			print(-1)
if file:
	if path.exists('input.txt'):
		sys.stdin = open('input.txt', 'r')
		sys.stdout = open('output.txt', 'w')
	else:
		input = sys.stdin.readline
solve()


def get_min_swaps(arr, start_pos):
	ans = 0
	odd_idx = 0
	if start_pos == 0:
		odd_idx = 1
	for i in range(start_pos, len(arr), 2):
		if arr[i] % 2 != 0:
			for j in range(odd_idx, len(arr), 2):
				if arr[j] % 2 != 1:
					odd_idx = j
					break
			ans += abs(odd_idx - i)
			odd_idx += 2
	return ans
T = int(input())
while T > 0:
	T -= 1
	n = int(input())
	arr = [int(i) for i in input().split(' ')]
	num_odd = 0
	for i in arr:
		if i % 2 == 1:
			num_odd += 1
	num_even = len(arr) - num_odd
	if abs(num_odd - num_even) > 1:
		print(-1)
	else:
		if n % 2 == 0:
			ans1 = get_min_swaps(arr, 0)
			ans2 = get_min_swaps(arr, 1)
			ans = min(ans1, ans2)
		elif num_odd > num_even:
			ans = get_min_swaps(arr, 1)
		else:
			ans = get_min_swaps(arr, 0)
		print(ans)


import math
for _ in range(int(input())):
	input()
	arr = list(map(int, input().split()))
	odd = 0
	even = 0
	swap = 0
	arr1 = []
	arr2 = []
	for i in arr:
		if i % 2 == 0:
			even += 1
		else:
			odd += 1
	if math.fabs(even - odd) > 1:
		print(-1)
		continue
	if even > odd:
		for i in range(len(arr)):
			if i % 2 == 0:
				if arr[i] % 2 != 0:
					arr1.append(i)
			elif arr[i] % 2 != 1:
				arr2.append(i)
		for i in zip(arr1, arr2):
			swap += math.fabs(i[0] - i[1])
	elif odd > even:
		for i in range(len(arr)):
			if i % 2 == 1:
				if arr[i] % 2 != 0:
					arr1.append(i)
			elif arr[i] % 2 != 1:
				arr2.append(i)
		for i in zip(arr1, arr2):
			swap += math.fabs(i[0] - i[1])
	else:
		(swap1, swap2) = (0, 0)
		for i in range(len(arr)):
			if i % 2 == 0:
				if arr[i] % 2 != 0:
					arr1.append(i)
			elif arr[i] % 2 != 1:
				arr2.append(i)
		for i in zip(arr1, arr2):
			swap1 += math.fabs(i[0] - i[1])
		arr1 = []
		arr2 = []
		for i in range(len(arr)):
			if i % 2 == 1:
				if arr[i] % 2 != 0:
					arr1.append(i)
			elif arr[i] % 2 != 1:
				arr2.append(i)
		for i in zip(arr1, arr2):
			swap2 += math.fabs(i[0] - i[1])
		swap = min(swap1, swap2)
	print(int(swap))


t = int(input())
for tc in range(t):
	n = int(input())
	L = list(map(int, input().split()))
	oneL = []
	for i in range(len(L)):
		L[i] %= 2
		if L[i] == 1:
			oneL.append(i)
	s = len(oneL)
	halfu = (n + 1) // 2
	halfd = n // 2
	if halfd > s or halfu < s:
		print(-1)
	else:
		(ret, ret2) = (n ** 2, n ** 2)
		if s == halfu:
			ret = 0
			for i in range(len(oneL)):
				ret += abs(oneL[i] - 2 * i)
		if s == halfd:
			ret2 = 0
			for i in range(len(oneL)):
				ret2 += abs(oneL[i] - 2 * i - 1)
		print(min(ret, ret2))


import sys
testcases = int(input())
while testcases > 0:
	n = int(input())
	c = [0] + list(map(int, sys.stdin.readline().split()))
	a = []
	b = []
	countr = 0
	for i in range(1, n + 1):
		if c[i] % 2 == 0:
			a.append(i)
		else:
			b.append(i)
	if abs(len(a) - len(b)) > 1:
		countr = -1
	elif len(a) > len(b):
		for j in range(len(a)):
			countr += abs(a[j] - 2 * j - 1)
	elif len(a) < len(b):
		for x in range(len(b)):
			countr += abs(b[x] - 2 * x - 1)
	else:
		c1 = 0
		c2 = 0
		for y in range(len(a)):
			c1 += abs(a[y] - 2 * y - 1)
			c2 += abs(b[y] - 2 * y - 1)
		countr = min(c1, c2)
	print(countr)
	testcases -= 1


for test in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	ji = 0
	ou = 0
	ans1 = float('inf')
	ans2 = float('inf')
	for i in range(n):
		if a[i] % 2 == 1:
			ji += 1
		else:
			ou += 1
	if abs(ji - ou) > 1:
		print(-1)
	else:
		if ji - ou <= 0:
			ans1 = 0
			ouou = 0
			for i in range(0, n):
				if a[i] % 2 == 0:
					d = i - 2 * ouou
					if d < 0:
						d *= -1
					ans1 += d
					ouou += 1
		if ji - ou >= 0:
			ans2 = 0
			jiji = 0
			for i in range(0, n):
				if a[i] % 2 == 1:
					d = i - 2 * jiji
					if d < 0:
						d *= -1
					ans2 += d
					jiji += 1
		print(min(ans1, ans2))


for i in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	odd = even = codd = ceven = 0
	for i in s:
		if i % 2:
			odd += abs(ceven - codd)
			codd += 1
		else:
			even += abs(ceven - codd)
			ceven += 1
	if abs(codd - ceven) > 1:
		print(-1)
	elif ceven > codd:
		print(even)
	elif codd > ceven:
		print(odd)
	else:
		print(min(odd, even))


def next_even(nums, last):
	last += 1
	while last < len(nums) and nums[last] % 2 == 1:
		last += 1
	return last

def next_odd(nums, last):
	last += 1
	while last < len(nums) and nums[last] % 2 == 0:
		last += 1
	return last

def solve(nums, odd):
	changes = 0
	last_odd = next_odd(nums, -1)
	last_even = next_even(nums, -1)
	for x in range(len(nums)):
		if odd and nums[x] % 2 == 0:
			if last_odd == len(nums):
				return float('inf')
			(nums[x], nums[last_odd]) = (nums[last_odd], nums[x])
			changes += abs(last_odd - x)
			last_odd = next_odd(nums, last_odd)
		if not odd and nums[x] % 2 == 1:
			if last_even == len(nums):
				return float('inf')
			(nums[x], nums[last_even]) = (nums[last_even], nums[x])
			changes += abs(last_even - x)
			last_even = next_even(nums, last_even)
		if x >= last_even:
			last_even = next_even(nums, last_even)
		if x >= last_odd:
			last_odd = next_odd(nums, last_odd)
		odd = not odd
	return changes
for _ in range(int(input())):
	input()
	nums = list(map(int, input().split()))
	(total_odd, total_even) = (0, 0)
	for (pos, x) in enumerate(nums):
		if x % 2 == 0:
			total_even += 1
		else:
			total_odd += 1
	if abs(total_even - total_odd) > 1:
		print('-1')
		continue
	ans = min(solve(nums[:], True), solve(nums, False))
	print(ans)


import math
debug = True

def pprint(*args, delimiter='#', amount=10):
	if debug:
		print('{0} DEBUG {0}'.format(delimiter * amount))
		print(*args)
		print('{0} ----- {0}'.format('-' * amount))

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	even = [i for i in range(n) if a[i] % 2 == 0]
	odd = [i for i in range(n) if a[i] % 2 != 0]
	if n % 2 == 0 and len(even) != len(odd):
		return -1
	if n % 2 != 0 and abs(len(even) - len(odd)) != 1:
		return -1
	if n % 2 == 0:
		(ans_even, ans_odd) = (0, 0)
		index = 0
		for elem in even:
			ans_even += abs(elem - index)
			index += 2
		index = 0
		for elem in odd:
			ans_odd += abs(elem - index)
			index += 2
		return min(ans_even, ans_odd)
	ans = 0
	index = 0
	if len(odd) > len(even):
		for elem in odd:
			ans += abs(index - elem)
			index += 2
	else:
		for elem in even:
			ans += abs(index - elem)
			index += 2
	return ans
t = int(input())
while t > 0:
	t -= 1
	print(solve())


class Dsu:

	def __init__(self, n):
		self.parent = list(range(n))
		self.rank = [1] * n

	def find(self, x):
		while x != self.parent[x]:
			self.parent[x] = self.parent[self.parent[x]]
			x = self.parent[x]
		return x

	def union(self, x, y):
		(px, py) = (self.find(x), self.find(y))
		if px == py:
			return 0
		if self.rank[py] > self.rank[px]:
			(px, py) = (py, px)
		self.parent[py] = px
		self.rank[px] += self.rank[py]
		return 1

	def get_size(self, x):
		return self.rank[self.find(x)]

class SegTree:

	def __init__(self, n, array):
		self.n = n
		self.tree = [0] * (2 * n)
		for i in range(n, 2 * n):
			self.tree[i] = array[i - n]
		for i in range(n - 1, -1, -1):
			self.tree[i] = self.tree[2 * i] + self.tree[2 * i + 1]

	def update(self, i, val):
		self.tree[i] = val
		while i:
			self.tree[i] = self.tree[i * 2] + self.tree[i * 2 + 1]
			i //= 2

	def query(self):
		pass

	def top(self):
		return self.tree[0]
from sys import stdin, stdout
from bisect import bisect_left, bisect_right
from math import ceil, floor, log, gcd, sqrt
from collections import Counter, deque
from heapq import heappush, heappop, heapify

def re():
	return stdin.readline().rstrip()

def ints():
	return map(int, stdin.readline().split())

def test(cases):
	for _ in range(cases):
		solve()
mod = 1000000007
inf = 1e+18
nl = '\n'

def solve():
	n = int(re())
	a = list(ints())
	(o, e, ol, el) = (0, 0, [], [])
	for i in range(n):
		if a[i] & 1:
			o += 1
			ol.append(i)
		else:
			e += 1
			el.append(i)
	if n & 1 and abs(e - o) != 1:
		print(-1)
		return
	if not n & 1 and e != o:
		print(-1)
		return

	def miku(a, p):
		arr = a.copy()
		res = 0
		(po, pe) = (0, 0)
		for i in range(n):
			if arr[i] & 1 != p & 1:
				need = arr[i] & 1 ^ 1
				if need == 0:
					while el[pe] <= i:
						pe += 1
					nxt = el[pe]
					res += nxt - i
					(arr[i], arr[nxt]) = (arr[nxt], arr[i])
					pe += 1
				else:
					while ol[po] <= i:
						po += 1
					nxt = ol[po]
					res += nxt - i
					(arr[i], arr[nxt]) = (arr[nxt], arr[i])
					po += 1
			p += 1
		return res
	if o > e:
		print(miku(a, 1))
	elif e > o:
		print(miku(a, 0))
	else:
		print(min(miku(a, 0), miku(a, 1)))
	return
test(int(re()))


import sys
for _ in range(int(sys.stdin.readline())):
	n = int(sys.stdin.readline())
	x = [int(i) % 2 for i in sys.stdin.readline().split()]
	p = [idx for (idx, i) in enumerate(x) if i == 1]
	if n % 2 == 0 and len(p) != n // 2 or (n % 2 and len(p) not in [n // 2, n // 2 + 1]):
		print(-1)
		continue
	if n % 2:
		if len(p) == n // 2:
			print(sum((abs(i - idx * 2 - 1) for (idx, i) in enumerate(p))))
		else:
			print(sum((abs(i - idx * 2) for (idx, i) in enumerate(p))))
	else:
		print(min(sum((abs(i - idx * 2) for (idx, i) in enumerate(p))), sum((abs(i - idx * 2 - 1) for (idx, i) in enumerate(p)))))


def main():
	for _ in range(int(input())):
		n = int(input())
		a = list(map(int, input().split()))
		t = [x % 2 for x in a]
		(odd, even) = (t.count(1), t.count(0))
		if abs(odd - even) > 1:
			print(-1)
			continue
		ans1 = 0
		ans2 = 0
		if odd > even:
			curr = 0
			for i in range(n):
				if t[i] == 1:
					ans1 += abs(curr - i)
					curr += 2
			print(ans1)
		elif even > odd:
			curr = 0
			for i in range(n):
				if t[i] == 0:
					ans1 += abs(curr - i)
					curr += 2
			print(ans1)
		else:
			curr1 = 0
			curr2 = 1
			for i in range(n):
				if t[i] == 1:
					ans1 += abs(curr1 - i)
					ans2 += abs(curr2 - i)
					curr1 += 2
					curr2 += 2
			print(min(ans1, ans2))
main()


for _ in range(int(input())):
	N = int(input())
	A = [[], []]
	for (i, j) in enumerate(map(int, input().split())):
		A[j & 1].append(i)
	if len(A[0]) + 1 < len(A[1]) or len(A[1]) + 1 < len(A[0]):
		print(-1)
		continue
	ans = 2e+18
	for i in [0, 1]:
		if max(len(A[0]), len(A[1])) != len(A[i]):
			continue
		cur = 0
		for j in range(0, N, 2):
			cur += abs(j - A[i][j // 2])
		ans = min(ans, cur)
	print(ans)


from collections import deque
import math
test_cases = int(input())
for test_case in range(test_cases):
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	d = deque([])
	odd = 0
	even = 0
	for i in a:
		if i % 2 == 0:
			even += 1
		else:
			odd += 1
	if odd == even:
		e = 0
		o = 0
		for i in range(n):
			if a[i] % 2 != 0:
				d.append(i)
		for i in range(n):
			if i % 2 != 0:
				o += abs(i - d.popleft())
		d = deque([])
		for i in range(n):
			if a[i] % 2 == 0:
				d.append(i)
		for i in range(n):
			if i % 2 != 0:
				e += abs(i - d.popleft())
		ans = min(e, o)
	elif abs(odd - even) == 1:
		if odd > even:
			for i in range(n):
				if a[i] % 2 != 0:
					d.append(i)
			for i in range(n):
				if i % 2 == 0:
					ans += abs(i - d.popleft())
		elif odd < even:
			for i in range(n):
				if a[i] % 2 == 0:
					d.append(i)
			for i in range(n):
				if i % 2 == 0:
					ans += abs(i - d.popleft())
	else:
		ans = -1
	print(ans)


import bisect
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for _ in range(int(input())):
	n = int(input())
	ai = list(map(int, input().split()))
	o = 0
	oi = []
	e = 0
	ei = []
	for i in range(n):
		if ai[i] % 2 == 1:
			ai[i] = 1
			o += 1
			oi.append(i)
		else:
			ai[i] = 0
			e += 1
			ei.append(i)
	if n == 1:
		print(0)
	elif n == 2:
		if ai[0] != ai[1]:
			print(0)
		else:
			print(-1)
	elif abs(o - e) >= 2:
		print(-1)
	else:
		a1 = []
		a2 = []
		if n % 2 == 0:
			if abs(o - e) == 1:
				print(-1)
			else:
				ans1 = 0
				ans2 = 0
				for i in range(n):
					if i % 2 == 0:
						ans1 += abs(i - oi[i // 2])
					else:
						ans2 += abs(i - oi[i // 2])
				print(min(ans1, ans2))
		elif o > e:
			aa = 0
			for i in range(n):
				if i % 2 == 1:
					aa += abs(ei[i // 2] - i)
			print(aa)
		else:
			aa = 0
			for i in range(n):
				if i % 2 == 1:
					aa += abs(oi[i // 2] - i)
			print(aa)


for _ in ' ' * int(input()):
	n = int(input())
	a = [*map(lambda x: int(x) % 2, input().split())]
	c1 = a.count(1)
	if abs(n - 2 * c1) > 1:
		print(-1)
		continue
	c = 0
	r1 = 0
	for i in range(n):
		if a[i]:
			r1 += abs(c - i)
			c += 2
	if c1 < n - c1:
		r1 = float('inf')
	c = 0
	r0 = 0
	for i in range(n):
		if a[i] < 1:
			r0 += abs(c - i)
			c += 2
	if c1 > n - c1:
		r0 = float('inf')
	print(min(r0, r1))


def getMinimal(f, r):
	n = 0
	a = 0
	b = 0
	for i in range(len(f) + len(r)):
		if i % 2 == 0:
			n = n + abs(i - f[a])
			a = a + 1
		else:
			n = n + abs(i - r[b])
			b = b + 1
	return int(n / 2)
tn = int(input())
for t in range(tn):
	n = int(input())
	arr = input().split(' ')
	s1 = []
	s2 = []
	for i in range(len(arr)):
		arr[i] = int(arr[i]) % 2
		if arr[i] > 0:
			s1.append(i)
		else:
			s2.append(i)
	if abs(len(s1) - len(s2)) > 1:
		print(-1)
		continue
	if n % 2 > 0:
		if len(s1) > len(s2):
			print(getMinimal(s1, s2))
		else:
			print(getMinimal(s2, s1))
	else:
		print(min(getMinimal(s1, s2), getMinimal(s2, s1)))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().strip().split()))
	(e, ce, o, co) = (0, 0, 0, 0)
	for i in a:
		if i % 2 == 0:
			ce += abs(o - e)
			e += 1
		else:
			co += abs(o - e)
			o += 1
	if abs(e - o) > 1:
		print(-1)
	elif e > o:
		print(ce)
	elif o > e:
		print(co)
	else:
		print(min(ce, co))


for _ in range(int(input())):
	n = int(input())
	a = [int(x) for x in input().split()]
	e = 0
	o = 0
	odd = []
	for i in range(n):
		if a[i] & 1:
			o += 1
			a[i] = 1
			odd.append(i)
		else:
			e += 1
			a[i] = 0
	if abs(e - o) > 1:
		print(-1)
		continue
	ans = 99999999999999
	for i in [0, 1]:
		if o != (n + 1 - i) // 2:
			continue
		val = 0
		for j in range(o):
			val += abs(i + 2 * j - odd[j])
		ans = min(val, ans)
	print(ans)


mod = 1000000007
from sys import stdin, stdout
from collections import defaultdict, Counter, deque
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	li = list(map(int, stdin.readline().split()))
	if n == 1:
		print(0)
	elif n == 2:
		c = 0
		for ele in li:
			if ele % 2 == 0:
				c += 1
		if c == 1:
			print(0)
		else:
			print(-1)
	else:
		even = odd = 0
		p = 0
		evens = []
		for i in range(n):
			if li[i] % 2 == 0:
				even += 1
				evens.append(i)
		odd = n - even
		if odd >= n // 2 and even >= n // 2:
			if odd > even:
				x = 1
				s = 0
				for ele in evens:
					y = 2 * x - 1
					x += 1
					s += abs(y - ele)
				print(s)
			elif even > odd:
				x = 0
				s = 0
				for ele in evens:
					y = 2 * x
					x += 1
					s += abs(y - ele)
				print(s)
			else:
				x = 0
				s = 0
				for ele in evens:
					y = 2 * x
					x += 1
					s += abs(y - ele)
				s2 = 0
				x = 1
				for ele in evens:
					y = 2 * x - 1
					x += 1
					s2 += abs(y - ele)
				print(min(s2, s))
		else:
			print(-1)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = [int(j) for j in input().split()]
	even = []
	odd = []
	for i in range(n):
		if arr[i] % 2 == 0:
			even += [i]
		else:
			odd += [i]
	if n % 2 == 0 and len(even) != len(odd):
		print(-1)
		continue
	elif n % 2 == 1 and abs(len(even) - len(odd)) != 1:
		print(-1)
		continue
	evenfirst = []
	oddsecond = []
	oddfirst = []
	evensecond = []
	for i in range(n):
		if i % 2 == 0:
			evenfirst += [i]
		else:
			oddsecond += [i]
	for i in range(n):
		if i % 2 == 0:
			oddfirst += [i]
		else:
			evensecond += [i]
	if n % 2 == 0:
		(ans1, ans2) = (0, 0)
		(ptr1, ptr2) = (0, 0)
		for i in range(n):
			if arr[i] % 2 == 0:
				ans1 += abs(evenfirst[ptr1] - i)
				ptr1 += 1
			else:
				ans1 += abs(oddsecond[ptr2] - i)
				ptr2 += 1
		(ptr1, ptr2) = (0, 0)
		for i in range(n):
			if arr[i] % 2 == 0:
				ans2 += abs(evensecond[ptr1] - i)
				ptr1 += 1
			else:
				ans2 += abs(oddfirst[ptr2] - i)
				ptr2 += 1
		print(min(ans1, ans2) // 2)
	elif len(even) > len(odd):
		(ans1, ans2) = (0, 0)
		(ptr1, ptr2) = (0, 0)
		for i in range(n):
			if arr[i] % 2 == 0:
				ans1 += abs(evenfirst[ptr1] - i)
				ptr1 += 1
			else:
				ans1 += abs(oddsecond[ptr2] - i)
				ptr2 += 1
		print(ans1 // 2)
	else:
		ans2 = 0
		(ptr1, ptr2) = (0, 0)
		for i in range(n):
			if arr[i] % 2 == 0:
				ans2 += abs(evensecond[ptr1] - i)
				ptr1 += 1
			else:
				ans2 += abs(oddfirst[ptr2] - i)
				ptr2 += 1
		print(ans2 // 2)


G = int(input())
for g in range(G):
	n = int(input())
	val = list(map(int, input().split()))
	(a, b) = ([], [])
	for i in range(n):
		if val[i] & 1:
			a.append(i + 1)
		else:
			b.append(i + 1)
	A = len(a)
	B = len(b)
	if abs(A - B) > 1:
		print(-1)
		continue
	ans = 0
	if A > B:
		for i in range(A):
			ans += abs(a[i] - i * 2 - 1)
	elif A < B:
		for i in range(B):
			ans += abs(b[i] - i * 2 - 1)
	else:
		(ans1, ans2) = (0, 0)
		for i in range(A):
			ans1 += abs(a[i] - i * 2 - 1)
			ans2 += abs(b[i] - i * 2 - 1)
		ans = min(ans1, ans2)
	print(ans)


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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from math import ceil
mod = 10 ** 9 + 7
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(odd, even) = (0, 0)
	first = []
	second = []
	for i in range(n):
		if a[i] % 2:
			odd += 1
			first.append(i)
		else:
			even += 1
			second.append(i)
		a[i] = a[i] % 2
	if n == 1:
		print(0)
	elif n % 2 == 1:
		if abs(odd - even) != 1:
			print(-1)
		else:
			(swaps, at) = (0, 0)
			if odd > even:
				for i in range(odd):
					swaps += abs(at - first[i])
					at += 2
			else:
				for i in range(even):
					swaps += abs(at - second[i])
					at += 2
			print(swaps)
	elif n % 2 == 0:
		if abs(even - odd) != 0:
			print(-1)
		else:
			at = 0
			(s1, s2) = (0, 0)
			for i in range(even):
				s1 += abs(at - second[i])
				at += 2
			at = 0
			for i in range(odd):
				s2 += abs(at - first[i])
				at += 2
			print(min(s1, s2))


for __ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	a = 0
	a0 = 0
	b = 0
	b0 = 0
	for i in range(n):
		if s[i] % 2 == 0:
			a0 += abs(a - b)
			a += 1
		else:
			b0 += abs(a - b)
			b += 1
	if abs(a - b) > 1:
		print(-1)
	elif a > b:
		print(a0)
	elif b > a:
		print(b0)
	else:
		print(min(a0, b0))


import sys
input = sys.stdin.readline
from collections import Counter

def calc(A, X, k):
	ANS = 0
	i = 0
	j = 0
	for ind in range(k):
		while A[i] != 0:
			i += 1
		while X[j] != 0:
			j += 1
		ANS += abs(i - j)
		i += 1
		j += 1
	return ANS
t = int(input())
for tests in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	A = [a % 2 for a in A]
	C = Counter(A)
	ANS = 1 << 60
	if C[0] == C[1]:
		X = [0, 1] * (n // 2)
		ANS = min(ANS, calc(A, X, n // 2))
		X = [1, 0] * (n // 2)
		ANS = min(ANS, calc(A, X, n // 2))
		print(ANS)
	elif C[0] == C[1] + 1:
		X = [0, 1] * (n // 2) + [0]
		ANS = min(ANS, calc(A, X, n // 2 + 1))
		print(ANS)
	elif C[1] == C[0] + 1:
		X = [1, 0] * (n // 2) + [1]
		ANS = min(ANS, calc(A, X, n // 2))
		print(ANS)
	else:
		print(-1)


T = int(input())
for testcase in range(1, T + 1):
	n = int(input())
	a = list(map(int, input().split()))
	b = [x % 2 for x in a]
	x = b.count(0)
	y = n - x
	if abs(x - y) > 1:
		print(-1)
	elif abs(x - y) == 0:
		pos = [i for i in range(0, n, 2)]
		inds = [i for (i, x) in enumerate(b) if x == 0]
		count = 0
		for i in range(len(inds)):
			count += abs(inds[i] - pos[i])
		count1 = count
		inds = [i for (i, x) in enumerate(b) if x == 1]
		count = 0
		for i in range(len(inds)):
			count += abs(inds[i] - pos[i])
		print(min(count1, count))
	else:
		if x > y:
			v = 0
		else:
			v = 1
		inds = [i for (i, x) in enumerate(b) if x == v]
		pos = [i for i in range(0, n, 2)]
		count = 0
		for i in range(len(inds)):
			count += abs(inds[i] - pos[i])
		print(count)


from re import A
import sys
import math
import bisect
from sys import stdin, stdout
from math import gcd, floor, sqrt, log
from collections import defaultdict as dd
from bisect import bisect_left as bl, bisect_right as br
sys.setrecursionlimit(100000000)
inp = lambda : int(input())
strng = lambda : input().strip()
jn = lambda x, l: x.join(map(str, l))
strl = lambda : list(input().strip())
mul = lambda : map(int, input().strip().split())
mulf = lambda : map(float, input().strip().split())
seq = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
ceildiv = lambda x, d: x // d if x % d == 0 else x // d + 1
flush = lambda : stdout.flush()
stdstr = lambda : stdin.readline()
stdint = lambda : int(stdin.readline())
stdpr = lambda x: stdout.write(str(x))

def func():
	n = inp()
	l = seq()
	even = 0
	even_first = 0
	odd = 0
	odd_first = 0
	for i in range(n):
		if l[i] % 2:
			odd_first += abs(even - odd)
			odd += 1
		else:
			even_first += abs(even - odd)
			even += 1
	if abs(odd - even) > 1:
		return -1
	elif even > odd:
		return even_first
	elif odd > even:
		return odd_first
	else:
		return min(even_first, odd_first)
t = inp()
for i in range(t):
	print(func())


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	ce = 0
	co = 0
	c1 = 0
	d1 = 0
	for x in a:
		if x % 2 == 0:
			c1 += abs(co - ce)
			ce += 1
		else:
			d1 += abs(co - ce)
			co += 1
	if abs(co - ce) > 1:
		print(-1)
	elif ce > co:
		print(c1)
	elif ce < co:
		print(d1)
	else:
		print(min(c1, d1))


import sys

def main(N, A):
	C = [0] * 2
	for a in A:
		C[a & 1] += 1
	if abs(C[0] - C[1]) > 1:
		return -1
	ans = 10 ** 18
	for p in (0, 1):
		if C[p] < C[p ^ 1]:
			continue
		now = 0
		j = 0
		for i in range(N):
			if A[i] & 1 == p:
				now += abs(i - j)
				j += 2
		ans = min(ans, now)
	return ans
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	N = int(input())
	(*A,) = map(int, input().split())
	print(main(N, A))


from collections import Counter, deque
from math import *
mod = 1000000007

def solve():
	n = int(input())
	l = list(map(int, input().split()))
	d = {0: deque(), 1: deque()}
	odd = False
	for i in range(n):
		if l[i] % 2 == 0:
			d[0].append(i)
		else:
			d[1].append(i)
			if i == 0:
				odd = True
	if n % 2 == 0:
		if len(d[0]) != n // 2:
			print(-1)
			return
	if n % 2 != 0:
		if len(d[0]) != n // 2 and len(d[0]) != n // 2 + 1:
			print(-1)
			return
	if n == 1 or n == 2:
		print(0)
		return
	if n % 2 == 0:
		if odd:
			x = d[1]
			y = d[0]
		else:
			x = d[0]
			y = d[1]
		count = 0
		cur = 0
		for i in range(len(x)):
			count += abs(cur - x[i])
			cur += 2
		count1 = 0
		cur1 = 0
		for i in range(len(y)):
			count1 += abs(cur1 - y[i])
			cur1 += 2
		print(min(count, count1))
		return
	else:
		if len(d[0]) > len(d[1]):
			x = d[0]
			y = d[1]
		else:
			x = d[1]
			y = d[0]
		count = 0
		cur = 0
		for i in range(len(x)):
			count += abs(cur - x[i])
			cur += 2
		print(count)
		return
t = int(input())
for _ in range(t):
	solve()


def diff(x, y):
	if x >= y:
		return x - y
	return y - x
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	odd = []
	eve = []
	for i in range(n):
		if a[i] % 2 == 0:
			eve.append(i)
		else:
			odd.append(i)
	o = len(odd)
	e = len(eve)
	if o == e:
		ii = 0
		jj = 0
		ans1 = 0
		ans2 = 0
		for i in range(n):
			if i % 2 == 0:
				ans1 += diff(eve[ii], i)
				ii += 1
			else:
				ans1 += diff(odd[jj], i)
				jj += 1
		ii = 0
		jj = 0
		for i in range(n):
			if i % 2 != 0:
				ans2 += diff(eve[ii], i)
				ii += 1
			else:
				ans2 += diff(odd[jj], i)
				jj += 1
		print(min(ans1 // 2, ans2 // 2))
	elif e > o and e - o == 1:
		ii = 0
		jj = 0
		ans = 0
		for i in range(n):
			if i % 2 == 0:
				ans += diff(eve[ii], i)
				ii += 1
			else:
				ans += diff(odd[jj], i)
				jj += 1
		print(ans // 2)
	elif o > e and o - e == 1:
		ii = 0
		jj = 0
		ans = 0
		for i in range(n):
			if i % 2 != 0:
				ans += diff(eve[ii], i)
				ii += 1
			else:
				ans += diff(odd[jj], i)
				jj += 1
		print(ans // 2)
	else:
		print(-1)


from sys import stdin
from collections import defaultdict
from bisect import insort, bisect_left
from math import ceil

def solve(n, arr, start):
	ans = 0
	for i in range(n):
		if arr[i]:
			ans += abs(start - i)
			start += 2
	return ans
for _ in range(int(stdin.readline())):
	n = int(stdin.readline())
	arr = list(map(lambda x: int(x) % 2, stdin.readline().split()))
	if n == 1:
		print(0)
	else:
		odd = sum(arr)
		even = n - odd
		if abs(odd - even) > 1:
			print(-1)
		else:
			oddidx = 0
			if even == odd:
				print(min(solve(n, arr, 0), solve(n, arr, 1)))
			elif even > odd:
				print(solve(n, arr, 1))
			else:
				print(solve(n, arr, 0))


import sys
input = sys.stdin.readline
t = int(input())
out = []
for _ in range(t):
	n = int(input())
	l = list(map(int, input().split()))
	odd = []
	for i in range(n):
		if l[i] % 2 == 1:
			odd.append(i)
	poss = [10 ** 20]
	if len(odd) == n // 2:
		want = range(1, n, 2)
		assert len(want) == len(odd)
		val = 0
		for i in range(n // 2):
			val += abs(odd[i] - want[i])
		poss.append(val)
	if len(odd) == (n + 1) // 2:
		want = range(0, n, 2)
		assert len(want) == len(odd)
		val = 0
		for i in range((n + 1) // 2):
			val += abs(odd[i] - want[i])
		poss.append(val)
	o = min(poss)
	if o == 10 ** 20:
		o = -1
	out.append(o)
print('\n'.join(map(str, out)))


for _ in range(int(input())):
	n = int(input())
	take = list(map(int, input().split()))
	even_t = even_c = odd_t = odd_c = 0
	for i in take:
		if i % 2 == 0:
			even_t += abs(even_c - odd_c)
			even_c += 1
		else:
			odd_t += abs(even_c - odd_c)
			odd_c += 1
	if abs(odd_c - even_c) > 1:
		print(-1)
	elif even_c > odd_c:
		print(even_t)
	elif even_c < odd_c:
		print(odd_t)
	else:
		print(min(even_t, odd_t))


def fun(y):
	d = 0
	ans = 0
	for i in range(len(nums)):
		if nums[i] != y:
			d += y - nums[i]
		ans += abs(d)
		y ^= 1
	return ans

def count_operations(n, nums):
	f0 = 0
	f1 = 0
	for x in range(len(nums)):
		nums[x] = nums[x] % 2
		if nums[x] == 0:
			f0 += 1
		else:
			f1 += 1
	if n % 2 == 0:
		if f0 == f1:
			ans = min(fun(0), fun(1))
			return ans
		else:
			return -1
	elif f0 == f1 + 1:
		return fun(0)
	elif f1 == f0 + 1:
		return fun(1)
	else:
		return -1
t = int(input())
for y in range(t):
	n = int(input())
	nums = list(map(int, input().split()))
	print(count_operations(n, nums))


T = int(input())
for t in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	prev = A[0] % 2
	r = 1
	m = 0
	for a in A[1:]:
		if a % 2 != prev:
			if r == 1:
				prev = a % 2
			else:
				r -= 1
				m += r
		else:
			r += 1
	m = m if r == 1 else -1
	prev = A[0] % 2
	r = 1
	m2 = 0
	for a in A[1:]:
		if a % 2 != prev:
			if m2:
				if r == 1:
					prev = a % 2
				else:
					r -= 1
					m2 += r
			else:
				m2 += r
		else:
			r += 1
	m2 = m2 if r == 1 else -1
	print(m if m2 == -1 else m2 if m == -1 else min(m, m2))


import itertools
from sys import maxsize, stdout, stdin, stderr
mod = int(1000000000.0 + 7)
import sys

def I():
	return int(stdin.readline())

def lint():
	return [int(x) for x in stdin.readline().split()]

def S():
	return list(map(str, input().strip()))

def grid(r, c):
	return [lint() for i in range(r)]
from collections import defaultdict, Counter, deque
import math
import heapq
from heapq import heappop, heappush
import bisect
from math import inf
from itertools import groupby
from itertools import permutations as comb

def gcd(a, b):
	while b:
		a %= b
		tmp = a
		a = b
		b = tmp
	return a

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

def check_prime(n):
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def nCr(n, r):
	return fact(n) // (fact(r) * fact(n - r))

def fact(n):
	res = 1
	for i in range(2, n + 1):
		res = res * i
	return res

def primefactors(n):
	num = 0
	while n % 2 == 0:
		num += 1
		n = n / 2
	for i in range(3, int(math.sqrt(n)) + 1, 2):
		while n % i == 0:
			num += 1
			n = n // i
	if n > 2:
		num += 1
	return num

def ask(a, b, c):
	print(c, a, b, flush=True)
	n = I()
	return n

def linear_sieve(n):
	is_composite = [False] * n
	prime = []
	for i in range(2, n):
		if not is_composite[i]:
			prime.append(i)
		for j in prime:
			is_composite[i * j] = True
			if i % prime == 0:
				break
	return prime

def dfs(i, p, d):
	(a, tmp) = (0, 0)
	for j in d[i]:
		if j != p:
			a += 1
			tmp += dfs(j, i)
	if a == 0:
		return 0
	return tmp / a + 1

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(i)
			n = n // i
	if n > 2:
		l.append(n)
	return l
d = []
primes = []
prim = [0] * (10 ** 5 + 1)

def sieve(n):
	for i in range(n):
		d.append(i)
	for i in range(2, n):
		if d[i] == i:
			prim[i] = 1
			primes.append(i)
		j = 0
		while j < len(primes) and primes[j] <= d[i] and (primes[j] * i < n):
			d[i * primes[j]] = primes[j]
			j += 1

def primeFactors(n):
	factors = []
	while n != 1:
		factors.append(d[n])
		n //= d[n]
	return factors

def func(a):
	b = 1
	res = 0
	for i in range(len(a) - 1, -1, -1):
		res += b * int(a[i])
		b *= 2
	return res

def solA():
	t = 1
	t = I()
	for _ in range(t):
		(c, d) = lint()
		if abs(c - d) % 2 == 1:
			print(-1)
		elif c == 0 and d == 0:
			print(0)
		elif c == d:
			print(1)
		else:
			print(2)

def solb():
	t = I()
	for _ in range(t):
		n = I()
		s = lint()
		r = 0
		(e, o) = (0, 1)
		(a, b) = (0, 0)
		for i in range(n):
			if s[i] % 2 == 0:
				r += abs(i - o)
				a += 1
				o += 2
			else:
				r += abs(i - e)
				b += 1
				e += 2
		(e, o) = (0, 1)
		q = 0
		for i in range(n):
			if s[i] % 2 == 0:
				q += abs(i - e)
				e += 2
			else:
				q += abs(i - o)
				o += 2
		if min(a, b) < n // 2:
			print(-1)
		elif n % 2 == 1:
			if a < b:
				print(r // 2)
			else:
				print(q // 2)
		else:
			print(min(q, r) // 2)

def solc():
	n = I()
	s = lint()
	cnt = 0
	a = 0
	l = [0] * (n + 1)
	cnt = [0] * (n + 1)
	for i in range(n):
		if i % 2 == 0:
			a += s[i]
		else:
			p = a
			a -= min(p, s[i])
			if a >= 0:
				if a <= l[i - 1]:
					cnt[i + 1] = cnt[i - 1] + min(s[i], p)
				else:
					cnt[i + 1] = min(s[i], p)
			else:
				cnt[i + 1] = 0
			a -= s[i] - min(s[i], p)
			l[i + 1] = a
			if a < 0:
				a = 0
				l[i + 1] = -mod
	print(sum(cnt))

def sold():
	(n, k) = lint()
	o1 = ask(1, 2, 'and')
	a1 = ask(1, 2, 'or')
	c1 = o1 + a1
	o2 = ask(2, 3, 'and')
	a2 = ask(2, 3, 'or')
	c2 = o2 + a2
	o3 = ask(1, 3, 'and')
	a3 = ask(1, 3, 'or')
	c3 = o3 + a3
	a = [inf] * (n + 1)
	a[3] = (c3 + c2 - c1) // 2
	a[2] = c2 - a[3]
	a[1] = c1 - a[2]
	for i in range(4, n + 1):
		p = ask(1, i, 'and')
		q = ask(1, i, 'or')
		x = p + q
		a[i] = x - a[1]
	a.sort()
	print('finish', a[k - 1])
solb()


import sys, os, io
input = sys.stdin.readline
PI = 3.141592653589793
INF = float('inf')
MOD = 1000000007

def bin32(num):
	return '{0:032b}'.format(num)

def add(x, y):
	return (x + y) % MOD

def sub(x, y):
	return (x - y + MOD) % MOD

def mul(x, y):
	return x * y % MOD

def gcd(x, y):
	if y == 0:
		return x
	return gcd(y, x % y)

def lcm(x, y):
	return x * y // gcd(x, y)

def power(x, y):
	res = 1
	x %= MOD
	while y != 0:
		if y & 1:
			res = mul(res, x)
		y >>= 1
		x = mul(x, x)
	return res

def mod_inv(n):
	return power(n, MOD - 2)

def prob(p, q):
	return mul(p, power(q, MOD - 2))

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

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

def ls():
	return [i for i in input().split()]
for t in range(ii()):
	t += 1
	n = ii()
	a = li()
	if n == 1:
		print(0)
		continue
	for i in range(n):
		a[i] = a[i] % 2
	one = sum(a)
	zer = n - one
	if abs(one - zer) > 1:
		print(-1)
		continue
	g = [i % 2 for i in range(n)]
	h = [(i + 1) % 2 for i in range(n)]
	(x, y, z, w) = ([], [], [], [])
	for i in range(n):
		if g[i] != a[i] and g[i] == 0:
			x.append(i)
		if g[i] != a[i] and g[i] == 1:
			y.append(i)
		if h[i] != a[i] and h[i] == 0:
			w.append(i)
		if h[i] != a[i] and h[i] == 1:
			z.append(i)
	ans1 = INF
	ans2 = INF
	if len(x) == len(y):
		ans1 = 0
		for i in range(len(x)):
			ans1 += abs(x[i] - y[i])
	if len(w) == len(z):
		ans2 = 0
		for i in range(len(w)):
			ans2 += abs(z[i] - w[i])
	print(min(ans1, ans2))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	evens = []
	for (i, x) in enumerate(a):
		if x % 2 == 0:
			evens.append(i)
	evens.reverse()
	s = 0
	if n % 2:
		if len(evens) == n // 2:
			for j in range(1, n, 2):
				s += abs(j - evens.pop())
			print(s)
		elif len(evens) == n // 2 + 1:
			for j in range(0, n, 2):
				s += abs(j - evens.pop())
			print(s)
		else:
			print(-1)
	elif len(evens) == n // 2:
		copy = evens.copy()
		for j in range(1, n, 2):
			s += abs(j - evens.pop())
		s1 = 0
		for j in range(0, n, 2):
			s1 += abs(j - copy.pop())
		print(min(s, s1))
	else:
		print(-1)


def process(A):
	n = len(A)
	odds = []
	evens = []
	for i in range(n):
		if A[i] % 2 == 0:
			evens.append(i)
		else:
			odds.append(i)
	if n % 2 == 0:
		if len(evens) != n // 2:
			return -1
		answer1 = 0
		answer2 = 0
		for i in range(0, n, 2):
			answer1 += abs(odds[i // 2] - i)
			answer2 += abs(evens[i // 2] - i)
		return min(answer1, answer2)
	else:
		if len(odds) != n // 2 and len(evens) != n // 2:
			return -1
		if len(odds) == n // 2:
			answer = 0
			for i in range(0, n, 2):
				answer += abs(evens[i // 2] - i)
			return answer
		else:
			answer = 0
			for i in range(0, n, 2):
				answer += abs(odds[i // 2] - i)
			return answer
t = int(input())
for i in range(t):
	n = int(input())
	A = [int(x) for x in input().split()]
	print(process(A))


from sys import stdin, stdout, stderr

def getInts():
	return map(int, stdin.readline().split())

def getStrs():
	return stdin.readline().split()

def getInt():
	return int(stdin.readline())
for _ in range(getInt()):
	n = getInt()
	ar = list(getInts())
	ans = -1
	cnt = [0, 0]
	for i in range(n):
		ar[i] &= 1
		cnt[ar[i]] += 1

	def fun(x, a):
		res = 0
		j = 0
		for i in range(n):
			r = i & 1 ^ x
			if r == a[i]:
				a[i] = -1
				continue
			while j < n and (a[j] == -1 or r != a[j]):
				j += 1
			a[j] = r ^ 1
			a[i] = -1
			res += abs(j - i)
		return res
	m1 = n >> 1
	m2 = n - m1
	if n == 1:
		ans = 0
	elif cnt[0] == m1 or cnt[0] == m2:
		ans = float('inf')
		if cnt[0] == m1:
			ans = min(ans, fun(1, ar[0:n]))
		if cnt[0] == m2:
			ans = min(ans, fun(0, ar))
	stdout.write(f'{ans}\n')


import sys
input = sys.stdin.readline

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

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

def input_string():
	s = input()
	return list(s[:len(s) - 1])

def input_int_gen():
	return map(int, input().split())
from collections import Counter
tests = inp()
for _ in range(tests):
	n = inp()
	a = input_list()
	odd_count = len([x for x in a if x % 2 == 1])
	even_count = n - odd_count
	if not odd_count - even_count == 1 and (not odd_count - even_count == -1) and (not odd_count - even_count == 0):
		print(-1)
		continue
	ret = 0
	pos = 0
	if odd_count > even_count:
		for (i, x) in enumerate(a):
			if x % 2 == 1:
				ret += abs(i - pos)
				pos += 2
	elif even_count > odd_count:
		for (i, x) in enumerate(a):
			if x % 2 == 0:
				ret += abs(i - pos)
				pos += 2
	else:
		reteven = 0
		retodd = 0
		poseven = 0
		posodd = 0
		for (i, x) in enumerate(a):
			if x % 2 == 1:
				retodd += abs(i - posodd)
				posodd += 2
			if x % 2 == 0:
				reteven += abs(i - poseven)
				poseven += 2
		ret = min(reteven, retodd)
	print(ret)


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	odd = 0
	even = 0
	for a in arr:
		if a & 1:
			odd += 1
		else:
			even += 1
	if abs(odd - even) > 1:
		print(-1)
	else:
		l1 = []
		l2 = []
		l3 = []
		l4 = []
		for idx in range(n):
			if idx % 2 == 0 and arr[idx] % 2 == 1:
				l1.append(idx)
			elif idx % 2 == 1 and arr[idx] % 2 == 0:
				l2.append(idx)
			if idx % 2 == 0 and arr[idx] % 2 == 0:
				l3.append(idx)
			elif idx % 2 == 1 and arr[idx] % 2 == 1:
				l4.append(idx)
		if len(l1) + len(l2) == 0 or len(l3) + len(l4) == 0:
			print(0)
			continue
		ans1 = 0
		ans2 = 0
		if len(l1) == len(l2):
			i = 0
			while i < len(l1):
				ans1 += abs(l1[i] - l2[i])
				i += 1
		else:
			ans1 = 10000000000.0
		if len(l3) == len(l4):
			i = 0
			while i < len(l3):
				ans2 += abs(l3[i] - l4[i])
				i += 1
		else:
			ans2 = 10000000000.0
		ans = min(ans1, ans2)
		print(ans)


import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	odd = []
	even = []
	for i in range(len(a)):
		if a[i] % 2 == 1:
			odd.append(i)
		else:
			even.append(i)
	if abs(len(odd) - len(even)) > 1:
		print(-1, flush=False)
		return
	ans1 = 0
	ans2 = 10 ** 18
	cur = 0
	if len(odd) > len(even):
		(even, odd) = (odd, even)
	for i in range(len(even)):
		ans1 += abs(even[i] - cur)
		cur += 2
	cur = 0
	if len(odd) == len(even):
		ans2 = 0
		for i in range(len(odd)):
			ans2 += abs(odd[i] - cur)
			cur += 2
	print(min(ans1, ans2), flush=False)

def main():
	t = int(input())
	while t:
		solve()
		t -= 1
main()


for _ in range(int(input())):
	n = int(input())
	a = [int(i) for i in input().split()]
	(e, o, l, i) = (0, 0, [], 0)
	for el in a:
		if el % 2:
			o += 1
		else:
			e += 1
			l += [i]
		i += 1
	if abs(e - o) > 1:
		print(-1)
		continue
	(first_possibility, second_possibility, only_possibility) = (0, 0, 0)
	if e == o:
		for i in range(e):
			first_possibility += abs(i * 2 - l[i])
			second_possibility += abs(i * 2 + 1 - l[i])
		print(min(first_possibility, second_possibility))
		continue
	if e > o:
		for i in range(e):
			only_possibility += abs(2 * i - l[i])
		print(only_possibility)
	else:
		for i in range(e):
			only_possibility += abs(2 * i + 1 - l[i])
		print(only_possibility)


for _ in range(int(input())):
	n = int(input())
	s = list(map(int, input().split()))
	(cnt1, cnt2) = (0, 0)
	for x in s:
		if x % 2 == 0:
			cnt2 += 1
		else:
			cnt1 += 1
	if abs(cnt1 - cnt2) > 1:
		print(-1)
	elif cnt1 - cnt2 == 1:
		res = 0
		last1 = 0
		for i in range(n):
			if s[i] % 2 == 1:
				res += abs(last1 - i)
				last1 += 2
		print(res)
	elif cnt2 - cnt1 == 1:
		res = 0
		last2 = 0
		for i in range(n):
			if s[i] % 2 == 0:
				res += abs(last2 - i)
				last2 += 2
		print(res)
	else:
		minim = 10000000000000000
		res = 0
		(last1, last2) = (1, 0)
		for i in range(n):
			if s[i] % 2 == 1:
				res += abs(last1 - i)
				last1 += 2
		minim = min(minim, res)
		res = 0
		(last1, last2) = (0, 1)
		for i in range(n):
			if s[i] % 2 == 0:
				res += abs(last2 - i)
				last2 += 2
		minim = min(minim, res)
		print(minim)


import sys
import io, os
input = sys.stdin.buffer.readline
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	A = [a % 2 for a in A]
	O = []
	E = []
	for (i, a) in enumerate(A):
		if a == 0:
			E.append(i)
		else:
			O.append(i)
	if n % 2 == 1:
		if len(O) == n // 2:
			B = []
			for i in range(n):
				if i % 2 == 1:
					B.append(i)
			ans = 0
			for (i, j) in zip(O, B):
				ans += abs(i - j)
		elif len(O) == (n + 1) // 2:
			B = []
			for i in range(n):
				if i % 2 == 0:
					B.append(i)
			ans = 0
			for (i, j) in zip(O, B):
				ans += abs(i - j)
		else:
			ans = -1
		print(ans)
	else:
		if len(O) == n // 2:
			B1 = []
			for i in range(n):
				if i % 2 == 0:
					B1.append(i)
			ans1 = 0
			for (i, j) in zip(O, B1):
				ans1 += abs(i - j)
			B2 = []
			for i in range(n):
				if i % 2 == 1:
					B2.append(i)
			ans2 = 0
			for (i, j) in zip(O, B2):
				ans2 += abs(i - j)
			ans = min(ans1, ans2)
		else:
			ans = -1
		print(ans)


from bisect import insort, bisect_right, bisect_left
from sys import stdout, stdin, setrecursionlimit
from heapq import heappush, heappop, heapify
from io import BytesIO, IOBase
from collections import *
from itertools import *
from random import *
from string import *
from queue import *
from math import *
from re import *
from os import *

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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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 = read(self._fd, max(fstat(self._fd).st_size, 8192))
			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:
			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')
(stdin, stdout) = (IOWrapper(stdin), IOWrapper(stdout))
(graph, mod, szzz) = ({}, 10 ** 9 + 7, lambda : sorted(zzz()))

def getStr():
	return input()

def getInt():
	return int(input())

def listStr():
	return list(input())

def getStrs():
	return input().split()

def isInt(s):
	return '0' <= s[0] <= '9'

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

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

def output(answer, end='\n'):
	stdout.write(str(answer) + end)

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def getPrimes(N=10 ** 5):
	SN = int(sqrt(N))
	sieve = [i for i in range(N + 1)]
	sieve[1] = 0
	for i in sieve:
		if i > SN:
			break
		if i == 0:
			continue
		for j in range(2 * i, N + 1, i):
			sieve[j] = 0
	prime = [i for i in range(N + 1) if sieve[i] != 0]
	return prime

def primeFactor(n, prime=getPrimes()):
	lst = []
	mx = int(sqrt(n)) + 1
	for i in prime:
		if i > mx:
			break
		while n % i == 0:
			lst.append(i)
			n //= i
	if n > 1:
		lst.append(n)
	return lst
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
daysInMounth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
num = getInt()
for _ in range(num):
	n = getInt()
	arr = zzz()
	odd = []
	even = []
	for i in range(n):
		arr[i] = arr[i] % 2
		if arr[i]:
			odd.append(i)
		else:
			even.append(i)
	if abs(len(odd) - len(even)) >= 2:
		print(-1)
	elif len(odd) > len(even):
		j = 0
		ans = 0
		for i in range(1, n, 2):
			ans += abs(even[j] - i)
			j += 1
		print(ans)
	elif len(even) > len(odd):
		j = 0
		ans = 0
		for i in range(1, n, 2):
			ans += abs(odd[j] - i)
			j += 1
		print(ans)
	else:
		j = 0
		ans = 0
		for i in range(1, n, 2):
			ans += abs(even[j] - i)
			j += 1
		p1 = ans
		j = 0
		ans = 0
		for i in range(1, n, 2):
			ans += abs(odd[j] - i)
			j += 1
		print(min(p1, ans))


from collections import Counter
import string
import math
import bisect
import sys
from fractions import Fraction

def array_int():
	return [int(i) for i in sys.stdin.readline().split()]

def vary(arrber_of_variables):
	if arrber_of_variables == 1:
		return int(sys.stdin.readline())
	if arrber_of_variables >= 2:
		return map(int, sys.stdin.readline().split())

def makedict(var):
	return dict(Counter(var))
testcases = vary(1)
for _ in range(testcases):
	n = vary(1)
	num = array_int()
	odd = even = 0
	for i in range(n):
		if num[i] % 2:
			odd += 1
		else:
			even += 1
	if n % 2 == 0 and odd != even:
		print(-1)
	elif n % 2 and abs(odd - even) != 1:
		print(-1)
	elif even == odd:
		odd = []
		for i in range(n):
			if num[i] % 2:
				odd.append(i)
		ans1 = 0
		j = 0
		for i in range(n):
			if i % 2 == 0:
				ans1 += abs(odd[j] - i)
				j += 1
		even = []
		for i in range(n):
			if num[i] % 2 == 0:
				even.append(i)
		ans2 = 0
		j = 0
		for i in range(n):
			if i % 2 == 0:
				ans2 += abs(even[j] - i)
				j += 1
		print(min(ans1, ans2))
	elif even < odd:
		odd = []
		for i in range(n):
			if num[i] % 2:
				odd.append(i)
		ans1 = 0
		j = 0
		for i in range(n):
			if i % 2 == 0:
				ans1 += abs(odd[j] - i)
				j += 1
		print(ans1)
	else:
		even = []
		for i in range(n):
			if num[i] % 2 == 0:
				even.append(i)
		ans2 = 0
		j = 0
		for i in range(n):
			if i % 2 == 0:
				ans2 += abs(even[j] - i)
				j += 1
		print(ans2)


t = int(input())

def solve(arr1, arr2):
	index1 = [i for i in range(len(arr1)) if arr1[i] == 1]
	index2 = [i for i in range(len(arr2)) if arr2[i] == 1]
	return sum([abs(index1[i] - index2[i]) for i in range(len(index1))])
for _ in range(t):
	n = int(input())
	a = list(map(lambda x: int(x) % 2, input().split()))
	ones = a.count(1)
	zeros = n - ones
	if abs(ones - zeros) > 1:
		print(-1)
	else:
		x = [i % 2 for i in range(n)]
		y = [(i - 1) % 2 for i in range(n)]
		if ones > zeros:
			print(solve(a, y))
		elif zeros > ones:
			print(solve(a, x))
		else:
			print(min(solve(a, x), solve(a, y)))


def f(l):
	odd = []
	even = []
	for i in range(len(l)):
		if l[i] % 2 == 0:
			even.append(i)
		else:
			odd.append(i)
	if abs(len(odd) - len(even)) > 1:
		return -1
	ans = 0
	if len(odd) > len(even):
		for i in range(len(odd)):
			ans += abs(odd[i] - 2 * i)
		return ans
	if len(odd) < len(even):
		for i in range(len(even)):
			ans += abs(even[i] - 2 * i)
		return ans
	ans2 = 0
	for i in range(len(odd)):
		ans += abs(odd[i] - 2 * i)
	for i in range(len(odd)):
		ans2 += abs(odd[i] - (2 * i + 1))
	return min(ans, ans2)
n = int(input())
for i in range(n):
	a = input()
	lis = input()
	lis = lis.split()
	for i in range(len(lis)):
		lis[i] = int(lis[i])
	print(f(lis))


import sys
I = lambda x=' ': map(int, sys.stdin.readline().split(x))
pr = lambda *x, sep=' ', end='\n': sys.stdout.write(f'{sep.join(map(str, x))}{end}')

def solve():
	(n, a) = (*I(), [*I()])
	if n < 2:
		return pr(0)
	(cnt, c) = ([[], []], [0] * 2)
	for (i, x) in enumerate(a):
		cnt[x % 2].append(i)
	if abs(len(cnt[0]) - len(cnt[1])) > 1:
		return pr(-1)
	if len(cnt[0]) < len(cnt[1]):
		(cnt[0], cnt[1]) = (cnt[1], cnt[0])
	for i in range(n):
		c[0] += abs(i - cnt[i % 2][i // 2])
	if len(cnt[0]) == len(cnt[1]):
		(cnt[0], cnt[1]) = (cnt[1], cnt[0])
	for i in range(n):
		c[1] += abs(i - cnt[i % 2][i // 2])
	pr(min(c) // 2)

def main():
	(t,) = I()
	for _ in range(t):
		solve()
main()


from sys import stdout, stdin
input = stdin.readline

def output(answer):
	stdout.write('{}\n'.format(answer))

def even_index(arr, value):
	n = len(arr)
	itr = 0
	count = 0
	for index in range(0, n, 2):
		while arr[itr] != value:
			itr += 1
		count += abs(index - itr)
		itr += 1
	return count

def odd_index(arr, value):
	n = len(arr)
	itr = 0
	count = 0
	for index in range(1, n, 2):
		while arr[itr] != value:
			itr += 1
		count += abs(index - itr)
		itr += 1
	return count
for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	(odd, even) = (0, 0)
	for index in range(n):
		if arr[index] % 2 == 0:
			even = even + 1
			arr[index] = arr[index] % 2
		else:
			odd += 1
			arr[index] = arr[index] % 2
	if n % 2 == 0:
		if even != odd:
			output(-1)
			continue
		value1 = even_index(arr, 0)
		value2 = odd_index(arr, 0)
		value3 = even_index(arr, 1)
		value4 = odd_index(arr, 1)
		output(min(value1, value2, value3, value4))
	else:
		if max(odd, even) - min(odd, even) != 1:
			output(-1)
			continue
		v = None
		if odd > even:
			v = even_index(arr, 1)
		else:
			v = even_index(arr, 0)
		output(v)


import sys
ints = (int(x) for x in sys.stdin.read().split())
sys.setrecursionlimit(3000)
from collections import deque

def main():
	ntc = next(ints)
	for tc in range(1, ntc + 1):
		n = next(ints)
		a = [next(ints) % 2 for i in range(n)]
		n_ones = sum(a)
		n_zeros = n - sum(a)
		if abs(n_ones - n_zeros) > 1:
			ans = -1
		else:
			ones = [i for i in range(n) if a[i]]
			zeros = [i for i in range(n) if 1 - a[i]]
			move_ones = sum((abs(i - 2 * j) for (j, i) in enumerate(ones)))
			move_zeros = sum((abs(i - 2 * j) for (j, i) in enumerate(zeros)))
			if n_ones > n_zeros:
				ans = move_ones
			elif n_zeros > n_ones:
				ans = move_zeros
			else:
				ans = min(move_ones, move_zeros)
		print(ans)
	return
main()


from __future__ import print_function
from math import *
from collections import deque
import os
import sys
from io import BytesIO, IOBase

def main():
	pass
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')
mod = 10 ** 9 + 7

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

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

def insr():
	s = input()
	return list(s[:len(s)])

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

def merge(arr, temp, left, mid, right):
	inv_count = 0
	i = left
	j = mid
	k = left
	while i <= mid - 1 and j <= right:
		if arr[i] <= arr[j]:
			temp[k] = arr[i]
			k += 1
			i += 1
		else:
			temp[k] = arr[j]
			k += 1
			j += 1
			inv_count = inv_count + (mid - i)
	while i <= mid - 1:
		temp[k] = arr[i]
		k += 1
		i += 1
	while j <= right:
		temp[k] = arr[j]
		k += 1
		j += 1
	for i in range(left, right + 1, 1):
		arr[i] = temp[i]
	return inv_count

def _mergeSort(arr, temp, left, right):
	inv_count = 0
	if right > left:
		mid = int((right + left) / 2)
		inv_count = _mergeSort(arr, temp, left, mid)
		inv_count += _mergeSort(arr, temp, mid + 1, right)
		inv_count += merge(arr, temp, left, mid + 1, right)
	return inv_count

def minSwaps(arr, n):
	temp = [0 for i in range(n)]
	return _mergeSort(arr, temp, 0, n - 1)
for _ in range(inp()):
	n = inp()
	l = inlt()
	odd = 0
	even = 0
	for i in range(n):
		if l[i] % 2 == 0:
			even += 1
		else:
			odd += 1
	if n % 2 == 0 and odd != even:
		print(-1)
		continue
	if n % 2 != 0 and abs(odd - even) != 1:
		print(-1)
		continue
	odda = []
	evena = []
	oddc = 1
	evenc = 2
	for i in range(n):
		if l[i] % 2 == 0:
			odda.append(evenc)
			evenc += 2
		else:
			odda.append(oddc)
			oddc += 2
	oddc = 2
	evenc = 1
	for i in range(n):
		if l[i] % 2 == 0:
			evena.append(evenc)
			evenc += 2
		else:
			evena.append(oddc)
			oddc += 2
	if n % 2 == 1 and even > odd:
		print(minSwaps(evena, n))
	if n % 2 == 1 and odd > even:
		print(minSwaps(odda, n))
	if n % 2 == 0:
		print(min(minSwaps(evena, n), minSwaps(odda, n)))


def solve(n, A):
	(c, c1, d, d1) = (0, 0, 0, 0)
	for a in A:
		if a % 2 == 0:
			c1 += abs(c - d)
			c += 1
		else:
			d1 += abs(c - d)
			d += 1
	if abs(c - d) > 1:
		return -1
	if c > d:
		return c1
	if d > c:
		return d1
	return min(c1, d1)
t = int(input())
for _ in range(t):
	n = int(input())
	A = list(map(int, input().split()))
	print(solve(n, A))


t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(x) for x in input().split()]
	best = float('inf')
	for startLookingforEvens in range(0, 2):
		(nOdds, nEvens) = (0, 0)
		lookingForEven = startLookingforEvens == 0
		moves = 0
		for i in a:
			if lookingForEven:
				if i % 2 == 0:
					moves += nOdds
					if nOdds == 0:
						lookingForEven = False
					else:
						nOdds -= 1
				else:
					nOdds += 1
			elif i % 2 == 1:
				moves += nEvens
				if nEvens == 0:
					lookingForEven = True
				else:
					nEvens -= 1
			else:
				nEvens += 1
		if nOdds + nEvens == 0:
			best = min(best, moves)
	print(-1 if best == float('inf') else best)


from collections import deque
for _ in range(int(input())):
	n = int(input())
	a = [int(e) % 2 for e in input().split()]

	def minimal_ops(wanted):
		ops = 0
		i = 0
		extra = deque()
		while i < n:
			if extra and wanted == extra[0]:
				extra.popleft()
				wanted = 1 - wanted
			else:
				while i < n:
					if wanted == a[i]:
						ops += len(extra)
						wanted = 1 - wanted
						i += 1
						break
					else:
						extra.append(a[i])
						i += 1
				else:
					return 1 << 55
		return 1 << 55 if extra and (extra[0] != wanted or len(extra) > 1) else ops
	ans = min(minimal_ops(0), minimal_ops(1))
	print(-1 if ans == 1 << 55 else ans)


for _ in range(int(input())):
	n = map(int, input().split())
	l = list(map(int, input().split()))
	even = 0
	odd = 0
	for i in l:
		if i % 2 == 0:
			even += 1
		else:
			odd += 1
	if max(even, odd) - min(even, odd) > 1:
		print(-1)
	elif max(even, odd) - min(even, odd) == 0:
		ex = 0
		en = 0
		c = 0
		for i in range(len(l)):
			if l[i] % 2 == 0:
				ex = abs(i - c) + ex
				c += 2
		c = 0
		for i in range(len(l)):
			if l[i] % 2 != 0:
				en = abs(i - c) + en
				c += 2
		print(min(ex, en))
	elif even > odd:
		ex = 0
		c = 0
		for i in range(len(l)):
			if l[i] % 2 == 0:
				ex = abs(i - c) + ex
				c += 2
		print(ex)
	else:
		en = 0
		c = 0
		for i in range(len(l)):
			if l[i] % 2 != 0:
				en = abs(i - c) + en
				c += 2
		print(en)


def main():
	for _ in range(int(input())):
		n = int(input())
		a = [x % 2 for x in map(int, input().split())]
		ones = sum(a)
		if abs(ones * 2 - n) >= 2:
			print(-1)
			continue
		if ones * 2 < n:
			a = [1 - x for x in a]
		if ones * 2 == n:
			ans1 = sum((abs(x - y) for (x, y) in zip([i for (i, x) in enumerate(a) if x == 1], range(0, n, 2))))
			ans2 = sum((abs(x - y) for (x, y) in zip([i for (i, x) in enumerate(a) if x == 1], range(1, n, 2))))
			print(min(ans1, ans2))
		else:
			print(sum((abs(x - y) for (x, y) in zip([i for (i, x) in enumerate(a) if x == 1], range(0, n, 2)))))
main()


for _ in range(int(input())):
	n = int(input())
	(o, e) = ([], [])
	arr = [int(w) for w in input().split(' ')]
	(a, b) = (0, 0)
	(ans1, ans2) = (-1, -1)
	for i in range(n):
		if arr[i] % 2 == 0:
			e.append(i)
			a = a + 1
		else:
			o.append(i)
			b = b + 1
	if n % 2 == 0 and a == b:
		ans1 = 0
		ans2 = 0
		j = 0
		for i in range(n):
			if i % 2 == 0:
				ans1 = ans1 + abs(i - e[j])
				j = j + 1
		j = 0
		for i in range(n):
			if i % 2 != 0:
				ans2 = ans2 + abs(i - e[j])
				j = j + 1
	elif n % 2 != 0 and a - b == 1:
		ans = 0
		j = 0
		for i in range(n):
			if i % 2 == 0:
				ans = ans + abs(i - e[j])
				j = j + 1
	elif n % 2 != 0 and b - a == 1:
		ans = 0
		j = 0
		for i in range(n):
			if i % 2 != 0:
				ans = ans + abs(i - e[j])
				j = j + 1
	else:
		ans = -1
	if ans1 != -1 and ans2 != -1:
		ans = min(ans1, ans2)
	print(ans)


from math import *
from collections import *
from functools import *
from bisect import *
from itertools import *
from heapq import *
inf = float('inf')
ninf = -float('inf')
ip = input
alphal = 'abcdefghijklmnopqrstuvwxyz'
alphau = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
mod = 10 ** 9 + 7

def ipl():
	return list(map(int, ip().split()))

def ipn():
	return int(ip())

def ipf():
	return float(ip())

def solve():
	n = ipn()
	a = ipl()
	o = [i for i in range(n) if a[i] % 2 != 0]
	e = [i for i in range(n) if a[i] % 2 == 0]
	i = j = s1 = 0
	for k in range(n):
		if k % 2 == 0 and i < len(o):
			s1 += abs(k - o[i])
			i += 1
		elif k % 2 == 1 and j < len(e):
			s1 += abs(k - e[j])
			j += 1
		else:
			s1 = inf
	i = j = s2 = 0
	for k in range(n):
		if k % 2 == 1 and i < len(o):
			s2 += abs(k - o[i])
			i += 1
		elif k % 2 == 0 and j < len(e):
			s2 += abs(k - e[j])
			j += 1
		else:
			s2 = inf
	r = min(s1, s2)
	print(-1 if r == inf else r // 2)
t = ipn()
for _ in range(t):
	solve()


for _ in range(int(input())):
	n = int(input())
	ai = list(map(int, input().split()))
	o = 0
	e = 0
	for i in range(n):
		ai[i] = ai[i] % 2
		if ai[i] % 2 == 0:
			e = e + 1
		else:
			o = o + 1
	if abs(e - o) > 1:
		print(-1)
	else:
		a = ans = 0
		if e == o + 1:
			a = 1
			for i in range(n):
				if ai[i] == 1:
					ans = ans + abs(i - a)
					a = a + 2
		elif o == e + 1:
			for i in range(n):
				if ai[i] == 1:
					ans = ans + abs(i - a)
					a = a + 2
		else:
			ans1 = ans2 = 0
			for i in range(n):
				if ai[i] == 1:
					ans1 = ans1 + abs(i - a)
					a = a + 2
			a = 1
			for i in range(n):
				if ai[i] == 1:
					ans2 = ans2 + abs(i - a)
					a = a + 2
			ans = min(ans1, ans2)
		print(ans)


import sys
import math
import heapq
read = sys.stdin.readline
sys.setrecursionlimit(10 ** 5)
loop = int(read())
for _ in range(loop):
	num = int(read())
	li = list(map(int, read().split()))
	odd_cnt = 0
	odd_list = []
	even_cnt = 0
	even_list = []
	for idx in range(num):
		item = li[idx]
		if item % 2 == 0:
			even_cnt += 1
			even_list.append(idx)
		else:
			odd_cnt += 1
			odd_list.append(idx)
	if abs(even_cnt - odd_cnt) >= 2:
		print(-1)
		continue
	even_list.sort()
	odd_list.sort()
	answer = 0
	if even_cnt == odd_cnt:
		midd_answer = 0
		for idx in range(odd_cnt):
			answer += abs(odd_list[idx] - (idx * 2 + 1))
			midd_answer += abs(odd_list[idx] - idx * 2)
		answer = min(midd_answer, answer)
	elif even_cnt > odd_cnt:
		for idx in range(odd_cnt):
			answer += abs(odd_list[idx] - (idx * 2 + 1))
	else:
		for idx in range(odd_cnt):
			answer += abs(odd_list[idx] - idx * 2)
	print(answer)


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	odd = []
	for i in range(n):
		if a[i] % 2 == 1:
			odd.append(i)
	cur1 = 0
	cur2 = 1
	ans1 = 0
	ans2 = 0
	for i in odd:
		ans1 += abs(i - cur1)
		ans2 += abs(i - cur2)
		cur1 += 2
		cur2 += 2
	if abs(len(a) - len(odd) - len(odd)) <= 1:
		if len(a) - len(odd) - len(odd) == 1:
			print(ans2)
		elif len(a) - len(odd) - len(odd) == -1:
			print(ans1)
		else:
			print(min(ans1, ans2))
	else:
		print(-1)


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

def first(a, parity):
	cursor = 0
	for (i, x) in enumerate(a):
		if x == parity:
			yield abs(i - cursor)
			cursor += 2

def solve():
	n = int(input())
	a = [x & 1 for x in readline()]
	even = a.count(0)
	odd = a.count(1)
	assert even + odd == n
	if abs(even - odd) > 1:
		return -1
	if even > odd:
		return sum(first(a, 0))
	elif odd > even:
		return sum(first(a, 1))
	else:
		return min((sum(first(a, parity)) for parity in (0, 1)))
t = int(input())
for __ in range(t):
	print(solve())


for _ in range(int(input())):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = 10 ** 12
	for i in range(0, 2):
		x = []
		y = []
		for j in range(i, n, 2):
			x.append(j)
		for j in range(0, n, 1):
			if arr[j] % 2 == 0:
				y.append(j)
		if len(x) == len(y):
			cur = 0
			for j in range(len(x)):
				cur += abs(x[j] - y[j])
			ans = min(ans, cur)
	if ans == 10 ** 12:
		print(-1)
	else:
		print(ans)


import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
	N = int(input())
	A = list(map(int, input().split()))
	evs = []
	ods = []
	for (i, a) in enumerate(A):
		if a % 2:
			ods.append(i)
		else:
			evs.append(i)
	if abs(len(ods) - len(evs)) > 1:
		print(-1)
		continue
	if len(ods) > len(evs):
		arr = [i * 2 for i in range(N // 2 + 1)]
		ans = 0
		for (a, b) in zip(arr, ods):
			ans += abs(a - b)
		print(ans)
	elif len(ods) < len(evs):
		arr = [i * 2 for i in range(N // 2 + 1)]
		ans = 0
		for (a, b) in zip(arr, evs):
			ans += abs(a - b)
		print(ans)
	else:
		arr = [i * 2 for i in range(N)]
		ans1 = 0
		for (a, b) in zip(arr, ods):
			ans1 += abs(a - b)
		ans2 = 0
		for (a, b) in zip(arr, evs):
			ans2 += abs(a - b)
		print(min(ans1, ans2))


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	c1 = c2 = 0
	for i in a:
		if i % 2 == 0:
			c1 += 1
		else:
			c2 += 1
	if abs(c1 - c2) > 1:
		print(-1)
	elif len(a) <= 2:
		print(0)
	else:
		c1 = c2 = c3 = c4 = 0
		a1 = []
		a2 = []
		for i in range(n):
			if a[i] % 2 == 0:
				a1.append(i)
			else:
				a2.append(i)
		if n % 2 == 0:
			x = 0
			for i in range(0, n, 2):
				c1 += abs(i - a1[x])
				c2 += abs(i - a2[x])
				x += 1
			x = 0
			for i in range(1, n, 2):
				c3 += abs(i - a1[x])
				c4 += abs(i - a2[x])
				x += 1
			print(min(c1, c2, c3, c4))
		else:
			if len(a1) > len(a2):
				x = 0
				for i in range(0, n, 2):
					c1 += abs(i - a1[x])
					x += 1
				x = 0
				for i in range(1, n, 2):
					c2 += abs(i - a2[x])
					x += 1
			else:
				x = 0
				for i in range(0, n, 2):
					c1 += abs(i - a2[x])
					x += 1
				x = 0
				for i in range(1, n, 2):
					c2 += abs(i - a1[x])
					x += 1
			print(min(c1, c2))


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	a = list(map(int, input().split()))
	a = [i % 2 for i in a]
	odd = 0
	even = 0
	for i in range(n):
		if a[i] == 0:
			even += 1
		else:
			odd += 1
	if abs(odd - even) >= 2:
		print(-1)
		return
	if even > odd:
		l = 0
		count = 0
		for i in range(n):
			if a[i] == 0:
				count += abs(i - l)
				l += 2
		print(count)
		return
	elif odd > even:
		l = 1
		count1 = 0
		for i in range(n):
			if a[i] == 0:
				count1 += abs(i - l)
				l += 2
		print(count1)
		return
	else:
		l = 0
		count = 0
		for i in range(n):
			if a[i] == 0:
				count += abs(i - l)
				l += 2
		l = 1
		count1 = 0
		for i in range(n):
			if a[i] == 0:
				count1 += abs(i - l)
				l += 2
		print(min(count, count1))
for _ in range(int(input())):
	main()


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)
for _ in range(ii1()):
	n = ii1()
	arr = iia()
	odd = 0
	for i in range(n):
		arr[i] = arr[i] % 2
		odd += arr[i]
	even = n - odd
	if n % 2 == 0:
		if even != odd:
			print(-1)
			continue
	if abs(odd - even) > 1:
		print(-1)
		continue
	opos = []
	epos = []
	for i in range(n):
		if arr[i]:
			opos.append(i)
		else:
			epos.append(i)
	ans = 0
	if n % 2:
		if odd > even:
			j = 0
			for i in range(0, n + 10, 2):
				ans += abs(opos[j] - i)
				j += 1
				if j == odd:
					break
			print(ans)
		else:
			j = 0
			for i in range(0, n + 10, 2):
				ans += abs(epos[j] - i)
				j += 1
				if j == even:
					break
			print(ans)
	else:
		ans = 0
		ans2 = 0
		j = 0
		N = len(opos)
		for i in range(0, n + 10, 2):
			ans += abs(opos[j] - i)
			j += 1
			if j == N:
				break
		j = 0
		for i in range(1, n + 10, 2):
			ans2 += abs(opos[j] - i)
			j += 1
			if j == N:
				break
		print(min(ans, ans2))


from itertools import permutations as per
from math import factorial as fact
from difflib import SequenceMatcher
t = int(input())
for _ in range(t):
	n = int(input())
	a = [int(i) for i in input().split()]
	o = []
	e = []
	for i in range(n):
		if a[i] % 2 == 1:
			o.append(i)
		else:
			e.append(i)
	if abs(len(o) - len(e)) > 1:
		print(-1)
	elif len(o) == len(e):
		ans1 = ans2 = 0
		for i in range(0, n, 2):
			ans1 += abs(i - o[i // 2])
		for i in range(0, n, 2):
			ans2 += abs(i - e[i // 2])
		print(min(ans1, ans2))
	else:
		ans = 0
		if len(o) > len(e):
			for i in range(0, n, 2):
				ans += abs(i - o[i // 2])
		else:
			for i in range(0, n, 2):
				ans += abs(i - e[i // 2])
		print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	liczby = list(map(int, input().split()))
	wynik = 0
	par = 0
	nie = 0
	parzyste = []
	nieparzyste = []
	for x in range(n):
		if liczby[x] % 2 == 0:
			par += 1
			parzyste.append(x + 1)
		else:
			nie += 1
			nieparzyste.append(x + 1)
	if abs(nie - par) > 1:
		wynik = -1
	elif nie > par:
		a = 1
		for x in range(nie):
			wynik += abs(nieparzyste[x] - a)
			a += 2
	elif par > nie:
		a = 1
		for x in range(par):
			wynik += abs(parzyste[x] - a)
			a += 2
	else:
		a = 1
		wynik2 = 0
		for x in range(nie):
			wynik += abs(nieparzyste[x] - a)
			wynik2 += abs(parzyste[x] - a)
			a += 2
		if wynik2 < wynik:
			wynik = wynik2
	print(wynik)


def main():
	from sys import stdin, setrecursionlimit
	input = stdin.buffer.readline
	for _ in range(int(input())):
		n = int(input())
		a = [int(i) & 1 for i in input().split()]
		ans = 10 ** 10
		if sum(a) == (n + 1) // 2:
			c = 0
			j = 0
			for i in range(n):
				if a[i]:
					c += abs(i - j)
					j += 2
			ans = min(ans, c)
		if sum(a) == n // 2:
			c = 0
			j = 1
			for i in range(n):
				if a[i]:
					c += abs(i - j)
					j += 2
			ans = min(ans, c)
		if ans == 10 ** 10:
			print(-1)
		else:
			print(ans)
main()


import sys
from sys import stdin
tt = int(stdin.readline())
ANS = []
for loop in range(tt):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().split()))
	for i in range(n):
		a[i] %= 2
	sa = sum(a)
	ones = []
	for i in range(n):
		if a[i] == 1:
			ones.append(i)
	tans = float('inf')
	x = [i % 2 for i in range(n)]
	sx = sum(x)
	if sa == sx:
		ox = []
		for i in range(n):
			if x[i] == 1:
				ox.append(i)
		ans = 0
		for i in range(len(ox)):
			ans += abs(ones[i] - ox[i])
		tans = min(ans, tans)
	x = [1 ^ i % 2 for i in range(n)]
	sx = sum(x)
	if sa == sx:
		ox = []
		for i in range(n):
			if x[i] == 1:
				ox.append(i)
		ans = 0
		for i in range(len(ox)):
			ans += abs(ones[i] - ox[i])
		tans = min(ans, tans)
	if tans == float('inf'):
		tans = -1
	ANS.append(str(tans))
print('\n'.join(ANS))


t = int(input())
while t:
	t -= 1
	n = int(input())
	a = list(map(int, input().split()))
	(l, r, temp) = ([], [], 0)
	for i in range(n):
		if a[i] % 2 == 0:
			r.append(i)
		else:
			l.append(i)
	if n % 2:
		if abs(len(l) - len(r)) == 1:
			if len(l) > len(r):
				ans1 = 0
				for i in range(len(l)):
					ans1 += abs(l[i] - temp)
					temp += 2
				print(ans1)
			else:
				ans1 = 0
				for i in range(len(r)):
					ans1 += abs(r[i] - temp)
					temp += 2
				print(ans1)
		else:
			print(-1)
	elif len(l) == len(r):
		ans1 = 0
		for i in range(len(l)):
			ans1 += abs(l[i] - temp)
			temp += 2
		(res, temp) = (0, 0)
		for i in range(len(l)):
			res += abs(r[i] - temp)
			temp += 2
		print(min(ans1, res))
	else:
		print(-1)


t = int(input())

def get_posone(first, l):
	odd = l % 2
	if first:
		if odd:
			return [2 * i for i in range(int(l / 2 + 0.5))]
		else:
			return [2 * i for i in range(int(l / 2))]
	elif odd:
		return [2 * i + 1 for i in range(int(l / 2 - 0.5))]
	else:
		return [2 * i + 1 for i in range(int(l / 2))]
for i in range(t):
	ll = int(input())
	arr = [int(c) % 2 for c in input().split(' ')]
	if len(arr) % 2:
		total_par = True
		if sum(arr) == len(arr) / 2 + 0.5:
			first_set = [1]
		elif sum(arr) == len(arr) / 2 - 0.5:
			first_set = [0]
		else:
			first_set = []
	else:
		total_par = False
		if sum(arr) == len(arr) / 2:
			first_set = [0, 1]
		else:
			first_set = []
	if len(first_set) == 0:
		print(-1)
		continue
	hops = []
	for first in first_set:
		posone = get_posone(first, len(arr))
		poscurrent = [i for i in range(len(arr)) if arr[i]]
		sm = 0
		for i in range(len(posone)):
			sm += abs(posone[i] - poscurrent[i])
		hops.append(sm)
	print(min(hops))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	even = 0
	for i in range(n):
		if a[i] & 1 == 0:
			even += 1
	odd = n - even
	if n & 1 == 1:
		if abs(even - odd) > 1:
			print(-1)
			continue
	elif even != odd:
		print(-1)
		continue
	if n & 1 == 0:
		pos = -1
		for i in range(n):
			if a[i] & 1 == 1:
				pos = i
				break
		x = []
		for i in range(n):
			if a[i] & 1 == 0:
				x.append(i)
		for i in range(len(x)):
			if x[i] < pos:
				x[i] += 1
			else:
				break
		ans1 = pos
		j = 0
		for i in range(1, n, 2):
			ans1 += abs(x[j] - i)
			j += 1
		pos = -1
		x = []
		for i in range(n):
			if a[i] & 1 == 0:
				pos = i
				break
		for i in range(n):
			if a[i] & 1 == 1:
				x.append(i)
		for i in range(len(x)):
			if x[i] < pos:
				x[i] += 1
			else:
				break
		ans2 = pos
		j = 0
		for i in range(1, n, 2):
			ans2 += abs(x[j] - i)
			j += 1
		print(min(ans1, ans2))
	elif even > odd:
		pos = -1
		x = []
		for i in range(n):
			if a[i] & 1 == 0:
				pos = i
				break
		for i in range(n):
			if a[i] & 1 == 1:
				x.append(i)
		for i in range(len(x)):
			if x[i] < pos:
				x[i] += 1
			else:
				break
		ans = pos
		j = 0
		for i in range(1, n, 2):
			ans += abs(x[j] - i)
			j += 1
		print(ans)
	else:
		pos = -1
		for i in range(n):
			if a[i] & 1 == 1:
				pos = i
				break
		x = []
		for i in range(n):
			if a[i] & 1 == 0:
				x.append(i)
		for i in range(len(x)):
			if x[i] < pos:
				x[i] += 1
			else:
				break
		ans = pos
		j = 0
		for i in range(1, n, 2):
			ans += abs(x[j] - i)
			j += 1
		print(ans)


for _ in range(int(input())):
	n = int(input())
	a = [int(x) % 2 for x in input().split()]
	ans = float('inf')
	s = sum(a)
	if n // 2 == s:
		x = 0
		(z, o) = (0, 0)
		for (i, v) in enumerate(a):
			if v == 1:
				o += 1
				x += abs(o - z)
			else:
				z += 1
		ans = min(x, ans)
	if (n + 1) // 2 == s:
		x = 0
		(z, o) = (0, 0)
		for (i, v) in enumerate(a):
			if v == 1:
				o += 1
			else:
				z += 1
				x += abs(o - z)
		ans = min(x, ans)
	print(ans if ans < float('inf') else -1)


import sys
import math
import collections
import operator as op
from collections import deque
from math import gcd, inf, sqrt, pi, cos, sin, ceil, log2, floor, log
from bisect import bisect_right, bisect_left, bisect
from itertools import permutations
from functools import reduce
from sys import stdin, stdout, setrecursionlimit
setrecursionlimit(2 ** 20)

def ncr(n, r):
	r = min(r, n - r)
	numer = reduce(op.mul, range(n, n - r, -1), 1)
	denom = reduce(op.mul, range(1, r + 1), 1)
	return numer // denom

def prime_factors(n):
	i = 2
	factors = []
	while i * i <= n:
		if n % i:
			i += 1
		else:
			n //= i
			factors.append(i)
	if n > 1:
		factors.append(n)
	return list(factors)

def isPowerOfTwo(x):
	return x and (not x & x - 1)
MOD = 1000000007
PMOD = 998244353
LOGN = 30
alp = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
N = 2 * 100005 + 1
T = 1
T = int(stdin.readline())
for _ in range(T):
	n = int(stdin.readline())
	a = list(map(int, stdin.readline().rstrip().split()))
	oc = 0
	zc = 0
	for i in range(n):
		a[i] = a[i] % 2
		if a[i] == 0:
			zc += 1
		else:
			oc += 1
	if abs(zc - oc) > 1:
		print(-1)
	elif oc == zc:
		ix = []
		for i in range(n):
			if a[i] == 0:
				ix.append(i)
		nn = len(ix)
		ind = 0
		ans1 = 0
		for i in range(n):
			if i % 2 == 1 and ind < nn:
				ans1 += abs(i - ix[ind])
				ind += 1
		ind = 0
		ans2 = 0
		for i in range(n):
			if i % 2 == 0 and ind < nn:
				ans2 += abs(i - ix[ind])
				ind += 1
		print(min(ans1, ans2))
	else:
		if oc > zc:
			(zc, oc) = (oc, zc)
			for i in range(n):
				a[i] = 1 - a[i]
		ix = []
		for i in range(n):
			if a[i] == 0:
				ix.append(i)
		nn = len(ix)
		ind = 0
		ans = 0
		for i in range(n):
			if i % 2 == 0:
				ans += abs(i - ix[ind])
				ind += 1
		print(ans)


import time
import os, sys
from datetime import datetime
from math import floor, sqrt, gcd, factorial, ceil, log2
from collections import Counter, defaultdict
import bisect
from itertools import chain
from collections import deque
from sys import maxsize as INT_MAX
from itertools import permutations
(ONLINE_JUDGE, INF, mod) = (False, float('inf'), int(1000000000.0) + 7)
if os.path.exists('D:\\Contest'):
	ONLINE_JUDGE = True
	sys.stdin = open('inp.txt', 'r')
	sys.stdout = open('out.txt', 'w')

def readint():
	return int(sys.stdin.readline())

def readstr():
	return sys.stdin.readline()

def readlst():
	return list(map(int, sys.stdin.readline().strip().split()))

def readmul():
	return map(int, sys.stdin.readline().strip().split())

def mulfloat():
	return map(float, sys.stdin.readline().strip().split())

def flush():
	return sys.stdout.flush()

def power_two(x):
	return 1 << x

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

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
			return n - leftGreater

def lower_bound(arr, n, val):
	(l, r) = (-1, n)
	while r > l + 1:
		m = int(l + r >> 1)
		if arr[m] < val:
			l = m
		else:
			r = m
	return r

def upper_bound(arr, n, val):
	(l, r) = (-1, n)
	while r > l + 1:
		m = int(l + r >> 1)
		if arr[m] <= val:
			l = m
		else:
			r = m
	return l

def binpow(a, n, mod):
	res = 1
	while n:
		if n & 1:
			res = res * a % mod
			n -= 1
		a = a * a % mod
		n = n >> 1
	return res

def printmat(l, seperate=True):
	for i in range(0, len(l)):
		if seperate:
			print(*l[i], sep=' ')
		else:
			print(*l[i], sep='')

def is_perfect_square(num):
	temp = num ** 0.5
	return temp // 1 == temp

def f(a, comp):
	(ans, diff, n) = (0, 0, len(a))
	for i in range(n):
		if a[i] != comp:
			diff += comp - a[i]
		ans += abs(diff)
		comp = comp ^ 1
	return ans

def john_3_16(cnter):
	n = readint()
	a = readlst()
	(cnt, d, o, e) = (0, 0, 0, 0)
	for i in range(n):
		if a[i] & 1:
			a[i] = 1
			o += 1
		else:
			a[i] = 0
			e += 1
	if n & 1 and max(o, e) - 1 > n // 2 or (n & 1 == 0 and max(o, e) > n // 2):
		print(-1)
		return
	if n & 1 == 0:
		cnt = min(f(a, 0), f(a, 1))
	elif e == o + 1:
		cnt = f(a, 0)
	elif o == e + 1:
		cnt = f(a, 1)
	else:
		cnt = -1
	print(cnt)
	return

def main():
	tc = readint()
	start = time.time()
	cnt = 1
	while tc:
		john_3_16(cnt)
		tc -= 1
		cnt += 1
	if ONLINE_JUDGE:
		print(f'{(time.time() - start) * 1000}ms')
		pass
main()


import bisect
import collections
import copy
import functools
import heapq
import itertools
import math
import sys
import string
import random
from typing import List
sys.setrecursionlimit(99999)

def f(s):
	return int(s) % 2
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(f, input().split()))
	(a, b) = ([], [])
	for (i, c) in enumerate(arr):
		if c == 0:
			a.append(i)
		else:
			b.append(i)
	s = sum(arr)
	if s > (n + 1) // 2 or n - s > (n + 1) // 2:
		print(-1)
		continue
	tar = 1
	ans = float('inf')
	if n - s > s:
		pass
	else:
		ai = 0
		bi = 0
		ta = 0
		for i in range(n):
			cur = (i + tar) % 2
			if cur == 0:
				pos = a[ai]
				ai += 1
			else:
				pos = b[bi]
				bi += 1
			if pos > i:
				ta += pos - i
		ans = min(ans, ta)
	tar = 0
	if s > n - s:
		pass
	else:
		ai = 0
		bi = 0
		ta = 0
		for i in range(n):
			cur = (i + tar) % 2
			if cur == 0:
				pos = a[ai]
				ai += 1
			else:
				pos = b[bi]
				bi += 1
			if pos > i:
				ta += pos - i
		ans = min(ans, ta)
	print(ans)


t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	ans = pow(10, 15)
	arr2 = []
	c = 0
	for i in range(n):
		if arr[i] & 1:
			c += 1
			arr2.append(i)
	if n & 1:
		if c == n // 2:
			curr = 1
			temp = 0
			for i in range(c):
				temp += abs(arr2[i] - curr)
				curr += 2
			ans = min(ans, temp)
		elif c == n // 2 + 1:
			curr = 0
			temp = 0
			for i in range(c):
				temp += abs(arr2[i] - curr)
				curr += 2
			ans = min(ans, temp)
		else:
			print(-1)
			continue
	elif c == n // 2:
		curr = 0
		temp = 0
		for i in range(c):
			temp += abs(arr2[i] - curr)
			curr += 2
		ans = min(ans, temp)
		curr = 1
		temp = 0
		for i in range(c):
			temp += abs(arr2[i] - curr)
			curr += 2
		ans = min(ans, temp)
	else:
		print(-1)
		continue
	print(ans)


t = int(input())
for iii in range(t):
	n = int(input())
	a = list(map(lambda p: int(p) % 2, input().split()))
	win = 0
	cnt_1 = sum(a)
	ans = 10000000000000000
	if n % 2 == 0 and n // 2 == cnt_1:
		j = 0
		win = 1
		cnt = 0
		for i in range(n):
			if a[i] == 1:
				j = i
				break
		for i in range(0, n, 2):
			cnt += abs(j - i)
			for q in range(j + 1, n):
				if a[q] == 1:
					j = q
					break
		ans = min(ans, cnt)
		j = 0
		win = 1
		cnt = 0
		for i in range(n):
			if a[i] == 1:
				j = i
				break
		for i in range(1, n, 2):
			cnt += abs(j - i)
			for q in range(j + 1, n):
				if a[q] == 1:
					j = q
					break
		ans = min(ans, cnt)
	if n % 2 == 1:
		if n // 2 == cnt_1:
			j = 0
			win = 1
			cnt = 0
			for i in range(n):
				if a[i] == 1:
					j = i
					break
			for i in range(1, n, 2):
				cnt += abs(j - i)
				for q in range(j + 1, n):
					if a[q] == 1:
						j = q
						break
			ans = min(ans, cnt)
		if n // 2 == cnt_1 - 1:
			j = 0
			win = 1
			cnt = 0
			for i in range(n):
				if a[i] == 1:
					j = i
					break
			for i in range(0, n, 2):
				cnt += abs(j - i)
				for q in range(j + 1, n):
					if a[q] == 1:
						j = q
						break
			ans = min(ans, cnt)
	if win == 0:
		print(-1)
	else:
		print(ans)


iteration = int(input())
while iteration != 0:
	iteration -= 1
	n = int(input())
	inp = list(map(int, input().split()))
	even = 0
	e = 0
	odd = 0
	o = 0
	for i in inp:
		if int(i) % 2 == 0:
			e += abs(even - odd)
			even += 1
		else:
			o += abs(even - odd)
			odd += 1
	if abs(even - odd) > 1:
		print(-1)
	elif even > odd:
		print(e)
	elif odd > even:
		print(o)
	else:
		print(min(e, o))


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

def first(a, parity):
	idx = 0
	for (i, x) in enumerate(a):
		if x == parity:
			yield abs(i - idx)
			idx += 2

def solve():
	n = int(input())
	a = [x % 2 for x in readline()]
	even = a.count(0)
	odd = a.count(1)
	if abs(even - odd) > 1:
		return -1
	if even > odd:
		return sum(first(a, 0))
	elif even < odd:
		return sum(first(a, 1))
	else:
		return min(sum(first(a, 0)), sum(first(a, 1)))
T = int(input())
for _ in range(T):
	print(solve())


import sys
t = int(input())
for _ in range(t):
	n = int(input())
	arr = list(map(int, input().split()))
	(odd, even) = ([], [])
	for i in range(n):
		if arr[i] % 2 == 0:
			even.append(i)
		else:
			odd.append(i)
	(a, b) = (0, 0)
	if len(odd) != n // 2 + n % 2:
		a = sys.maxsize
	else:
		for i in range(0, len(even)):
			a += abs(2 * i + 1 - even[i])
	if len(even) != n // 2 + n % 2:
		b = sys.maxsize
	else:
		for i in range(0, len(odd)):
			b += abs(2 * i + 1 - odd[i])
	if min(a, b) == sys.maxsize:
		print(-1)
	else:
		print(min(a, b))


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	odd = 0
	even = 0
	for x in a:
		if x % 2 == 1:
			odd += 1
		else:
			even += 1
	if abs(odd - even) > 1:
		print(-1)
	elif odd == even:
		ans1 = 0
		lse1 = 1
		for i in range(n):
			if a[i] % 2 == 0:
				ans1 += abs(i - lse1)
				lse1 += 2
		ans2 = 0
		lso2 = 1
		for i in range(n):
			if a[i] % 2 == 1:
				ans2 += abs(i - lso2)
				lso2 += 2
		print(min(ans1, ans2))
	elif odd < even:
		ans3 = 0
		lso3 = 1
		for i in range(n):
			if a[i] % 2 == 1:
				ans3 += abs(i - lso3)
				lso3 += 2
		print(ans3)
	elif odd > even:
		ans4 = 0
		lse4 = 1
		for i in range(n):
			if a[i] % 2 == 0:
				ans4 += abs(i - lse4)
				lse4 += 2
		print(ans4)


from __future__ import division, print_function
import math
import sys
import os
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')

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')

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

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

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

def insr():
	s = input().strip()
	return list(s[:len(s)])

def invr():
	return map(int, input().split())
for _ in range(inp()):
	n = inp()
	l = inlt()
	e = []
	o = []
	for i in range(n):
		if l[i] % 2:
			o.append(i)
		else:
			e.append(i)
	if abs(len(o) - len(e)) > 1:
		print(-1)
	else:
		idxs = [i for i in range(0, n, 2)]
		if len(o) == len(e):
			(sw1, sw2) = (0, 0)
			for i in range(len(o)):
				sw1 += abs(o[i] - idxs[i])
				sw2 += abs(e[i] - idxs[i])
			print(min(sw1, sw2))
		elif len(o) == len(idxs):
			sw1 = 0
			for i in range(len(o)):
				sw1 += abs(o[i] - idxs[i])
			print(sw1)
		else:
			sw2 = 0
			for i in range(len(e)):
				sw2 += abs(e[i] - idxs[i])
			print(sw2)


import os
import sys
from io import BytesIO, IOBase
import math
from collections import Counter

def func(n, array):
	if n == 1:
		return 0
	array = [val % 2 for val in array]
	counter = Counter(array)
	if 0 not in counter or 1 not in counter:
		return -1
	if abs(counter[0] - counter[1]) > 1:
		return -1
	if counter[0] > counter[1]:
		start = 0
	elif counter[0] < counter[1]:
		start = 1
	else:
		start = None
	second = False
	if start is None:
		start = array[0]
		second = True
	count = 0
	index = 0
	for (i, val) in enumerate(array):
		if val == start:
			count += abs(i - index)
			index += 2
	desire = sum(list(range(0, n, 2)))
	if not second:
		return count
	count2 = 0
	index = 1
	for (i, val) in enumerate(array):
		if val == start:
			count2 += abs(i - index)
			index += 2
	return min(count, count2)

def main():
	num_test = int(parse_input())
	result = []
	for _ in range(num_test):
		n = int(parse_input())
		array = [int(i) for i in parse_input().split()]
		result.append(func(n, array))
	print('\n'.join(map(str, result)))
parse_input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
import math
strInp = lambda : input().strip().split()
intInp = lambda : list(map(int, strInp()))

def compareDistance(copybin, possible, n):
	diff = 0
	next_0 = 0
	next_1 = 0
	for i in range(n):
		if copybin[i] != possible[i]:
			if possible[i] == 0:
				for j in range(max(i, next_0), n):
					if copybin[j] == possible[i]:
						next_0 = j
						diff += j - i
						tmp = copybin[i]
						copybin[i] = copybin[j]
						copybin[j] = tmp
						break
			else:
				for j in range(max(i, next_1), n):
					if copybin[j] == possible[i]:
						next_1 = j
						diff += j - i
						tmp = copybin[i]
						copybin[i] = copybin[j]
						copybin[j] = tmp
						break
	return diff
for t in range(int(input())):
	n = int(input())
	arr = intInp()
	bin = []
	possible1 = []
	possible2 = []
	j = 0
	for i in arr:
		if i % 2 == 0:
			bin.append(0)
		else:
			bin.append(1)
		possible1.append(j % 2)
		possible2.append((j + 1) % 2)
		j += 1
	if abs(bin.count(0) - bin.count(1)) > 1:
		print(-1)
	else:
		diff1 = compareDistance(bin.copy(), possible1, n)
		diff2 = compareDistance(bin.copy(), possible2, n)
		if n % 2 == 0:
			print(min(diff2, diff1))
		elif bin.count(0) == n // 2:
			print(diff2)
		else:
			print(diff1)


import sys
input = iter(sys.stdin.read().splitlines()).__next__

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	num_odd = sum((a_i % 2 for a_i in a))
	num_even = n - num_odd
	if not abs(num_odd - num_even) <= 1:
		return -1
	odd_loc = [i for i in range(n) if a[i] % 2]
	swaps_if_odd_first = sum((abs(j - i) for (i, j) in zip(odd_loc, range(0, n, 2))))
	swaps_if_even_first = sum((abs(j - i) for (i, j) in zip(odd_loc, range(1, n, 2))))
	if num_odd == num_even:
		return min(swaps_if_odd_first, swaps_if_even_first)
	elif num_odd > num_even:
		return swaps_if_odd_first
	else:
		return swaps_if_even_first
t = int(input())
output = []
for _ in range(t):
	output.append(solve())
print(*output, sep='\n')


def get_ind(arr, start, val):
	for i in range(start, len(arr)):
		if arr[i] == val:
			return i
for _ in range(int(input())):
	arr_len = int(input())
	arr = map(int, input().split())
	arr = list(map(lambda x: 0 if x % 2 == 0 else 1, arr))
	num_odd = sum(arr)
	num_even = len(arr) - num_odd
	if len(arr) == 1:
		print(0)
	elif abs(num_odd - num_even) > 1:
		print(-1)
	else:
		poss = []
		for is_odd_first in [True, False]:
			if is_odd_first and num_odd < num_even:
				continue
			if not is_odd_first and num_odd > num_even:
				continue
			copy = arr.copy()
			(next_even, next_odd) = (0, 0)
			is_odd = is_odd_first
			tot = 0
			for i in range(len(arr)):
				if is_odd:
					next_odd = get_ind(copy, next_odd, 1)
					tot += abs(next_odd - i)
					(copy[i], copy[next_odd]) = (copy[next_odd], copy[i])
					next_odd += 1
				else:
					next_even = get_ind(copy, next_even, 0)
					tot += abs(next_even - i)
					(copy[i], copy[next_even]) = (copy[next_even], copy[i])
					next_even += 1
				is_odd = not is_odd
			poss.append(tot)
		print(min(poss))


import os, sys
from io import BytesIO, IOBase
from collections import *

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, 8192))
			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, 8192))
			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')

class dict(dict):

	def __missing__(self, key):
		return 0
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
inp = lambda dtype: [dtype(x) for x in input().split()]
inp_2d = lambda dtype, n: [dtype(input()) for _ in range(n)]
inp_2ds = lambda dtype, n: [inp(dtype) for _ in range(n)]
inp_enu = lambda dtype: [(i, x) for (i, x) in enumerate(inp(dtype))]
inp_enus = lambda dtype, n: [[i] + [inp(dtype)] for i in range(n)]
ceil1 = lambda a, b: (a + b - 1) // b
for _ in range(int(input())):
	(n, a) = (int(input()), inp(int))
	(e, o, ans) = (deque(), deque(), 0)
	for i in range(n):
		if a[i] & 1:
			o.append(i)
		else:
			e.append(i)
	(d, ans) = (abs(len(e) - len(o)), 0)
	if d > 1:
		print(-1)
		continue
	if len(e) > len(o):
		for i in range(n):
			if i & 1:
				ans += abs(o.popleft() - i)
			else:
				ans += abs(e.popleft() - i)
	elif len(o) > len(e):
		for i in range(n):
			if i & 1:
				ans += abs(e.popleft() - i)
			else:
				ans += abs(o.popleft() - i)
	else:
		(ans0, ans1) = (0, 0)
		(o1, e1) = (o.copy(), e.copy())
		for i in range(n):
			if i & 1:
				ans0 += abs(o.popleft() - i)
			else:
				ans0 += abs(e.popleft() - i)
		for i in range(n):
			if i & 1:
				ans1 += abs(e1.popleft() - i)
			else:
				ans1 += abs(o1.popleft() - i)
		ans = min(ans0, ans1)
	print(ans >> 1)


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	n = int(input())
	w = list(map(int, input().split()))
	(a, b, c, d) = (0, 0, 0, 0)
	for i in range(n):
		if w[i] % 2:
			c += 1
			a += abs(c - d)
		else:
			d += 1
			b += abs(c - d)
	if abs(c - d) > 1:
		print(-1)
	elif c > d:
		print(b)
	elif d > c:
		print(a)
	else:
		print(min(a, b))


import math
import heapq
import string
from collections import deque
from bisect import bisect, bisect_left, bisect_right, insort
import sys
input = sys.stdin.readline
S = lambda : input().rstrip()
I = lambda : int(S())
M = lambda : map(int, S().split())
L = lambda : list(M())
H = 1000000000 + 7

def ans(l, a, f):
	n = len(l)
	(p1, p2) = ([], [])
	for i in range(n):
		if l[i] == f:
			p1.append(i)
		if a[i] == f:
			p2.append(i)
	s = 0
	for i in range(len(p1)):
		s += abs(p1[i] - p2[i])
	return s
for _ in range(I()):
	n = I()
	l = L()
	for i in range(n):
		l[i] = l[i] % 2
	o = l.count(1)
	z = l.count(0)
	if abs(o - z) > 1:
		print(-1)
		continue
	if o > z:
		a = [0] * n
		for i in range(0, n, 2):
			a[i] = 1
		print(ans(l, a, 1))
	elif z > o:
		a = [1] * n
		for i in range(0, n, 2):
			a[i] = 0
		print(ans(l, a, 0))
	else:
		a1 = [0] * n
		a2 = [1] * n
		for i in range(0, n, 2):
			a1[i] = 1
			a2[i] = 0
		print(min(ans(l, a1, 0), ans(l, a2, 0)))


t = int(input())
for i in range(t):
	n = int(input())
	a = list(map(int, input().split()))
	c = 0
	for j in range(n):
		if a[j] % 2:
			c += 1
		else:
			c -= 1
	if abs(c) > 1:
		print(-1)
	elif c == 0:
		ec = 0
		s1 = 0
		for j in range(n):
			if a[j] % 2 != 0:
				s1 += abs(j - ec * 2)
				ec += 1
		ec = 0
		s = 0
		for j in range(n):
			if a[j] % 2 == 0:
				s += abs(j - ec * 2)
				ec += 1
		print(min(s, s1))
	elif c == 1:
		ec = 0
		s = 0
		for j in range(n):
			if a[j] % 2 != 0:
				s += abs(j - ec * 2)
				ec += 1
		print(s)
	elif c == -1:
		ec = 0
		s = 0
		for j in range(n):
			if a[j] % 2 == 0:
				s += abs(j - ec * 2)
				ec += 1
		print(s)
	else:
		print('error')


for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	(e, o) = (0, 0)
	for i in a:
		if i % 2:
			o += 1
		else:
			e += 1
	if e > (n + 1) // 2 or o > (n + 1) // 2:
		print(-1)
	else:
		e = []
		o = []
		for i in range(n):
			if a[i] % 2:
				o.append(i)
			else:
				e.append(i)
		if len(o) > len(e):
			x = o
			y = e
		else:
			x = e
			y = o
		(i, j) = (0, 0)
		ans = 0
		while i < n and j < len(x):
			ans += abs(i - x[j])
			i += 2
			j += 1
		(i, j) = (1, 0)
		h = 0
		while i < n and j < len(y):
			h += abs(i - y[j])
			i += 2
			j += 1
		ans = min(ans, h)
		if len(x) == len(y):
			(i, j) = (0, 0)
			h = 0
			while i < n and j < len(y):
				h += abs(i - y[j])
				i += 2
				j += 1
			ans = min(ans, h)
			(i, j) = (1, 0)
			h = 0
			while i < n and j < len(x):
				h += abs(i - x[j])
				i += 2
				j += 1
			ans = min(ans, h)
		print(ans)


import sys, os, io
import math
from collections import defaultdict

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

def li():
	return list(map(int, input().split()))
if os.path.exists('input.txt'):
	sys.stdin = open('input.txt', 'r')
	sys.stdout = open('output.txt', 'w')
else:
	input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def ff(a, new):
	ones = []
	zero = []
	res = 0
	for i in range(len(a)):
		if a[i] == new[i]:
			continue
		if a[i] % 2 == 1:
			ones.append(i)
		else:
			zero.append(i)
	x = len(ones)
	for i in range(x):
		y = ones[i] - zero[i]
		if y < 0:
			y *= -1
		res += y
	return res

def funodd(n, a):
	o = a.count(1)
	e = a.count(0)
	f = 0
	if o > e:
		f = 1
	new = []
	for i in range(n):
		new.append(f)
		f = abs(1 - f)
	ones = []
	zero = []
	res = 0
	for i in range(len(a)):
		if a[i] == new[i]:
			continue
		if a[i] % 2 == 1:
			ones.append(i)
		else:
			zero.append(i)
	x = len(ones)
	for i in range(x):
		y = ones[i] - zero[i]
		if y < 0:
			y *= -1
		res += y
	print(res)

def funeven(n, a):
	new1 = []
	f = 0
	for i in range(n):
		new1.append(f)
		f = abs(1 - f)
	ans = ff(a, new1)
	new2 = []
	f = 1
	for i in range(n):
		new2.append(f)
		f = abs(1 - f)
	ans = min(ans, ff(a, new2))
	print(ans)

def fun(a, n):
	if n % 2 == 1:
		funodd(n, a)
	else:
		funeven(n, a)

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

def solve():
	n = ii()
	a = li()
	o = 0
	e = 0
	for i in range(n):
		a[i] %= 2
		if a[i]:
			o += 1
		else:
			e += 1
	if max(o, e) > ceil(n):
		print(-1)
		return
	fun(a, n)
t = 1
t = ii()
for _ in range(t):
	solve()


from sys import stdin

def solve():
	n = int(stdin.readline().strip())
	seq = list(map(int, stdin.readline().split()))
	odd = []
	even = []
	for i in range(n):
		if seq[i] % 2:
			odd.append(i)
		else:
			even.append(i)
	if n % 2 == 0:
		if len(odd) != len(even):
			print(-1)
			return
		res_1 = 0
		res_2 = 0
		target = 0
		for (i, j) in zip(odd, even):
			res_1 += abs(i - target)
			res_2 += abs(j - target)
			target += 2
		print(min(res_1, res_2))
		return
	elif len(odd) > len(even):
		if len(odd) != n // 2 + 1:
			print(-1)
			return
		target = 0
		res = 0
		for i in odd:
			res += abs(i - target)
			target += 2
		print(res)
	else:
		if len(even) != n // 2 + 1:
			print(-1)
			return
		target = 0
		res = 0
		for i in even:
			res += abs(i - target)
			target += 2
		print(res)
t = int(stdin.readline().strip())
for _ in range(t):
	solve()


import math
import sys
from math import cos, pi, ceil, pi, tan, floor, gcd, sqrt, log2, factorial, log10
from collections import Counter
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase
from statistics import mean, median, mode
alpha = ascii_lowercase
beta = ascii_uppercase

def nextPowerOf2(n):
	p = 1
	if n and (not n & n - 1):
		return n
	while p < n:
		p <<= 1
	return p

def get_Divisors(n):
	arr = []
	i = 1
	while i <= sqrt(n):
		if n % i == 0:
			if n / i == i:
				arr.append(i)
			else:
				arr.append(i)
				arr.append(n // i)
		i = i + 1
	return arr

def isprime(n):
	if n == 1:
		return 0
	i = 2
	while i * i <= n:
		if n % i == 0:
			return 0
		i += 1
	return 1

def Sieve(n):
	prime = [True for i in range(n + 1)]
	prime[0] = False
	prime[1] = False
	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 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 create_matrix(n, m):
	mat = [[] for i in range(n)]
	for i in range(n):
		for j in range(m):
			mat[i].append(0)
	return mat
from bisect import *

def main():
	for tt in range(int(input())):
		n = int(input())
		arr = list(map(int, input().split()))
		o = 0
		e = 0
		for i in arr:
			if i % 2 == 0:
				e += 1
			else:
				o += 1
		if abs(o - e) > 1:
			print(-1)
		elif o > e:
			odd = []
			even = []
			for i in range(n):
				if (i + 1) % 2 == 1 and arr[i] % 2 == 0:
					odd.append(i)
				elif (i + 1) % 2 == 0 and arr[i] % 2 == 1:
					even.append(i)
			ans = 0
			for i in range(len(odd)):
				ans += abs(odd[i] - even[i])
			print(ans)
		elif o < e:
			odd = []
			even = []
			for i in range(n):
				if i % 2 == 0 and arr[i] % 2 == 1:
					even.append(i)
				elif i % 2 == 1 and arr[i] % 2 == 0:
					odd.append(i)
			ans = 0
			for i in range(len(odd)):
				ans += abs(odd[i] - even[i])
			print(ans)
		else:
			od = []
			odd = []
			ev = []
			even = []
			(ans1, ans2) = (0, 0)
			for i in range(n):
				if i % 2 == 0 and arr[i] % 2:
					ev.append(i)
				elif i % 2 and arr[i] % 2 == 0:
					od.append(i)
			for i in range(len(od)):
				ans1 += abs(od[i] - ev[i])
			for i in range(n):
				if (i + 1) % 2 == 0 and arr[i] % 2:
					even.append(i)
				elif (i + 1) % 2 and arr[i] % 2 == 0:
					odd.append(i)
			for i in range(len(odd)):
				ans2 += abs(odd[i] - even[i])
			print(min(ans1, ans2))
main()


for t in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	d1 = 0
	for i in range(n):
		if a[i] % 2:
			d1 += 1
			a[i] = 1
		else:
			a[i] = 0
	d0 = n - d1
	ans = 0
	if abs(d0 - d1) > 1:
		ans = -1
	else:
		zero = []
		one = []
		if d0 > d1:
			for i in range(n):
				if i % 2 == 0 and a[i] == 1:
					one.append(i)
				if i % 2 == 1 and a[i] == 0:
					zero.append(i)
			for i in range(len(one)):
				ans += abs(zero[i] - one[i])
		elif d1 > d0:
			for i in range(n):
				if i % 2 == 0 and a[i] == 0:
					zero.append(i)
				if i % 2 == 1 and a[i] == 1:
					one.append(i)
			for i in range(len(one)):
				ans += abs(zero[i] - one[i])
		else:
			for i in range(n):
				if i % 2 == 0 and a[i] == 1:
					one.append(i)
				if i % 2 == 1 and a[i] == 0:
					zero.append(i)
			ans0 = 0
			for i in range(len(one)):
				ans0 += abs(zero[i] - one[i])
			zero = []
			one = []
			for i in range(n):
				if i % 2 == 0 and a[i] == 0:
					zero.append(i)
				if i % 2 == 1 and a[i] == 1:
					one.append(i)
			ans1 = 0
			for i in range(len(one)):
				ans1 += abs(zero[i] - one[i])
			ans = min(ans0, ans1)
	print(ans)