Allen is hosting a formal dinner party. $2n$ people come to the event in $n$ pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The $2n$ people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.

Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.


-----Input-----

The first line contains a single integer $n$ ($1 \le n \le 100$), the number of pairs of people.

The second line contains $2n$ integers $a_1, a_2, \dots, a_{2n}$. For each $i$ with $1 \le i \le n$, $i$ appears exactly twice. If $a_j = a_k = i$, that means that the $j$-th and $k$-th people in the line form a couple.


-----Output-----

Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.


-----Examples-----
Input
4
1 1 2 3 3 2 4 4

Output
2

Input
3
1 1 2 2 3 3

Output
0

Input
3
3 1 2 3 1 2

Output
3



-----Note-----

In the first sample case, we can transform $1 1 2 3 3 2 4 4 \rightarrow 1 1 2 3 2 3 4 4 \rightarrow 1 1 2 2 3 3 4 4$ in two steps. Note that the sequence $1 1 2 3 3 2 4 4 \rightarrow 1 1 3 2 3 2 4 4 \rightarrow 1 1 3 3 2 2 4 4$ also works in the same number of steps.

The second sample case already satisfies the constraints; therefore we need $0$ swaps.

Difficulty: MEDIUM
Tags: Mathematics

Implementation

Greedy algorithms
Skill types: Greedy algorithms
Expected Time Complexity: O(N^2)

Starter code:

Solutions:
n = int(input())
s = input().split(' ')
bit = [0] * 2 * n

def decbit(position):
	if position >= 2 * n:
		return
	bit[position] -= 1
	decbit(position + (position & -position))

def getsum(left, right):
	return get(right) - get(left - 1)

def get(position):
	if position < 0:
		return 0
	if position == 0:
		return bit[0]
	return bit[position] + get(position - (position & -position))
left = [-1] * n
right = [-1] * n
isLeft = [False] * 2 * n
for i in range(2 * n):
	s[i] = int(s[i]) - 1
	if left[s[i]] != -1:
		right[s[i]] = i
	else:
		left[s[i]] = i
		isLeft[i] = True
res = 0
for i in range(2 * n):
	if isLeft[i]:
		res += right[s[i]] - i - 1 + getsum(i, right[s[i]])
		decbit(right[s[i]])
print(res)


n = int(input())
nums = input()
nums = [int(i) for i in nums.split()]
ans = 0
for i in range(0, len(nums), 2):
	target = nums[i]
	other = i + 1
	for j in range(i + 1, len(nums)):
		if nums[j] == target:
			other = j
	swaps = other - i - 1
	ans += swaps
	for j in range(other, i + 1, -1):
		(nums[j], nums[j - 1]) = (nums[j - 1], nums[j])
print(ans)


n = int(input())
a = list(map(int, input().split()))
c = 0
while len(a) > 0:
	x = a[0]
	a.remove(x)
	c += a.index(x)
	a.remove(x)
print(c)


n = int(input())
a = list(map(int, input().split()))
t = 0
for i in range(n):
	x = a[0]
	a.pop(0)
	y = a.index(x)
	t += y
	a.pop(y)
print(t)


n = int(input())
arr = list(map(int, input().split()))
ct = 0
ans = 0
for i in range(2 * n):
	for j in range(i + 2, 2 * n):
		if arr[j] == arr[i]:
			(p, q) = (i, j)
			while q - p != 1:
				(arr[q], arr[q - 1]) = (arr[q - 1], arr[q])
				q -= 1
				ct += 1
print(ct)


n = int(input())
ta = input().split()
a = []
vis = []
t = []
cnt = 0
for i in range(n * 2):
	a.append(int(ta[i]))
	vis.append(0)
	t.append(0)
for i in range(n * 2):
	if vis[a[i]]:
		continue
	vis[a[i]] = 1
	t[a[i]] = cnt
	cnt += 1
ans = 0
for i in range(n * 2):
	for j in range(i + 1, n * 2):
		if t[a[i]] > t[a[j]]:
			ans += 1
print(ans)


n = int(input())
a = [int(x) for x in input().split()]
low = 0
high = 2 * n - 1
ans = 0
while low < high:
	if a[low + 1] == a[low]:
		low += 2
	else:
		for i in range(low + 1, high + 1):
			if a[i] == a[low]:
				while i > low + 1:
					a[i] = a[i - 1]
					i -= 1
					ans += 1
				a[low + 1] = a[low]
				low += 2
				break
	if a[high - 1] == a[high]:
		high -= 2
	else:
		for i in range(high - 1, low - 1, -1):
			if a[i] == a[high]:
				while i < high - 1:
					a[i] = a[i + 1]
					i += 1
					ans += 1
				a[high - 1] = a[high]
				high -= 2
				break
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
while len(a) > 0:
	if a[0] == a[1]:
		a = a[2:]
		continue
	p = 0
	for i in range(1, len(a)):
		if a[i] == a[0]:
			p = i
			break
	a = a[1:p] + a[p + 1:]
	ans += p - 1
print(ans)


n = 2 * int(input())
a = list(map(int, input().split()))
z = 0
for i in range(0, n - 1, 2):
	if a[i] != a[i + 1]:
		for j in range(i + 1, n):
			if a[j] == a[i]:
				z += j - i - 1
				a.pop(j)
				a.insert(i + 1, a[i])
print(z)


n = int(input())
a = list(map(int, input().split()))
ans = 0
for _ in range(n - 1):
	b = a[0]
	i = a.index(a.pop(0))
	ans += i
	a.pop(i)
print(ans)


def func(x):
	d = {i: [] for i in range(1, n + 1)}
	ans = 0
	for i in range(len(x)):
		d[x[i]].append(i + 1)
	return d
for u in range(1):
	n = int(input())
	x = [int(w) for w in input().split()]
	ans = 0
	for j in range(n - 1):
		d = func(x)
		for i in d:
			if len(d[i]):
				if d[i][0] == 1:
					t = i
					break
		ans += d[t][1] - d[t][0] - 1
		x.remove(t)
		x.remove(t)
	print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
d = {}
c = {}
for i in range(1, n + 1):
	d[i] = 0
	c[i] = 0
for i in range(n - 1):
	for j in range(2 * n - i * 2):
		if c[a[j]] == 0:
			c[a[j]] += 1
			d[a[j]] += j
		else:
			d[a[j]] += j - 1
	mn = 100000000000
	nx = -1
	for j in d:
		if d[j] < mn:
			mn = d[j]
			nx = j
		d[j] = 0
		c[j] = 0
	del d[nx]
	del c[nx]
	ans += mn
	a = [i for i in a if i != nx]
print(ans)


n = int(input())
a = list(map(int, input().split()))
mo = 0
for i in range(0, 2 * n, 2):
	if a[i] != a[i + 1]:
		r = a.index(a[i], i + 1)
		for j in range(r, i + 1, -1):
			(a[j], a[j - 1]) = (a[j - 1], a[j])
			mo += 1
print(mo)


n = int(input()) * 2
a = list(map(int, input().split()))
ans = 0
i = 0
while i < n - 1:
	if a[i] != a[i + 1]:
		j = i + 1
		flag = False
		while j < n:
			if a[i] == a[j]:
				flag = True
				break
			j += 1
		if flag:
			temp = a[j]
			ans += j - (i + 1)
			a.insert(i + 1, temp)
			a.pop(j + 1)
	i += 2
print(ans)


n = int(input())
l = list(map(int, input().split(' ')))
(a, c) = (0, 0)
while l:
	a = l[0]
	l.remove(a)
	c += l.index(a)
	l.remove(a)
print(c)


n = int(input())
line = input().split(' ')
for i in range(len(line)):
	line[i] = int(line[i])
ans = 0
while len(line) != 0:
	if line[0] == line[1]:
		line.pop(0)
		line.pop(0)
	else:
		first = line.pop(0)
		idx = line.index(first)
		ans += idx
		line.pop(idx)
print(ans)


n = int(input())
couples = [*map(int, input().split())]
swap = 0
for i in range(0, 2 * n, 2):
	val = couples[i]
	if val != couples[i + 1]:
		ind = couples.index(val, i + 1)
		couples.insert(i + 1, couples.pop(ind))
		swap += ind - (i + 1)
print(swap)


n = int(input())
a = list(map(int, input().split()))
p = 0
while p + 1 < len(a) and a[p] == a[p + 1]:
	p += 2
c = 0
while p < len(a):
	if p + 1 < len(a):
		i = a.index(a[p], p + 1)
		c += i - p - 1
		tmp = a.pop(i)
		a.insert(p, tmp)
	while p + 1 < len(a) and a[p] == a[p + 1]:
		p += 2
print(c)


n = int(input())
li = [int(i) for i in input().split(' ')]
swaps = 0
while len(li) > 0:
	idx = li[1:].index(li[0]) + 1
	swaps += idx - 1
	li = li[1:idx] + li[idx + 1:]
print(swaps)


n = int(input())
s = [int(z) for z in input().split()]
counter = 0
for i in range(0, 2 * n, 2):
	sn = s[i]
	for j in range(i + 1, 2 * n):
		if s[j] == sn:
			for p in range(j, i + 1, -1):
				(s[p], s[p - 1]) = (s[p - 1], s[p])
				counter += 1
			break
print(counter)


n = int(input())
a = list(map(int, input().split()))
ans = 0
pos = 2 * n - 2
for i in range(n):
	x = a[-1]
	a.pop(-1)
	y = a.index(x)
	ans += pos - y
	pos -= 2
	a.pop(y)
print(ans)


n = int(input())
a = list(map(int, input().split()))
i = 0
c = 0
while i < 2 * n - 1:
	if a[i] == a[i + 1]:
		i = i + 2
	else:
		for j in range(i + 1, 2 * n):
			if a[j] == a[i]:
				k = j
				break
		for j in range(k, i + 1, -1):
			(a[j], a[j - 1]) = (a[j - 1], a[j])
			c = c + 1
		i = i + 2
print(c)


n = int(input())
a = [int(i) for i in input().split()]
c = 0
i = 0
while i < 2 * n:
	for j in range(i + 1, 2 * n):
		if a[i] == a[j]:
			while j != i + 1:
				(a[j], a[j - 1]) = (a[j - 1], a[j])
				j -= 1
				c += 1
	i += 2
print(c)


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

def find(l, x):
	try:
		return l.index(x)
	except ValueError:
		return -2

def solve(p):
	if len(p) == 0:
		return 0
	x = p[0]
	k = find(p[1:], x) + 1
	if k == -1:
		return solve(p[1:])
	return k - 1 + solve(p[1:k] + p[k + 1:])
print(solve(p))


n = int(input())
lis = [*map(int, input().split())]
ans = i = j = 0
while lis:
	fin = lis[i + 1:].index(lis[i]) + i + 1
	ans += fin - i - 1
	lis.pop(fin)
	i += 1
	if i == len(lis):
		break
print(ans)


def swap(l, a, b):
	(l[a], l[b]) = (l[b], l[a])
N = int(input())
a = list(map(int, input().split()))
i = 0
s = 0
while i < 2 * N - 2:
	if a[i] != a[i + 1]:
		ii = a.index(a[i], i + 1)
		d = ii - i - 1
		s += d
		for j in range(d):
			swap(a, i + 1 + (d - j - 1), i + 1 + (d - j))
	i += 2
print(s)


n = int(input())
line = list(map(int, input().split(' ')))
st = {}
ed = {}
for x in range(2 * n):
	if line[x] in st:
		ed[line[x]] = x
	else:
		st[line[x]] = x
ans = 0
for x in range(1, n + 1):
	for y in range(x + 1, n + 1):
		(u, v) = (x, y)
		if st[x] > st[y]:
			(u, v) = (v, u)
		if ed[v] < ed[u]:
			ans = ans + 2
		elif st[v] < ed[u]:
			ans = ans + 1
print(ans)


n = int(input())
pairs = [int(i) for i in input().split(' ')]
N = len(pairs)
N //= 2
visited = [False] * (N + 1)
minimumSwaps = 0
for i in range(2 * N):
	if visited[pairs[i]] == False:
		visited[pairs[i]] = True
		count = 0
		for j in range(i + 1, 2 * N):
			if visited[pairs[j]] == False:
				count += 1
			elif pairs[i] == pairs[j]:
				minimumSwaps += count
print(minimumSwaps)


n = int(input())
a = list(map(int, input().split()))
pairs = []
ans = 0
for i in range(2 * n):
	if a[i] not in pairs:
		for j in range(i + 1, 2 * n):
			if a[i] == a[j]:
				ans += j - i - 1
				a = a[:j] + a[j + 1:]
				a.insert(i, a[i])
				pairs.append(a[i])
				break
print(ans)


n = int(input())
arr = list(map(int, input().split()))
i = 0
moves = 0
for i in range(0, len(arr) - 1, 2):
	if arr[i] == arr[i + 1]:
		pass
	else:
		n_ind = arr[i + 1:].index(arr[i])
		arr.pop(n_ind + i + 1)
		arr.insert(i + 1, arr[i])
		moves += n_ind
print(moves)


n = int(input())
a = list(map(int, input().split()))
ct = 0
i = 0
while i < 2 * n:
	e = a[i]
	j = i + 1
	t = j + a[j:].index(e)
	el = a.pop(t)
	a = a[:j] + [el] + a[j:]
	ct += t - j
	i += 2
print(ct)


n = int(input())
l = list(map(int, input().split()))
num = 0
while len(l) > 0:
	if l[0] == l[1]:
		l = l[2:]
	else:
		x = l[0]
		l = l[1:]
		s = l.index(x)
		num += s
		l.remove(l[s])
print(num)


import sys
input = sys.stdin.readline
n = int(input())
c = list(map(int, input().split()))
ans = 0
for i in range(n):
	f = c.pop(0)
	g = c.index(f)
	c.pop(g)
	ans += g
print(ans)


import collections
from functools import cmp_to_key
import math
import sys

def getIntList():
	return list(map(int, input().split()))
import bisect
try:
	import numpy
	dprint = print
	dprint('debug mode')
except ModuleNotFoundError:

	def dprint(*args, **kwargs):
		pass

def makePair(z):
	return [(z[i], z[i + 1]) for i in range(0, len(z), 2)]

def memo(func):
	cache = {}

	def wrap(*args):
		if args not in cache:
			cache[args] = func(*args)
		return cache[args]
	return wrap

@memo
def comb(n, k):
	if k == 0:
		return 1
	if n == k:
		return 1
	return comb(n - 1, k - 1) + comb(n - 1, k)
(N,) = getIntList()
za = getIntList()
res = 0
for i in range(0, 2 * N, 2):
	if za[0] == za[1]:
		za = za[2:]
		continue
	g = za[0]
	dprint(g)
	t = za.index(g, 1)
	res += t - 1
	za.remove(g)
	za.remove(g)
	dprint(za)
print(res)


n = int(input())
u = list(map(int, input().split()))
ans = 0
i = 0

def finl(u, p, j):
	for i in range(j + 1, len(u)):
		if u[i] == p:
			return i
	return -1
while i < 2 * n:
	j = finl(u, u[i], i)
	ans += j - i - 1
	u = u[:i + 1] + u[j:j + 1] + u[i + 1:j] + u[j + 1:]
	i += 2
print(ans)


n = int(input())
a = input().split(' ')
s = set()
ans = 0
for i in range(2 * n - 1):
	for j in range(i + 1, 2 * n):
		if a[i] != a[j] and a[j] not in s and (a[i] not in s):
			ans = ans + 1
		elif a[i] == a[j]:
			s.add(a[i])
			break
print(ans)


import time
n = int(input())
a = [int(i) for i in input().split()]
start = time.time()
ans = 0
for i in range(2 * n):
	if a[i] == -1:
		continue
	t = 0
	for j in range(i + 1, 2 * n):
		if a[j] == -1:
			t += 1
		if a[j] == a[i]:
			break
	ans += j - i - 1 - t
	a[j] = -1
print(ans)
finish = time.time()


n = int(input().strip())
ais = list(map(int, input().strip().split()))
cnt = 0
while len(ais) > 0:
	a = ais[0]
	del ais[0]
	i = ais.index(a)
	cnt += i
	del ais[i]
print(cnt)


n = int(input())
p = list(map(int, input().split()))
ch = [0 for i in range(n + 1)]
for i in range(0, 2 * n, 2):
	if p[i] == p[i + 1]:
		ch[p[i]] = 1
count = 0
for i in range(0, 2 * n, 2):
	if p[i + 1] != p[i]:
		for j in range(i + 1, 2 * n):
			if p[j] == p[i]:
				count = count + j - i
				count -= 1
				for l in range(j, i + 1, -1):
					p[l] = p[l - 1]
				p[i + 1] = p[i]
				ch[p[i]] = 1
				break
print(count)


n = int(input())
l = list(map(int, input().strip().split()))
lo = [-1 for i in range(n + 1)]
lc = [-1 for j in range(n + 1)]
lp = [[] for u in range(n + 1)]
open1 = []
count = 0
for i in range(2 * n):
	if lo[l[i]] == -1:
		for j in open1:
			lp[l[i]].append(j)
		open1.append(l[i])
		lo[l[i]] = i + 1
	else:
		r = 0
		lc[l[i]] = i + 1
		for j in lp[l[i]]:
			if lc[j] != -1:
				r = r + 1
		open1.remove(l[i])
		count = count + lc[l[i]] - lo[l[i]] - 1 - r
print(count)


input()
a = list(map(int, input().split()))
cnt = 0
while a:
	i = a.index(a.pop(0))
	cnt += i
	a.pop(i)
print(cnt)


from collections import defaultdict

def get_ind(j):
	global indexes
	indexes = defaultdict(list)
	for i in range(j, 2 * n):
		indexes[a[i]].append(i)
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, 2 * n, 2):
	get_ind(i)
	el = a[i]
	p = indexes[el].pop()
	for j in range(i + 1, p)[::-1]:
		(a[j], a[j + 1]) = (a[j + 1], a[j])
		ans += 1
print(ans)


import sys
import math
input = sys.stdin.readline
n = int(input())
arr = list(map(int, input().split()))
ans = 0
i = 0
while i < 2 * n:
	curr = 0
	for j in range(i + 1, 2 * n):
		if arr[j] == arr[i]:
			curr = j
			break
	while curr > i + 1:
		arr[curr] = arr[curr] + arr[curr - 1]
		arr[curr - 1] = arr[curr] - arr[curr - 1]
		arr[curr] = arr[curr] - arr[curr - 1]
		ans += 1
		curr -= 1
	i += 2
print(ans)


n = int(input())
used = [0] * (n + 1)
a = list(map(int, input().split()))
r = [0] * (n + 1)
l = [0] * (n + 1)
c = [0] * (n + 1)
for i in range(2 * n):
	r[a[i]] = i
for i in range(2 * n - 1, -1, -1):
	l[a[i]] = i
for i in range(n, 0, -1):
	c[i] = r[i] - l[i] - 1
res = 0
for i in range(2 * n):
	if used[a[i]]:
		continue
	used[a[i]] = 1
	res += c[a[i]]
	for j in range(1, n + 1):
		if l[j] <= r[a[i]] and r[a[i]] <= r[j]:
			c[j] -= 1
print(res)


def findMinimumAdjacentSwaps(a, n):
	visited = [0] * (n + 1)
	ans = 0
	for i in range(2 * n):
		if visited[a[i]] == 0:
			visited[a[i]] = 1
			count = 0
		for j in range(i + 1, 2 * n):
			if visited[a[j]] == 0:
				count += 1
			elif a[i] == a[j]:
				ans += count
	return ans
n = int(input())
a = list(map(int, input().split()))
ans = findMinimumAdjacentSwaps(a, n)
print(ans)


n = int(input())
arr = list(map(int, input().split()))
ans = 0
for i in range(0, n * 2, 2):
	fi = arr.index(arr[i], i + 1, len(arr))
	if i + 1 != fi:
		ans += fi - i - 1
		del arr[fi]
		arr.insert(i + 1, arr[i])
print(ans)


def unpaired(x):
	n = 0
	q = [[] for i in range(len(x) // 2)]
	for i in range(0, len(x)):
		if i + 1 < len(x) and x[i] == x[i + 1]:
			n += 1
		q[x[i] - 1].append(i)
	y = []
	for i in q:
		y.append(abs(i[0] - i[1]) - 1)
	return [len(x) // 2 - n, y]
n = int(input())
a = [int(i) for i in input().split()]
nswaps = 0
(unp, arr) = unpaired(a)
for i in range(2 * n):
	if unp == 0:
		break
	if arr[a[i] - 1] != 0:
		nswaps += arr[a[i] - 1]
		for j in range(i + arr[a[i] - 1] + 1, i, -1):
			a[j] = a[j - 1]
		a[i + 1] = a[i]
		(unp, arr) = unpaired(a)
print(nswaps)


def solve(N, A):
	c = 0
	for i in range(N):
		if A[2 * i] == A[2 * i + 1]:
			continue
		j = A.index(A[2 * i], 2 * i + 1)
		c += j - (2 * i + 1)
		for k in range(j - 1, 2 * i, -1):
			A[k + 1] = A[k]
		A[2 * i + 1] = A[2 * i]
	return c

def main():
	N = int(input())
	A = [int(e) for e in input().split(' ')]
	assert len(A) == 2 * N
	print(solve(N, A))
main()


n = int(input())
a = [*[int(x) - 1 for x in input().split()]]
ans = 0
f = [True] * n
for i in range(2 * n - 1):
	if f[a[i]]:
		j = i + 1
		while a[j] != a[i]:
			if f[a[j]]:
				ans += 1
			j += 1
		f[a[i]] = False
print(ans)


def p(l, i, j):
	p = int(l[i])
	l[i] = int(l[j])
	l[j] = p
	return l
n = int(input())
l = list(map(int, input().split()))
c = 0
for i in range(0, n * 2 - 1, 2):
	if l[i] == l[i + 1]:
		continue
	else:
		for j in range(i + 1, n * 2):
			if l[i] == l[j]:
				while l[i] != l[i + 1]:
					l = p(l, j - 1, j)
					c += 1
					j -= 1
print(c)


n = int(input())
arr = list(map(int, input().split()))
swap = 0
for i in range(0, 2 * n, 2):
	ind = arr[i + 1:].index(arr[i]) + i + 1
	if ind != i + 1:
		arr = arr[:i] + arr[ind:ind + 1] + arr[i:ind] + arr[ind + 1:]
		swap += ind - i - 1
print(swap)


import os
import sys
from io import BytesIO, IOBase

def main():
	n = int(input())
	ans = 0
	a = list(map(int, input().split(' ')))
	n = len(a)
	for x in range(0, n, 2):
		for y in range(x + 1, n):
			if a[y] == a[x]:
				yy = y
				while a[x + 1] != a[x]:
					(a[yy], a[yy - 1]) = (a[yy - 1], a[yy])
					yy -= 1
					ans += 1
	print(ans)
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')
main()


l = int(input())
a = list(map(int, input().split()))
count = 0
i = 0
n = 2 * l
while i < n - 2:
	if a[i] == a[i + 1]:
		i += 2
		continue
	j = i + 2
	while j < n and a[j] != a[i]:
		j += 1
	while j > i + 1:
		count += 1
		(a[j - 1], a[j]) = (a[j], a[j - 1])
		j -= 1
	i += 2
print(count)


n = int(input())
m = list(map(int, input().split()))
al = 0
for i in range(0, 2 * n - 1, 2):
	if m[i] != m[i + 1]:
		di = 0
		di1 = 0
		for j in range(i + 2, 2 * n):
			if m[j] == m[i]:
				di = j - i - 1
			elif m[j] == m[i + 1]:
				di1 = j - i
		al += min(di, di1)
		if di < di1:
			m = m[:i + 1] + [m[di + i + 1]] + [m[i + 1]] + m[i + 2:di + i + 1] + m[di + i + 2:]
		else:
			m = m[:i] + [m[di1 + i]] + [m[i + 1]] + [m[i]] + m[i + 2:di1 + i] + m[di1 + i + 1:]
print(al)


n = int(input())
data = [int(i) for i in input().split()]
pos = {}
here = {}
started = {}
for i in range(n + 1):
	pos[i] = []
	here[i] = 0
	started[i] = 0
for i in range(2 * n):
	pos[data[i]].append(i)
qu = []
for i in data:
	if here[i] == 0:
		qu.append(i)
		here[i] = 1
ans = 0
for i in qu:
	started[i] = 1
	for j in range(pos[i][0], pos[i][1]):
		if not started[data[j]]:
			ans += 1
print(ans)


R = lambda : map(int, input().split())
n = int(input())
a = list(R())
s = 0
while a:
	if a[0] == a[1]:
		a = a[2:]
	else:
		i = a[2:].index(a[0]) + 2
		a = a[1:i] + a[i + 1:]
		s += i - 1
print(s)


n = int(input())
a = list(map(int, input().split()))
n *= 2
k = 0
for i in range(n - 1):
	for j in range(i + 1, n):
		if a[j] == a[i]:
			k += j - i - 1
			for l in range(j, i, -1):
				a[l] = a[l - 1]
			a[i + 1] = a[i]
print(k)


n = int(input())
s = list(map(int, input().split()))
t = 0
while len(s) > 0:
	ls = len(s)
	s1 = s.index(s[0], 1)
	sn = s.index(s[-1])
	if s1 < ls - sn - 1:
		s = s[1:s1] + s[s1 + 1:]
		t += s1 - 1
	else:
		t += ls - sn - 2
		s = s[:sn] + s[sn + 1:ls - 1]
print(t)


j = int(input()) * 2
l = list(map(int, input().split()))
for i in range(0, j, 2):
	if l[i] != l[i + 1]:
		break
for j in range(j, 1, -2):
	if l[j - 2] != l[j - 1]:
		break
(l, res) = (l[i:j], 0)
while l:
	i = l.index(l.pop())
	res += len(l) - i - 1
	del l[i]
print(res)


n = int(input())
s = list(map(int, input().split()))
swap = [0 for i in range(n + 1)]
for i in range(0, 2 * n, 2):
	if s[i] == s[i + 1]:
		swap[s[i]] = 1
cnt = 0
for i in range(0, 2 * n, 2):
	if swap[s[i]] == 0 and s[i + 1] != s[i]:
		for j in range(i + 1, 2 * n):
			if s[j] == s[i]:
				cnt += j - i - 1
				k = s[i + 1]
				for l in range(j, i + 1, -1):
					s[l] = s[l - 1]
				s[i + 1] = s[i]
				swap[s[i]] = 1
				break
print(cnt)


N = int(input())
a = [int(c) for c in input().split()]
pos = [-1] * (N + 1)
pos2 = [-1] * (N + 1)
count = 0
out = 0
for c in range(2 * N):
	if pos[a[c]] != -1:
		count += c - pos[a[c]] - 1
		pos2[a[c]] = c
	else:
		pos[a[c]] = c
for c in range(1, N + 1):
	found = [False] * (N + 1)
	for d in range(pos[c] + 1, pos2[c]):
		if found[a[d]]:
			out += 1
		else:
			found[a[d]] = True
print(out + count // 2)


n = int(input())
s = input()
s = s.split()
a = []
for i in range(2 * n):
	a.append(int(s[i]))
ans = 0
for i in range(0, 2 * n, 2):
	if a[i] == a[i + 1]:
		continue
	index = 0
	for j in range(i + 1, 2 * n):
		if a[j] == a[i]:
			index = j
			break
	for j in range(index, i + 1, -1):
		(a[j], a[j - 1]) = (a[j - 1], a[j])
		ans += 1
print(ans)


N = int(input())
flag = 1
a = [int(i) for i in input().split()]
Sum = 0
for i in range(0, len(a) - 1, 2):
	j = i + 1
	while a[j] != a[i]:
		j += 1
	Sum += j - i - 1
	a = a[:i + 1] + [a[j]] + a[i + 1:j] + a[j + 1:]
print(Sum)


n = int(input())
a = list(map(int, input().split()))
n = n * 2
ans = 0
for i in range(n):
	if a[i] != 0:
		for j in range(i + 1, n):
			if a[j] == 0:
				continue
			elif a[j] != a[i]:
				ans += 1
			else:
				a[i] = a[j] = 0
				break
print(ans)


n = int(input())
a = list(map(int, input().split()))
c = 0
for _i in range(n):
	i = 2 * _i
	for j in range(i + 1, 2 * n):
		if a[i] == a[j]:
			a[i + 2:j + 1] = a[i + 1:j]
			a[i + 1] = a[j]
			c += j - i - 1
			break
print(c)


from collections import defaultdict
from math import *
import os
import sys
from io import BytesIO, IOBase
from heapq import nlargest
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

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

def changearray(j, i):
	b = arr[i]
	for k in range(i, j, -1):
		arr[k] = arr[k - 1]
	arr[j] = b

def checker(j, x):
	for i in range(j + 1, n + n):
		if arr[i] == x:
			changearray(j, i)
			return i
n = int(input())
arr = list(map(int, input().split()))
ans = 0
i = 0
while i < n + n - 1:
	if arr[i] != arr[i + 1]:
		ans += checker(i, arr[i]) - i - 1
	i += 2
print(ans)


TN = 1

def solution():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(0, 2 * n, 2):
		if a[i] != a[i + 1]:
			ind = i + 1 + a[i + 1:].index(a[i])
			ans += ind - i - 1
			x = a[ind]
			a[i + 2:ind + 1] = a[i + 1:ind]
			a[i + 1] = x
	print(ans)
while TN != 0:
	solution()
	TN -= 1


def swap(x1, x2, a):
	temp = a[x1]
	a[x1] = a[x2]
	a[x2] = temp

def shift(fr, to, a):
	for i in range(fr, to - 1, -1):
		swap(i, i - 1, a)
n = int(input())
a = [int(x) for x in input().split(' ')]
answer = 0
for i in range(n):
	pos1 = 2 * i
	pos2 = a.index(a[pos1], pos1 + 1)
	answer += pos2 - pos1 - 1
	shift(pos2, pos1 + 1, a)
print(answer)


n = int(input())
a = list(map(int, input().split()))
ans = 0
while len(a) > 0:
	x = a.pop(0)
	ind = a.index(x)
	ans += ind
	a.pop(ind)
print(ans)


(n, s) = (int(input()), 0)
(*a,) = map(int, input().split())
for i in range(2 * n - 1, -1, -2):
	s += i - a.index(a[i]) - 1
	a.remove(a[i])
print(s)


def main():
	n = int(input())
	a = list(map(int, input().split()))
	r = 0
	while a:
		c = a[0]
		del a[0]
		for i in range(len(a)):
			if c == a[i]:
				break
		del a[i]
		r += i
	print(r)
main()


import sys
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
swaps = 0
for i in range(0, 2 * n, 2):
	if a[i] == a[i + 1]:
		continue
	c = a[i]
	j = i + 2
	while c != a[j]:
		j += 1
	MIN = i + 1
	while j > MIN:
		(a[j], a[j - 1]) = (a[j - 1], a[j])
		j -= 1
		swaps += 1
print(swaps)


n = int(input())
a = [int(x) for x in input().split()]
used = [0 for _ in range(n * 2)]
res = 0
for i in range(n * 2):
	if used[i]:
		continue
	cnt = 0
	for j in range(i + 1, n * 2):
		if a[i] == a[j]:
			used[j] = 1
			break
		if used[j] == 0:
			cnt += 1
	res += cnt
print(res)


_ = int(input())
ns = list(map(int, input().split()))
seen = [False for _ in range(len(ns) // 2 + 1)]
count = 0
index = 0
while index != len(ns):
	e = ns[index]
	if seen[e]:
		while ns[index - 1] != e:
			(ns[index], ns[index - 1]) = (ns[index - 1], ns[index])
			seen[ns[index]] = False
			count += 1
			index -= 1
	seen[e] = True
	index += 1
print(count)


n = int(input())
a = list(map(int, input().split()))
z = [1] * (2 * n)
o = 0
for i in range(2 * n - 1):
	if z[i] == 1:
		b = a[i + 1:].index(a[i]) + i + 1
		z[b] = 0
		o += sum(z[i + 1:b])
print(o)


from sys import stdin

def solve(tc):
	n = int(stdin.readline().strip())
	li = list(map(int, stdin.readline().split()))
	ans = 0
	for i in range(0, 2 * n, 2):
		for j in range(i + 1, 2 * n):
			if li[i] == li[j]:
				for k in range(j, i + 1, -1):
					ans += 1
					tmp = li[k]
					li[k] = li[k - 1]
					li[k - 1] = tmp
				break
	print(ans)
LOCAL_TEST = not __debug__
if LOCAL_TEST:
	infile = __file__.split('.')[0] + '-test.in'
	stdin = open(infile, 'r')
tcs = int(stdin.readline().strip()) if LOCAL_TEST else 1
tc = 1
while tc <= tcs:
	solve(tc)
	tc += 1


n = int(input())
li = [*map(int, input().split())]
count = 0
while len(li) > 0:
	a = li[0]
	li.remove(a)
	count += li.index(a)
	li.remove(a)
print(count)


from queue import deque
n = int(input())
arr = deque(map(int, input().split()))
ans = 0
for i in range(0, n * 2, 2):
	fi = arr.index(arr[i], i + 1, len(arr))
	if i + 1 != fi:
		ans += fi - i - 1
		del arr[fi]
		arr.insert(i + 1, arr[i])
print(ans)


def solu(n, l):
	total = 0
	for i in range(0, n - 1):
		i *= 2
		k = l[i]
		m = l[i + 1:].index(k)
		if m > 0:
			total += m
			l = l[:i + 2] + l[i + 1:i + 1 + m] + l[i + 2 + m:]
	return total
n = int(input())
l = list(map(int, input().split()))
print(solu(n, l))


N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
	b = a[i]
	a[i] = a[i] + 1000
	c = a.index(b)
	ans += c - i - 1
	a.remove(b)
print(ans)


def shift(ar, pos, req):
	cur = ar[pos]
	while pos > req:
		ar[pos] = ar[pos - 1]
		pos -= 1
	ar[req] = cur
n = int(input())
ar = list(map(int, input().split()))
i = 0
ans = 0
while i < 2 * n:
	pos = i + 1 + ar[i + 1:].index(ar[i])
	if pos - i > 1:
		ans += pos - (i + 1)
		shift(ar, pos, i + 1)
	i += 2
print(ans)


n = int(input())
arr = list(map(int, input().split()))
c = 0
for i in range(n):
	if arr[0] != arr[1]:
		index = arr[::-1].index(arr[0])
		c += len(arr) - 1 - index - 1
		del arr[len(arr) - 1 - index]
		del arr[0]
	else:
		del arr[0]
		del arr[0]
print(c)


input()
a = input().split()
cnt = 0
while len(a) > 0:
	k = a[1:].index(a[0]) + 1
	cnt += k - 1
	a = a[1:k] + a[k + 1:]
print(cnt)


n = int(input())
arr = input().split(' ')
arr = [int(x) for x in arr]
swaps = 0

def swap_array(i, j):
	swaps = 0
	for x in reversed(range(i + 1, j + 1)):
		temp = arr[x - 1]
		arr[x - 1] = arr[x]
		arr[x] = temp
		swaps += 1
	return swaps
for i in range(2 * n):
	for j in range(i + 1, 2 * n):
		if arr[i] == arr[j]:
			if j != i + 1:
				swaps += swap_array(i + 1, j)
print(swaps)


def count_inversions(lst):
	return merge_count_inversion(lst)[1]

def merge_count_inversion(lst):
	if len(lst) <= 1:
		return (lst, 0)
	middle = len(lst) // 2
	(left, a) = merge_count_inversion(lst[:middle])
	(right, b) = merge_count_inversion(lst[middle:])
	(result, c) = merge_count_split_inversion(left, right)
	return (result, a + b + c)

def merge_count_split_inversion(left, right):
	result = []
	count = 0
	(i, j) = (0, 0)
	left_len = len(left)
	while i < left_len and j < len(right):
		if left[i] <= right[j]:
			result.append(left[i])
			i += 1
		else:
			result.append(right[j])
			count += left_len - i
			j += 1
	result += left[i:]
	result += right[j:]
	return (result, count)
n = int(input())
values = [int(i) for i in input().split()]
rearranged = []
s = dict()
k = 0
for i in values:
	if i not in s:
		rearranged.append(k)
		s[i] = k
		k += 1
	else:
		rearranged.append(s[i])
print(count_inversions(rearranged))


n = int(input())
arr = list(map(int, input().split()))
dic = {i: [] for i in range(1, n + 1)}
for i in range(len(arr)):
	dic[arr[i]] += [i]
ans = 0
for i in range(1, n + 1):
	for j in range(i + 1, n + 1):
		(ai, bi) = dic[i]
		(aj, bj) = dic[j]
		if ai < aj:
			if bj < bi:
				ans += 2
			elif bi < aj:
				ans += 0
			else:
				ans += 1
		elif bi < bj:
			ans += 2
		elif bj < ai:
			ans += 0
		else:
			ans += 1
print(ans)


n = int(input())
n = 2 * n
couples = list(map(int, input().split()))
moves = 0
for i in range(0, n, 2):
	curr = couples[i]
	for j in range(i + 1, n):
		if couples[j] == curr:
			for k in range(j - 1, i, -1):
				couples[k + 1] = couples[k]
				moves += 1
			couples[i + 1] = couples[i]
			break
		else:
			continue
print(moves)


def sol(s):
	if len(s) == 0:
		return 0
	if s[0] == s[1]:
		return sol(s[2:])
	ind = s[1:].index(s[0])
	return ind + sol(s[1:ind + 1] + s[ind + 2:])
n = int(input())
s = list(map(int, input().split()))
print(sol(s))


n = int(input())
lst = list(map(int, input().strip().split(' ')))
c = 0
while len(lst) != 0:
	p = lst[0]
	del lst[0]
	i = lst.index(p)
	c += i
	del lst[i]
print(c)


n = int(input())
arr = list(map(int, input().split()))
ans = 0
while len(arr) != 0:
	e = arr.pop(0)
	ans += arr.index(e)
	arr.remove(e)
print(ans)


n = int(input())
arr = list(map(int, input().split()))
cnt = 0
for i in range(0, 2 * n - 1, 2):
	if arr[i] != arr[i + 1]:
		idx = None
		for j in range(i + 2, 2 * n):
			if arr[j] == arr[i]:
				idx = j
				break
		for j in reversed(range(i + 1, idx)):
			tmp = arr[j]
			arr[j] = arr[j + 1]
			arr[j + 1] = tmp
			cnt += 1
print(cnt)


n = int(input())
arr = list(map(int, input().split()))
count = 0
for i in range(0, n):
	index = arr[2 * i + 1:].index(arr[2 * i]) + i * 2 + 1
	for j in range(index, 2 * i + 1, -1):
		(arr[j], arr[j - 1]) = (arr[j - 1], arr[j])
		count += 1
print(count)


n = int(input())
l = list(map(int, input().split()))
ans = 0
while len(l) != 0:
	ans += l.index(l[0], 1) - 1
	c = l.pop(0)
	l.remove(c)
print(ans)


L = [0] * 1000
Q = [0] * 1000
W = []
n = int(input())
p = input().rstrip().split(' ')
for i in range(0, len(p)):
	if L[int(p[i])] == 0:
		L[int(p[i])] = i + 1
		W.append(int(p[i]))
	else:
		Q[int(p[i])] = i + 1
D = []
ans = 0
M = 0
for i in range(0, len(W)):
	A = L[W[i]]
	B = Q[W[i]]
	ans += B - A - 1
	for j in range(0, len(L)):
		if L[j] >= M and L[j] < B:
			L[j] += 1
		if Q[j] >= M and Q[j] < B:
			Q[j] += 1
print(ans)


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

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

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

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

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

class IOWrapper(IOBase):

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

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

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

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

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

def factors(n):
	fac = set()
	fac.add(1)
	fac.add(n)
	for i in range(2, int(math.sqrt(n)) + 1):
		if n % i == 0:
			fac.add(i)
			fac.add(n // i)
	return list(fac)
n = int(input())
a = list(map(int, input().split()))
ans = 0
while len(a) > 0:
	c = a.pop(0)
	i = a.index(c)
	ans += i
	del a[i]
print(ans)


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations, permutations
from bisect import *
from fractions import Fraction
from heapq import *
from random import randint

def main():
	n = int(input())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(0, 2 * n, 2):
		if a[i] != a[i + 1]:
			for j in range(i + 1, 2 * n):
				if a[j] == a[i]:
					for k in range(j, i + 1, -1):
						(a[k], a[k - 1]) = (a[k - 1], a[k])
						ans += 1
					break
	print(ans)
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')
main()


def sort(a):
	ans = 0
	for i in range(0, 2 * n - 2, 2):
		j = i + 1
		while a[j] != a[i]:
			j += 1
		for k in range(j, i + 1, -1):
			(a[k], a[k - 1]) = (a[k - 1], a[k])
		ans += j - i - 1
	return ans
n = int(input())
a = [int(i) for i in input().split()]
print(sort(a))


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	cur = a[2 * i]
	j = 2 * i + 1
	t = 0
	for k in range(j, n * 2):
		if a[k] == cur:
			t = k
			break
	for k in range(t, j, -1):
		a[k] = a[k - 1]
		ans += 1
	a[2 * i + 1] = cur
print(ans)


n = 2 * int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(1, len(a), 2):
	j = i
	while a[j] != a[i - 1]:
		j += 1
	while j != i:
		ans += 1
		(a[j - 1], a[j]) = (a[j], a[j - 1])
		j -= 1
print(ans)


def minSwaps(n, pairs, a):
	i = 0
	c = 0
	vis = [0] * (2 * n + 1)
	while i < 2 * n + 1:
		j = i + 1
		while j < 2 * n + 1 and vis[j]:
			j += 1
		if j >= 2 * n:
			break
		if a[i] == a[j]:
			vis[i] = 1
			vis[j] = 1
		else:
			k = sum(vis[i + 1:pairs[i]])
			f = pairs[i] - i - 1 - k
			k = sum(vis[j + 1:pairs[j]])
			s = pairs[j] - j + 1 - k
			if f < s:
				vis[i] = 1
				vis[pairs[i]] = 1
				c += f
			else:
				vis[j] = 1
				vis[pairs[j]] = 1
				c += s
		while i < 2 * n + 1 and vis[i]:
			i += 1
	return c
n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
d = {}
pairs = [0] * (2 * n)
for i in range(2 * n):
	if a[i] in d:
		pairs[d[a[i]]] = i
		pairs[i] = d[a[i]]
	else:
		d[a[i]] = i
print(minSwaps(n, pairs, a))


from __future__ import print_function
import sys
import math
import os.path
import random
from copy import deepcopy
from functools import reduce
from collections import Counter, ChainMap, defaultdict
from itertools import cycle, chain
from queue import Queue, PriorityQueue, deque
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, nlargest, nsmallest
import bisect
from statistics import mean, mode, median, median_low, median_high
sys.setrecursionlimit(10 ** 9)

def log(*args, **kwargs):
	print(*args, file=sys.stderr, **kwargs)

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

def nio(offset):
	return map(lambda x: int(x) + offset, input().split())

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

def toString(aList, sep=' '):
	return sep.join((str(x) for x in aList))

def toMapInvertIndex(aList):
	return {k: v for (v, k) in enumerate(aList)}

def sortId(arr):
	return sorted(range(arr), key=lambda k: arr[k])
(n,) = ni()
a = deque(nia())
s = 0
for _ in range(n):
	x = a.popleft()
	ix = a.index(x)
	s += ix
	a.remove(x)
print(s)


n = int(input())
ar = [int(i) for i in input().split()]
ans = 0
for i in range(2 * n):
	for j in range(i + 1, 2 * n):
		if ar[i] == ar[j]:
			while j != i + 1:
				(ar[j], ar[j - 1]) = (ar[j - 1], ar[j])
				j -= 1
				ans += 1
print(ans)


input()
a = [*map(int, input().split())]
c = 0
while a:
	i = a.index(a.pop(0))
	c += i
	del a[i]
print(c)


import sys

def fastio():
	from io import StringIO
	from atexit import register
	global input
	sys.stdin = StringIO(sys.stdin.read())
	input = lambda : sys.stdin.readline().rstrip('\r\n')
	sys.stdout = StringIO()
	register(lambda : sys.__stdout__.write(sys.stdout.getvalue()))
fastio()

def debug(*var, sep=' ', end='\n'):
	print(*var, file=sys.stderr, sep=sep, end=end)
INF = 10 ** 20
MOD = 10 ** 9 + 7
I = lambda : list(map(int, input().split()))
from math import gcd
from collections import defaultdict as dd, Counter
from bisect import bisect_left as bl, bisect_right as br
(n,) = I()
a = I()
ans = 0
v = [0] * 2 * n
for i in range(0, 2 * n, 2):
	v[i] = 1
	if a[i] == a[i + 1]:
		continue
	cur = a[i + 1]
	swaps = 0
	for j in range(i + 1, 2 * n):
		if a[j] == a[i]:
			break
		swaps += 1
	for k in range(j, i + 1, -1):
		a[k] = a[k - 1]
	a[i + 1] = a[i]
	debug(swaps)
	ans += swaps
print(ans)


n = int(input())
n = n * 2
x = [int(i) for i in input().split()]
swap = 0
while 0 < n:
	temp = x[1:].index(x[0])
	swap = swap + temp
	x.remove(x[0])
	x.remove(x[temp])
	n = n - 2
print(swap)


n = int(input())
l = list(map(int, input().split()))
i = 0
ans = 0
while i < len(l) - 1:
	if l[i] == l[i + 1]:
		i = i + 1
		continue
	j = i + 1
	ind = -1
	while j < len(l):
		if l[j] == l[i]:
			ind = j
			break
		j = j + 1
	while ind > i + 1:
		(l[ind], l[ind - 1]) = (l[ind - 1], l[ind])
		ans += 1
		ind -= 1
	i += 1
print(ans)


n = int(input())
a = list(map(int, input().split()))
vis = [0] * (n + 1)
ans = 0
for i in range(len(a)):
	if vis[a[i]] == 0:
		vis[a[i]] = 1
		count = 0
	for j in range(i + 1, 2 * n):
		if vis[a[j]] == 0:
			count += 1
		elif a[i] == a[j]:
			ans += count
print(ans)


n = int(input())
positions = list(map(int, input().split(' ')))
swaps = 0
for i in range(n):
	number = positions[0]
	swaps += positions[1:].index(number)
	positions.remove(number)
	positions.remove(number)
print(swaps)


n = input()
n = int(n)
a = [int(i) for i in input().split()]
d = []
l = []
n = 2 * n
d = {i: -1 for i in a}
point = 0
count = 0
for i in range(0, n):
	l.append(a[i])
	if d[a[i]] == -1:
		d[a[i]] = 0
	elif d[a[i]] == 0:
		d[a[i]] = 1
		j = l.index(a[i])
		l.pop()
		l.insert(j + 1, a[i])
		count = count + (i - j - 1)
		if j == point:
			point = point + 2
print(count)


n = int(input())
n *= 2
a = [*map(int, input().split())]
c = 0
for i in range(1, n, 2):
	j = next((j for j in range(i, n) if a[j] == a[i - 1]))
	c += j - i
	(a[i], a[i + 1:j + 1]) = (a[j], a[i:j])
print(c)


def adjacent_swap(l, u, v):
	for i in range(v, u, -1):
		(l[i], l[i - 1]) = (l[i - 1], l[i])
	return v - u - 1

def adjust(array):
	if not array:
		return 0
	distance = adjacent_swap(array, 0, 1 + array[1:].index(array[0]))
	return distance + adjust(array[2:])

def main():
	n = int(input())
	a = [int(item) for item in input().split()]
	locate = [[] for _ in range(n + 1)]
	for i in range(n):
		locate[a[i]] += [i]
	print(adjust(a))
while 1:
	try:
		main()
	except EOFError:
		break


def go():
	n = int(input())
	a = [int(i) for i in input().split(' ')]
	index = 0
	moves = 0
	while index < 2 * n - 1:
		if a[index] == a[index + 1]:
			index += 2
		else:
			search = a[index]
			for i in range(index + 1, 2 * n):
				if search == a[i]:
					a.insert(index + 1, a.pop(i))
					moves += i - index - 1
					index += 2
					break
	return moves
print(go())


n = int(input())
xs = [int(x) for x in input().split()]
seen = {}
res = 0
while xs:
	j = xs.index(xs[0], 1)
	res += j - 1
	xs = xs[1:j] + xs[j + 1:]
print(res)


n = int(input())
a = list(map(int, input().split()))
ans = 0
b = [-1] * (2 * n)
x = 0
for i in range(2 * n):
	for j in range(i + 1, 2 * n):
		if a[i] == a[j]:
			b[i] = x
			b[j] = x
			x += 1
for i in range(2 * n):
	for j in range(1, 2 * n):
		if b[j] < b[j - 1]:
			(b[j - 1], b[j]) = (b[j], b[j - 1])
			ans += 1
print(ans)


def f(pairs_count: int, people: list):
	swap_count = 0
	if pairs_count < 1:
		return swap_count
	for left in range(0, pairs_count + pairs_count, 2):
		a = left + 1
		for a in range(left + 1, len(people)):
			if people[left] == people[a]:
				break
		for i in range(a, left + 1, -1):
			(people[i - 1], people[i]) = (people[i], people[i - 1])
			swap_count += 1
	return swap_count
n = int(input())
l = [int(i) for i in input().split()]
print(f(n, l))


n = int(input())
l = list(map(int, input().split()))
sumi = 0
while len(l) > 2:
	for j in range(1, len(l)):
		if l[0] == l[j]:
			sumi = j - 1 + sumi
			k = l.pop(j)
			l.pop(0)
			break
print(sumi)


pairs = int(input())
line = [int(x) for x in input().split()]

def solve(lst, count):
	if len(lst) == 2:
		return count
	if lst[0] == lst[1]:
		return solve(lst[2:], count)
	targ = lst[0]
	other = lst[1:].index(targ)
	lst.remove(targ)
	lst.remove(targ)
	return solve(lst, count + other)
print(solve(line, 0))


n = int(input())
a = [int(i) for i in input().split()]
dict = {}
temp = []
for i in range(len(a)):
	dict[a[i]] = i
for i in range(len(a)):
	temp.append(dict[a[i]])
count = 0
for i in range(len(a) - 1):
	for j in range(len(a) - i - 1):
		if temp[j] > temp[j + 1]:
			(temp[j], temp[j + 1]) = (temp[j + 1], temp[j])
			count += 1
print(count)


nops = int(input())
pers = [int(x) for x in input().split()]
ans = 0
while pers:
	i = pers.index(pers.pop(0))
	ans += i
	pers.pop(i)
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(0, 2 * n, 2):
	if a[i] == a[i + 1]:
		continue
	pos = a[i + 1:].index(a[i])
	ans += pos
	a = a[:i + 1] + a[i:i + 1] + a[i + 1:i + pos + 1] + a[i + pos + 2:]
print(ans)


n = int(input())
a = [int(v) for v in input().split()]
c = 0
for j in range(0, 2 * n, 2):
	for k in range(j + 1, 2 * n):
		if a[j] == a[k]:
			c = c + (k - j - 1)
			a[j + 2:k + 1] = a[j + 1:k]
			a[j + 1] = a[j]
print(c)


a = int(input())
b = list(map(int, input().split()))

def bestmove(c):
	d = 0
	e = c.copy()
	if len(c) == 0:
		return 0
	else:
		for i in range(1, len(c)):
			if e[i] == e[0]:
				d = i
				break
		del e[d]
		del e[0]
		return d - 1 + bestmove(e)
print(bestmove(b))


n = int(input())
a = [int(x) for x in input().split()]
(ind, value) = (0, 0)
num = n
while num > 0:
	x = a[ind]
	nextInd = -1
	for i in range(ind + 1, 2 * n):
		if x is a[i]:
			nextInd = i
			break
	for i in range(nextInd, ind + 1, -1):
		(a[i], a[i - 1]) = (a[i - 1], a[i])
		value += 1
	ind += 2
	num -= 1
print(value)


input()
a = list(input().split())
ans = 0
while len(a) > 0:
	b = a[1:].index(a[0]) + 1
	ans += b - 1
	a = a[1:b] + a[b + 1:]
print(ans)


n = int(input())
x = list(map(int, input().split()))
ind = 0
count = 0
while ind != 2 * n:
	if x[ind] != x[ind + 1]:
		q = ind + 1
		while x[q] != x[ind]:
			q += 1
		while q != ind + 1:
			(x[q], x[q - 1]) = (x[q - 1], x[q])
			q -= 1
			count += 1
	ind += 2
print(count)


from __future__ import print_function
import sys
import math
import os.path
import random
from queue import deque
sys.setrecursionlimit(10 ** 9)
(n,) = map(int, input().split())
a = deque(list(map(int, input().split())))
s = 0
for _ in range(n):
	x = a.popleft()
	ix = a.index(x)
	s += ix
	a.remove(x)
print(s)


n = int(input())
global line
line = [int(x) for x in input().split()]
global positions
positions = {}
for i in range(2 * n):
	if line[i] in positions:
		positions[line[i]].append(i)
	else:
		positions[line[i]] = [i]
global nbrSwap
nbrSwap = 0

def swap():
	global line
	global positions
	global nbrSwap
	global commut
	for i in range(2 * n - 1):
		if line[i] != line[i + 1]:
			if i == positions[line[i]][0]:
				if i + 1 == positions[line[i + 1]][1]:
					commut = 1
					positions[line[i]][0] = i + 1
					positions[line[i + 1]][1] = i
					c = line[i]
					line[i] = line[i + 1]
					line[i + 1] = c
					nbrSwap += 1
				elif positions[line[i]][1] > positions[line[i + 1]][1]:
					commut = 1
					positions[line[i]][0] = i + 1
					positions[line[i + 1]][0] = i
					c = line[i]
					line[i] = line[i + 1]
					line[i + 1] = c
					nbrSwap += 1
			elif i + 1 == positions[line[i + 1]][1]:
				if positions[line[i + 1]][0] < positions[line[i]][0]:
					commut = 1
					positions[line[i]][1] = i + 1
					positions[line[i + 1]][1] = i
					c = line[i]
					line[i] = line[i + 1]
					line[i + 1] = c
					nbrSwap += 1
global commut
commut = 1
while commut == 1:
	commut = 0
	swap()
print(nbrSwap)


n = int(input())
arr = list(map(int, input().split()))
count = 0
for ii in range(2 * n):
	i = arr[ii]
	x = arr.index(i)
	y = arr.index(i, x + 1)
	arr1 = arr[:x + 1]
	arr2 = []
	for z in arr[x + 1:]:
		if z != i:
			arr2.append(z)
	newarr = arr1 + [i] + arr2
	arr = newarr[:]
	count += abs(x - y) - 1
print(count)


n = int(input())
a = [int(i) for i in input().split()]
ans = 0
for i in range(0, n):
	s = a[0]
	del a[0]
	s = a.index(s)
	ans += s
	del a[s]
print(ans)


import heapq
n = int(input())
n *= 2
line = list(map(int, input().split()))
cnt = 0
while True:
	flag = False
	(L, R) = (-1, -1)
	i = 0
	while i < n - 1:
		if line[i] == line[i + 1]:
			i += 2
		else:
			L = i
			for j in range(i + 1, n):
				if line[j] == line[i]:
					R = j
					break
			flag = True
			break
	if not flag:
		break
	cnt += R - L - 1
	t = line[L]
	for i in range(R, L + 1, -1):
		line[i] = line[i - 1]
	line[L + 1] = t
print(cnt)


def solve(a):
	swaps = 0
	for i in range(0, len(a), 2):
		if a[i] != a[i + 1]:
			for j in range(len(a) - 1, i + 1, -1):
				if a[j] == a[i]:
					t = a[j]
					a[j] = a[j - 1]
					a[j - 1] = t
					swaps += 1
	return swaps
input()
inp = [int(i) for i in input().strip().split(' ')]
print(solve(inp))


import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
sys.setrecursionlimit(10000)

def read_int():
	return int(input())

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

def read_float():
	return float(input())

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

def read_str():
	return input()

def read_str_n():
	return list(map(str, input().split()))

def error_print(*args):
	print(*args, file=sys.stderr)

def mt(f):
	import time

	def wrap(*args, **kwargs):
		s = time.time()
		ret = f(*args, **kwargs)
		e = time.time()
		error_print(e - s, 'sec')
		return ret
	return wrap

@mt
def slv(N, E):
	ans = 0
	while E:
		t = E.pop(0)
		i = E.index(t)
		ans += i
		E.pop(i)
	return ans

def main():
	N = read_int()
	E = read_int_n()
	print(slv(N, E))
main()


R = lambda : list(map(int, input().split()))
m = int(input())
n = m * 2
l = R()
s = 0
i = 0
test = 0
while i < n:
	for j in range(i + 1, n):
		if l[j] == l[i]:
			if j - i - 1 > 0:
				s += j - i - 1
				l = l[0:i + 1] + [l[i]] + l[i + 1:j] + l[j + 1:]
				i += 2
				test = 1
			else:
				i += 2
				test = 1
			break
	if test == 0:
		i += 1
	else:
		test = 0
print(s)


n = int(input())
a = list(map(int, input().split()))
ans = 0
while len(a) > 0:
	c = a.pop(0)
	i = a.index(c)
	ans += i
	del a[i]
print(ans)


n = int(input())
t = list(map(int, input().split()))
sw = 0
while t != []:
	pr = 1 + t[1:].index(t[0])
	sw += pr - 1
	t = t[1:pr] + t[pr + 1:]
print(sw)


n = int(input())
a = list(map(int, input().split()))
cost = 0
for i in range(n):
	pos2 = a.index(a[2 * i], 2 * i + 1)
	for j in range(pos2, 2 * i, -1):
		a[j] = a[j - 1]
	cost += pos2 - (2 * i + 1)
print(cost)


import math
from collections import Counter
L = lambda : list(map(int, input().split()))
M = lambda : map(int, input().split())
I = lambda : int(input())
IN = lambda : input()

def s(a):
	print(''.join(list(map(str, a))))
n = I()
a = L()
n += n
c = 0
while n > 0:
	x = a[0]
	a.pop(0)
	i = a.index(x)
	c += i
	a.pop(i)
	n -= 2
print(c)


n = int(input())
a = [int(x) for x in input().split()]
ans = 0
for i in range(0, 2 * n, 2):
	for j in range(i + 2, 2 * n):
		if a[i] == a[j]:
			for k in range(j - 1, i, -1):
				t = a[k]
				a[k] = a[k + 1]
				a[k + 1] = t
				ans += 1
			break
print(ans)


n = int(input())
l = list(map(int, input().split()))
ma = 0
while len(l) > 0:
	k = l[1:].index(l[0]) + 1
	ma += k - 1
	l = l[1:k] + l[k + 1:]
print(ma)


n = input()
ar = list(map(int, input().strip().split()))
i = 0
while len(ar) > 0:
	k = ar[0]
	del ar[0]
	t = ar.index(k)
	i += t
	del ar[t]
print(i)


n = int(input())
a = [0] + list(map(int, input().split()))
l = [0] * (n + 1)
ans = 0
for i in range(1, 2 * n + 1):
	if not l[a[i]]:
		l[a[i]] = 1
		c = 0
		for j in range(i + 1, 2 * n + 1):
			if not l[a[j]]:
				c += 1
			elif a[i] == a[j]:
				ans += c
print(ans)


n = int(input())
a = list(map(int, input().split()))
(q, q1) = (0, 0)
while q < 2 * n - 1:
	if a[q] == a[q + 1]:
		q += 2
	else:
		for q2 in range(a[q + 1:].index(a[q]) + q + 1, q + 1, -1):
			(a[q2], a[q2 - 1]) = (a[q2 - 1], a[q2])
			q1 += 1
		q += 2
print(q1)


def check(arr):
	for i in range(0, 2 * n, 2):
		if arr[i] != arr[i + 1]:
			return 1
	return 0
n = int(input())
mov2 = [0] * (2 * n)
appear = [0] * (n + 1)
arr = list(map(int, input().split()))
for i in range(2 * n):
	if appear[arr[i]] == 0:
		mov2[i] = 1
		appear[arr[i]] = 1
	else:
		mov2[i] = -1
ans = 0
while check(arr):
	for i in range(1, 2 * n):
		if mov2[i] == -1 and arr[i] != arr[i - 1]:
			leftist = i
			break
	while arr[leftist - 1] != arr[leftist]:
		(arr[leftist], arr[leftist - 1]) = (arr[leftist - 1], arr[leftist])
		(mov2[leftist], mov2[leftist - 1]) = (mov2[leftist - 1], mov2[leftist])
		leftist -= 1
		ans += 1
print(ans)


from functools import cmp_to_key
n = int(input())
s = input()
s = s.split(' ')
ans = 0
for i in range(1, 2 * n, 2):
	k = i
	for j in range(i, 2 * n):
		if s[j] == s[i - 1]:
			k = j
			break
	for j in range(k - 1, i - 1, -1):
		(s[j], s[j + 1]) = (s[j + 1], s[j])
		ans += 1
print(ans)


n = int(input()) * 2
a = list(input().split())
used = [True] * n
res = 0
for i in range(n):
	if used[i]:
		last = a[i]
		for j in range(i + 1, n):
			if used[j]:
				if a[j] == last:
					used[j] = False
					break
				res += 1
print(res)


n = int(input())
a = [int(v) for v in input().split()]
ans = 0
for i in range(0, 2 * n, 2):
	if a[i] == a[i + 1]:
		continue
	else:
		pair_idx = a[i + 2:].index(a[i]) + (i + 2)
		ans += pair_idx - (i + 1)
		a = a[:i + 1] + [a[i]] + a[i + 1:pair_idx] + a[pair_idx + 1:]
print(ans)


n = int(input())
a = list(map(int, input().split()))
count = 0
for i in range(0, 2 * n - 1):
	for j in range(i, 2 * n):
		if a[i] == a[j] and j == i + 1:
			break
		if a[i] == a[j] and i != j:
			for k in range(j, i + 1, -1):
				a[k] = a[k - 1]
				count += 1
			a[i + 1] = a[i]
			break
print(count)


k = input()
a = [int(x) for x in input().split()]
c = 0
while a:
	f = a.pop(0)
	i = a.index(f)
	c += i
	del a[i]
print(c)


n = int(input())
couples = input().split(' ')

def reorder(couples, i):
	global n
	first = couples[i]
	j = 2 * n - 1
	while couples[j] != first:
		j -= 1
	count = 0
	while j - 1 != i:
		count += 1
		couples[j] = couples[j - 1]
		j -= 1
	couples[i + 1] = first
	return (couples, count)
i = 0
total = 0
while i < 2 * n - 1:
	if couples[i] == couples[i + 1]:
		i += 2
	else:
		(couples, count) = reorder(couples, i)
		total += count
		i += 2
print(total)


import itertools as it
import sys

def bad(x):
	r = 0
	changed = True
	while changed:
		changed = False
		for i in range(len(x)):
			if changed:
				break
			first = i
			for j in range(i + 1, len(x)):
				if x[i] == x[j]:
					if i + 1 == j:
						break
					r += j + 1 - (i + 2)
					for k in range(i + 1, j)[::-1]:
						(x[k], x[k + 1]) = (x[k + 1], x[k])
					changed = True
					break
	return r
R = sys.stdin.read().split(' ')
Z = []
for u in R:
	u = u.strip()
	Z += u.split('\n')
Z = [int(i.strip()) for i in Z if len(i)]
N = Z[0]
print(bad(Z[1:2 * N + 1]))


n = int(input())
l = list(map(int, input().split()))
ans = 0
m = []
for i in range(2 * n - 1, -1, -1):
	if l[i] not in m:
		m.append(l[i])
for tt in range(0, n):
	i = m[tt]
	j = l.index(i)
	l.pop(j)
	k = l.index(i)
	l.insert(k, j)
	ans += k - j
print(ans)


n = int(input())
a = [*map(int, input().split())]
c = 0
for i in range(1, n):
	j = a.index(a[i - 1], i)
	c += j - i
	del a[j]
print(c)


n = int(input())
L = list(map(int, input().split()))
ans = 0
while len(L) > 0:
	now = L.pop(0)
	ans += L.index(now)
	L.remove(now)
print(ans)


n = int(input())
l = list(map(int, input().split()))
k = [-1] * (2 * n)
cnt = 0
for i in range(2 * n):
	if k[l[i]] == -1:
		k[l[i]] = i
	else:
		p = l[0:k[l[i]] + 1]
		for j in range(k[l[i]] + 1, i):
			if l[j] in p:
				cnt
			else:
				cnt += 1
print(cnt)


n = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))
m = [None] * n
c = 0
for a in A:
	if m[a] is None:
		m[a] = c
		c += 1
A[:] = map(lambda i: m[i], A)

def count(L):
	inv_count = 0
	for i in range(len(L)):
		for j in range(i + 1, len(L)):
			if L[i] > L[j]:
				inv_count += 1
	return inv_count
print(count(A))


n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
	m = a[2 * i]
	for j in range(2 * i + 1, 2 * n):
		if a[j] == m:
			break
	del a[j]
	a.insert(2 * i, m)
	ans += j - 2 * i - 1
print(ans)


def move(a, i, j):
	f = a[i]
	for g in range(i, j, -1):
		a[g] = a[g - 1]
	a[j] = f
n = int(input())
a = list(map(int, input().split()))
c = 0
for i in range(0, 2 * n, 2):
	if a[i] != a[i + 1]:
		f = a[i + 1:].index(a[i]) + i + 1
		move(a, f, i + 1)
		c += f - i - 1
print(c)


n = int(input())
arr = list(map(int, input().split()))
ans = 0
while arr != []:
	x = arr[0]
	cnt = 0
	for i in range(1, len(arr)):
		if arr[i] == x:
			break
		else:
			cnt += 1
	ans += cnt
	temp = []
	for i in range(len(arr)):
		if arr[i] != x:
			temp.append(arr[i])
	arr = temp[:]
print(ans)


def findMinimumAdjacentSwaps(arr, N):
	visited = [False] * (N + 1)
	minimumSwaps = 0
	for i in range(2 * N):
		if visited[arr[i]] == False:
			visited[arr[i]] = True
			count = 0
			for j in range(i + 1, 2 * N):
				if visited[arr[j]] == False:
					count += 1
				elif arr[i] == arr[j]:
					minimumSwaps += count
	return minimumSwaps
n = int(input())
a = list(map(int, input().split()))
ans = findMinimumAdjacentSwaps(a, n)
print(ans)


n = int(input())
n1 = 2 * n
a = [int(x) for x in input().split()]
count1 = 0
ind = 0
while n > 0:
	n -= 1
	pos = -1
	curr = a[ind]
	for i in range(ind + 1, n1):
		if curr == a[i]:
			pos = i
			break
	for i in range(pos, ind + 1, -1):
		x = a[i]
		a[i] = a[i - 1]
		a[i - 1] = x
		count1 += 1
	ind += 2
print(count1)


def inp():
	return list(map(int, input().split()))
(n,) = inp()
l = inp()
ans = 0
for i in range(n - 1):
	x = l.pop(0)
	ans += l.index(x)
	l.remove(x)
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
while a:
	x = a.index(a.pop(0))
	ans = ans + x
	del a[x]
print(ans)


n = int(input())
line = [int(x) for x in input().split()]
ans = 0
while len(line) > 0:
	a = line.pop(0)
	ans += line.index(a)
	line.remove(a)
print(ans)


n = int(input())
num = list(map(int, input().split()))
ans = 0
while True:
	fl = False
	i = 0
	while i < n * 2 - 1:
		if num[i] != num[i + 1]:
			fl = True
			for j in range(i + 1, n * 2):
				if num[j] == num[i]:
					pos = j
					break
			for j in range(pos, i + 1, -1):
				(num[j], num[j - 1]) = (num[j - 1], num[j])
				ans += 1
			i += 2
		else:
			i += 2
	if not fl:
		break
print(ans)


n = int(input())
tab = [int(x) for x in input().split()]
result = 0
for i in range(0, 2 * n, 2):
	a = tab[i]
	b_i = tab[i + 1:].index(a) + i + 1
	if b_i == i + 1:
		continue
	result += b_i - i - 1
	tab = tab[:i + 1] + [a] + tab[i + 1:b_i] + tab[b_i + 1:]
print(result)


n = int(input())
arr = list(map(int, input().split()))
count = 0
for i in range(2 * n):
	if arr[i] == 0:
		continue
	for j in range(i + 1, 2 * n):
		if arr[i] == arr[j]:
			arr[j] = 0
			break
		count += arr[j] > 0
print(count)


input()
a = input().split()
ans = 0
while len(a) > 0:
	at = a[1:].index(a[0]) + 1
	ans += at - 1
	a = a[1:at] + a[at + 1:]
print(ans)


n = int(input())
a = input().split()
for i in range(0, n + n):
	a[i] = int(a[i])
ans = 0
for i in range(0, 2 * n - 1, 2):
	x = a[i]
	if a[i] == a[i + 1]:
		continue
	idx = a.index(a[i], i + 1)
	ans += idx - i - 1
	del a[idx]
	a.insert(i + 1, a[i])
print(ans)


def add(i):
	global t
	i += 1
	while i <= n * 2:
		t[i] += 1
		i += i & -i

def qry(i):
	global t
	s = 0
	while i:
		s += t[i]
		i -= i & -i
	return s
n = int(input())
a = list(map(int, input().strip().split(' ')))
b = [0 for i in range(2 * n + 2)]
t = b.copy()
for i in range(2 * n):
	b[a[i]] = i
s = 0
for i in range(2 * n):
	if b[a[i]] > i:
		s += b[a[i]] - i - 1 - qry(b[a[i]]) + qry(i)
		add(b[a[i]])
print(s)


a = input()
k = list(map(int, input().split()))
total = 0
while k:
	i = k.index(k.pop(0))
	total = total + i
	k.pop(i)
print(total)


import sys
n = int(input())
n *= 2
a = list(map(int, input().split()))
res = 0
for i in range(0, n, 2):
	if a[i] != a[i + 1]:
		t = a[i]
		for j in range(i + 1, n):
			if a[j] == t:
				for k in range(j, i + 1, -1):
					(a[k], a[k - 1]) = (a[k - 1], a[k])
					res += 1
				break
print(res)


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

def swap(a, p, q):
	(a[p], a[q]) = (a[q], a[p])
ans = 0
p = 0
while p < 2 * n:
	for i in range(p + 1, 2 * n):
		if a[i] == a[p]:
			for j in range(i, p + 1, -1):
				swap(a, j, j - 1)
				ans += 1
			break
	p += 2
print(ans)


def count_inversions(lst):
	return merge_count_inversion(lst)[1]

def merge_count_inversion(lst):
	if len(lst) <= 1:
		return (lst, 0)
	middle = int(len(lst) / 2)
	(left, a) = merge_count_inversion(lst[:middle])
	(right, b) = merge_count_inversion(lst[middle:])
	(result, c) = merge_count_split_inversion(left, right)
	return (result, a + b + c)

def merge_count_split_inversion(left, right):
	result = []
	count = 0
	(i, j) = (0, 0)
	left_len = len(left)
	while i < left_len and j < len(right):
		if left[i] <= right[j]:
			result.append(left[i])
			i += 1
		else:
			result.append(right[j])
			count += left_len - i
			j += 1
	result += left[i:]
	result += right[j:]
	return (result, count)

def f(n, nums):
	sset = dict()
	nums2 = []
	k = 1
	for i in nums:
		if i in sset:
			nums2.append(sset[i])
		else:
			nums2.append(k)
			sset[i] = k
			k += 1
	return count_inversions(nums2)
n = int(input())
queues = [int(i) for i in input().split()]
print(f(n, queues))


def main():
	n = int(input())
	a = list(map(int, input().split()))
	ret = 0
	while len(a) > 2:
		p = a[0]
		a = a[1:]
		c = a.index(p)
		ret += c
		a.pop(c)
	print(ret)
	return 0
main()


n = int(input())
ckn = list(map(int, input().split()))
res = 0
for p in range(n):
	f = ckn.pop(0)
	g_ind = ckn.index(f)
	res += g_ind
	ckn.pop(g_ind)
print(res)


n = int(input())
a = list(map(int, input().split()))
c = 0
while len(a) != 0:
	z = a[0]
	b = a.index(z)
	d = a[1:].index(z) + 1
	c += d - b - 1
	a.remove(z)
	a.remove(z)
print(c)


import sys
input = sys.stdin.readline
MOD = 1000000007
MOD2 = 998244353
ii = lambda : int(input().strip('\n'))
si = lambda : input().strip('\n')
dgl = lambda : list(map(int, input().strip('\n')))
f = lambda : map(int, input().strip('\n').split())
il = lambda : list(map(int, input().strip('\n').split()))
ls = lambda : list(input().strip('\n'))
let = 'abcdefghijklmnopqrstuvwxyz'
n = ii()
l = il()
ans = 0
while len(l) > 0:
	x = l[0]
	l.pop(0)
	for j in range(len(l)):
		if x == l[j]:
			ans += j
			l.pop(j)
			break
print(ans)


n = int(input())
l = [int(x) for x in input().split()]
ans = 0
while len(l) > 0:
	a = l[0]
	l = l[1:]
	ans += l.index(a)
	l.remove(a)
print(ans)


n = int(input())
lst = list(map(int, input().split(' ')))
spots = [[-1, -1] for i in range(n)]
swaps = 0
newlst = lst
while newlst != []:
	fst = newlst[0]
	templst = []
	for i in range(1, len(newlst)):
		if newlst[i] != fst:
			templst.append(newlst[i])
		else:
			swaps += i - 1
	newlst = templst
print(swaps)


n = int(input())
l = [int(i) for i in input().split()]
ans = 0
while len(l) > 0:
	a = l.pop(0)
	ans += l.index(a)
	l.remove(a)
print(ans)


p = int(input())
seq = list(map(int, input().split()))
total = 0
to_check = set((x + 1 for x in range(p)))
ind = 0
checked = [False for _ in range(p * 2)]
while to_check:
	current = seq[ind]
	checked[ind] = True
	for x in range(ind + 1, p * 2):
		if seq[x] == current:
			checked[x] = True
			break
		elif not checked[x]:
			total += 1
	to_check.remove(current)
	for other in range(ind + 1, p * 2):
		if not checked[other]:
			ind = other
			break
print(total)


n = int(input())
a = list(map(int, input().split()))
t = 0
for i in range(0, 2 * n, 2):
	x = a[i]
	y = a[i + 1]
	if x != y:
		for j in range(i + 1, 2 * n):
			if a[j] == x:
				break
		a = a[:i + 1] + [x] + a[i + 1:j] + a[j + 1:2 * n]
		t += j - i - 1
print(t)


n = int(input())
v = list(map(int, input().split()))
ans = 0
pos = [[j for j in range(2 * n) if v[j] == i + 1] for i in range(n)]
for p in range(len(v)):
	i = v[p] - 1
	if pos[i][0] + 1 == pos[i][1]:
		continue
	for j in range(len(pos)):
		if j == i:
			continue
		if pos[j][0] > pos[i][0] and pos[j][0] <= pos[i][1]:
			pos[j][0] += 1
		if pos[j][1] > pos[i][0] and pos[j][1] <= pos[i][1]:
			pos[j][1] += 1
	ans += pos[i][1] - pos[i][0] - 1
	pos[i][1] = pos[i][0] + 1
print(ans)


n = int(input())
arr = list(map(int, input().strip().split(' ')))
swap = 0
i = 0
while i < 2 * n:
	if arr[i] != arr[i + 1]:
		cnt = 0
		for j in range(2 * n):
			if arr[i] == arr[j]:
				cnt += 1
			if cnt == 2:
				break
		swap += j - i - 1
		key = arr[i + 1]
		while j > i + 2:
			arr[j] = arr[j - 1]
			j -= 1
		arr[i + 2] = key
		arr[i + 1] = arr[i]
	i += 2
print(swap)


n = int(input())
arr = list(map(int, input().split()))
dict1 = {}
for i in range(2 * n):
	try:
		dict1[arr[i]].append(i + 1)
	except:
		KeyError
		dict1[arr[i]] = [i + 1]
ans = 0
for i in range(0, 2 * n, 2):
	if dict1[arr[i]][1] == i + 1:
		j = dict1[arr[i]][0] - 1
		while j > dict1[arr[i]][1]:
			if dict1[arr[j - 1]][0] == j:
				dict1[arr[j - 1]][0] = j + 1
			else:
				dict1[arr[j - 1]][1] = j + 1
			dict1[arr[i]][0] -= 1
			temp = arr[j]
			arr[j] = arr[j - 1]
			arr[j - 1] = temp
			j -= 1
			ans += 1
	else:
		j = dict1[arr[i]][1] - 1
		while j > dict1[arr[i]][0]:
			if dict1[arr[j - 1]][0] == j:
				dict1[arr[j - 1]][0] = j + 1
			else:
				dict1[arr[j - 1]][1] = j + 1
			dict1[arr[i]][1] -= 1
			temp = arr[j]
			arr[j] = arr[j - 1]
			arr[j - 1] = temp
			j -= 1
			ans += 1
print(ans)


import itertools
import sys
n = int(input())
people = list(input().split())
interchanges = 0
for i in range(2 * n):
	if people[i] != 0:
		j = i + 1
		while j <= 2 * n:
			if people[i] == people[j]:
				people[j] = 0
				break
			if people[j] != 0:
				interchanges = interchanges + 1
			j = j + 1
print(interchanges)


n = int(input())
a = [int(i) for i in input().split()]
s = 0
for i in range(0, len(a), 2):
	if a[i] != a[i + 1]:
		rf = 2 * n - 1 - a[i:][::-1].index(a[i])
		s += abs(rf - (i + 1))
		a = a[:i + 1] + [a[rf]] + a[i + 1:rf] + a[rf + 1:]
print(s)


import sys
from collections import Counter

def main():
	n = int(sys.stdin.readline())
	a = list(map(int, sys.stdin.readline().split()))
	couple = [[] for _ in range(n)]
	for i in range(2 * n):
		couple[a[i] - 1].append(i)
	tmp = []
	for (a, b) in couple:
		if a - b < -1:
			tmp.append([a, b])
		elif a - b > 1:
			tmp.append([b, a])
	res = 0
	tmp.sort()
	for i in range(len(tmp)):
		(a, b) = tmp[i]
		res += b - a - 1
		for j in range(i + 1, len(tmp)):
			for k in range(2):
				tmp[j][k] += 1 if a < tmp[j][k] < b else 0
	print(res)
main()


n = int(input())
a = list(map(lambda x: int(x), input().split()))
mask = [0] * 2 * n
swaps = 0
for i in range(2 * n - 1):
	if mask[i] == 1:
		next
	if a[i] != a[i + 1]:
		m = 0
		for j in range(i + 1, 2 * n):
			if mask[j] == 1:
				m += 1
			if a[j] == a[i]:
				mask[j] = 1
				swaps += j - i - m - 1
				break
	else:
		mask[i + 1] = 1
	mask[i] = 1
print(swaps)


f = lambda : list(map(int, input().split()))
n = int(input())
a = f()
k = 0
while a:
	i = a.index(a.pop(0))
	k += i
	del a[i]
print(k)


n = int(input())
l = list(map(int, input().split()))
d = {}

def change(w, i, val):
	if d[w][0] == i:
		d[w][0] += val
		return
	d[w][1] += val

def isdone():
	for (j, i) in enumerate(l):
		if i in d:
			d[i].append(j)
		else:
			d[i] = [j]
isdone()
i = 0
m = 0
while True:
	if i >= 2 * n:
		break
	if l[i] != l[i + 1]:
		ind = max(d[l[i]])
		for j in range(ind, i + 1, -1):
			(l[j], l[j - 1]) = (l[j - 1], l[j])
			d = {}
			isdone()
			m += 1
	i += 2
print(m)


input()
p = list(map(int, input().split()))
d = {}
spot = {}
tot = 0
for k in range(len(p)):
	if p[k] not in d.keys():
		d[p[k]] = k
	else:
		spot[p[k]] = k
		d[p[k]] = k - d[p[k]]
for k in range(len(p)):
	tot += d[p[k]] - 1
	sp = spot[p[k]]
	for i in range(k, sp):
		if spot[p[i]] > sp:
			d[p[i]] -= 1
	d[p[k]] = 1
print(tot)


def solve(store, list, index, val):
	loc_count = 0
	while a[index - 1] != val:
		if store[a[index - 1]] != index - 1:
			store[a[index - 1]] = index
		(a[index - 1], a[index]) = (a[index], a[index - 1])
		loc_count += 1
		index -= 1
	return loc_count
(n, count) = (int(input()), 0)
store = {}
a = list(map(int, input().strip().split()))
for (i, item) in enumerate(a):
	if item not in store:
		store[item] = i
	elif store[item] != i:
		count += solve(store, a, i, item)
print(count)


n = int(input())
l = list(map(int, input().split()))
ans = 0
while len(l) > 0:
	p = l.pop(0)
	ans += l.index(p)
	l.pop(l.index(p))
print(ans)