#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int p[123], oc[123];

int main() {
	ios::sync_with_stdio(0);

	int n; cin >> n;

	n <<= 1;

	for (int i = 0; i < n; ++i) {
		cin >> p[i];
		oc[p[i]] = 0;
	}

	int ans = 0;

	for (int i = 0; i < n; ++i) {
		if (!oc[p[i]]) {
			int idx = 0;
			for (int j = i + 1; j < n; ++j) {
				if (p[j] == p[i]) {
					idx = j;
					break;
				}
			}
			while (idx != i + 1) {
				swap(p[idx], p[idx - 1]);
				--idx;
				++ans;
			}
			oc[p[i]] = 1;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int p[123], oc[123];

int main() {
	ios::sync_with_stdio(0);

	int n; cin >> n;

	n <<= 1;

	for (int i = 0; i < n; ++i) {
		cin >> p[i];
	}

	int ans = 0;

	for (int i = 0; i < n; ++i) {
		if (!oc[p[i]]) {
			int idx = 0;
			for (int j = i + 1; j < n; ++j) {
				if (p[j] == p[i]) {
					idx = j;
					break;
				}
			}
			while (idx != i + 1) {
				swap(p[idx], p[idx - 1]);
				--idx;
				++ans;
			}
			oc[p[i]] = 1;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int p[234], oc[123];

int main() {
	ios::sync_with_stdio(0);

	int n; cin >> n;

	n <<= 1;

	for (int i = 0; i < n; ++i) {
		cin >> p[i];
		oc[p[i]] = 0;
	}

	int ans = 0;

	for (int i = 0; i < n; ++i) {
		if (!oc[p[i]]) {
			int idx = 0;
			for (int j = i + 1; j < n; ++j) {
				if (p[j] == p[i]) {
					idx = j;
					break;
				}
			}
			while (idx != i + 1) {
				swap(p[idx], p[idx - 1]);
				--idx;
				++ans;
			}
			oc[p[i]] = 1;
		}
	}

	cout << ans << endl;

	return 0;
}


#include <cstring>
#include <iostream>
#include <algorithm>

int a[205]; // 输入
int b[105]; // 第一次出现位置

int main()
{
	memset(b, -1, sizeof b);
	int n;
	std::cin >> n;
	for (int i = 0; i < (n << 1); ++i)
	{
		std::cin >> a[i];
		if (b[a[i]] == -1)
			b[a[i]] = i;
	}
	int ans = 0;
	for (int i = 0; i < (n << 1); ++i)
		for (int j = 0; j < i; ++j)
			if (b[a[i]] < b[a[j]])
				++ans;
	std::cout << ans << std::endl;
}


#include <iostream>
#include <algorithm>

int n;

int bit[105];

struct Node
{
	int val;
	int id;
} a[105];

int lowbit(int x)
{
	return x & -x;
}

void update(int pos, int val)
{
	while (pos <= n)
	{
		bit[pos] += val;
		pos += lowbit(pos);
	}
}

int query(int pos)
{
	int res = 0;
	while (pos)
	{
		res += bit[pos];
		pos -= lowbit(pos);
	}
	return res;
}

int main()
{
	std::cin >> n;
	for (int i = 0; i < n; ++i)
		std::cin >> a[i].val >> a[i].id;
	std::sort(a, a + n, [](const Node &n1, const Node &n2)
	{
		return n1.val > n2.val;
	});
	int ans = 0;
	for (int i = 0; i < n; ++i)
	{
		update(a[i].id, 1);
		ans += query(a[i].id - 1);
	}
	std::cout << (ans ? ans + 1 : ans) << std::endl;
}


#include <iostream>
#include <algorithm>
#define dbg(x) std::clog << (x) << std::endl;

int n;

int bit[105];

struct Node
{
	int val;
	int id;
} a[105];

int lowbit(int x)
{
	return x & -x;
}

void update(int pos, int val)
{
	while (pos < n)
	{
		bit[pos] += val;
		pos += lowbit(pos);
	}
}

int query(int pos)
{
	int res = 0;
	//dbg(pos) ;
	//--pos;
	while (pos > 0)
	{
		//dbg(pos);
		res += bit[pos];
		pos -= lowbit(pos);
	}
	return res;
}

int main()
{
	std::cin >> n;
	for (int i = 0; i < n; ++i)
		std::cin >> a[i].val >> a[i].id;
	std::sort(a, a + n, [](const Node &n1, const Node &n2)
	{
		return n1.val > n2.val;
	});
	int ans = 0;
	for (int i = 0; i < n; ++i)
	{
		update(a[i].id, 1);
		ans += query(a[i].id - 1);
	}
	std::cout << (ans ? ans + 1 : ans) << std::endl;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <list>
#define INF 0x3f3f3f3f
#define maxn 105000
#define maxnn 6000
#define juzheng 300
#define line cout << "-------------------------" << endl;
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define fill_(a,b,n) fill(a,a + n,b)
#define esp 1e-9

#define ri(n) scanf("%d",&n)
#define ri2(a,b) scanf("%d %d",&a,&b)
#define ri3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define rd(n) scanf("%lf",&n)
#define rd2(a,b) scanf("%lf %lf",&a,&b)
#define rd3(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define rl(n) scanf("%lld",&n)
#define rl2(a,b) scanf("%lld %lld",&a,&b)
#define rl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define rui(n) scanf("%u",&n)
#define rui2(a,b) scanf("%u %u",&a,&b)
#define rui3(a,b,c) scanf("%u %u %u",&a,&b,&c)
#define rs(str) scanf("%s",str)
#define pr(n) cout << n << endl
#define debug(str,x) cout << str << ":" << x << endl
#define ll long long
#define int64 __int64
#define ui unsigned int

using namespace std;

const ll mod = 1e9 + 7;

//Date:2018-9-05
//Author：HarryBlackCat

struct xx{
	int l,r,val;
}meb[maxn];

int n,arr[maxn];
bool mark[maxn];

void init(){
	for(int i = 0;i < maxn;i++){
		meb[i].l = meb[i].r = -1;
		meb[i].val = i;
	}
	mem(mark,false);
}

int main() {
	//cin.sync_with_stdio(false);//降低cin，cout时间
	while(~ri(n)){
		init();

		for(int i = 0;i < 2 * n;i++){
			ri(arr[i]);
			if(meb[arr[i]].l == -1){
				meb[arr[i]].l = i;
			}
			else{
				meb[arr[i]].r = i;
				if(meb[arr[i]].r - meb[arr[i]].l == 1)
					mark[arr[i]] = true;
			}
		}

//		for(int i = 1;i <= n;i++){
//			printf("i:%d l:%d r:%d\n",i,meb[i].l,meb[i].r);
//		}

		int s = INF,e = -INF;
		for(int i = 1;i <= n;i++){
			if(!mark[meb[i].val]){
				s = min(s,meb[i].l);
				e = max(e,meb[i].r);
			}
		}

//		printf("s:%d e:%d\n",s,e);
//		for(int i = s;i <= e;i++){
//			printf("%d ",arr[i]);
//		}
//		puts("");

		int ans = 0;
		for(int i = s;i <= e;i++){
			int now = arr[i];
			if(mark[now])
				continue;
			set<int> s;
			ans += meb[now].r - meb[now].l - 1;
			mark[now] = true;
			for(int j = i + 1;j < meb[now].r;j++){
				if(mark[arr[j]])
					continue;
				if(!s.count(arr[j])){
					meb[arr[j]].l++;
					s.insert(arr[j]);
				}
				else{
					meb[arr[j]].r++;
				}
			}
		}

		pr(ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <list>
#define INF 0x3f3f3f3f
#define maxn 105000
#define maxnn 6000
#define juzheng 300
#define line cout << "-------------------------" << endl;
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define fill_(a,b,n) fill(a,a + n,b)
#define esp 1e-9

#define ri(n) scanf("%d",&n)
#define ri2(a,b) scanf("%d %d",&a,&b)
#define ri3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define rd(n) scanf("%lf",&n)
#define rd2(a,b) scanf("%lf %lf",&a,&b)
#define rd3(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define rl(n) scanf("%lld",&n)
#define rl2(a,b) scanf("%lld %lld",&a,&b)
#define rl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define rui(n) scanf("%u",&n)
#define rui2(a,b) scanf("%u %u",&a,&b)
#define rui3(a,b,c) scanf("%u %u %u",&a,&b,&c)
#define rs(str) scanf("%s",str)
#define pr(n) cout << n << endl
#define debug(str,x) cout << str << ":" << x << endl
#define ll long long
#define int64 __int64
#define ui unsigned int

using namespace std;

const ll mod = 1e9 + 7;

//Date:2018-9-05
//Author：HarryBlackCat

int arr[maxn],n;

int main() {
	//cin.sync_with_stdio(false);//降低cin，cout时间
	while(~ri(n)){
		for(int i = 0;i < 2 * n;i++)
			ri(arr[i]);

		int ans = 0;
		for(int i = 0;i < 2 * n;i++){
			if(arr[i]){
				for(int j = i + 1;arr[i] < 2 * n;j++){
					if(arr[j] == arr[i]){
						arr[j] = 0;
						break;
					}
					ans += (arr[j] > 0);
				}
			}
		}

		pr(ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int inf = 0x3f3f3f3f;
// head
int n,a[101];
int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
//    #ifdef LOCAL
//  freopen("data.in", "r", stdin);
  //freopen("data.out", "w", stdout);
//  #endif
    //scanf("%d",&a);
#ifdef LOCAL
    while(1){
//测试的时候请对重复使用的数组或者变量清零！！
//    k=0;
//    memset(a,0,sizeof(a));
//  以及后文中该特判return的地方要及时return！
#endif
    cin>>n;
    n*=2;
    rep(i,0,n){
        cin>>a[i];
    }
    int ans=0;
    for(int i=0;i<n;i+=2){
        for(int j=i+1;j<n;j++){
            if(a[i]==a[j]&&j==i+1) break;
            else if(a[i]==a[j]){
                for(int k=j;k>i+1;k--){
                    int tem=a[k];
                    a[k]=a[k-1];
                    a[k-1]=tem;
                }
                ans+=j-i-1;
            }
            else continue;
        }
    }
    cout<<ans<<endl;

#ifdef LOCAL
    }
#endif
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int inf = 0x3f3f3f3f;
// head
int n,a[201];

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
//    #ifdef LOCAL
//  freopen("data.in", "r", stdin);
  //freopen("data.out", "w", stdout);
//  #endif
    //scanf("%d",&a);
#ifdef LOCAL
    while(1){
//测试的时候请对重复使用的数组或者变量清零！！
//    k=0;
//    memset(a,0,sizeof(a));
//  以及后文中该特判return的地方要及时return！
#endif
    cin>>n;
    n*=2;
    rep(i,0,n){
        cin>>a[i];
    }
    int ans=0;
    for(int i=0;i<n;i+=2){
        for(int j=i+1;j<n;j++){
            if(a[i]==a[j]&&j==i+1) break;
            else if(a[i]==a[j]){
                for(int k=j;k>i+1;k--){
                    int tem=a[k];
                    a[k]=a[k-1];
                    a[k-1]=tem;
                }
                ans+=j-i-1;
            }
            else continue;
        }
    }
    cout<<ans<<endl;

#ifdef LOCAL
    }
#endif
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 1005
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
int f[MAXN],a[MAXN];
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    rep(i,1,n*2)
    scanf("%d",&a[i]);
    rep(i,1,n*2)
    {
        if(f[a[i]])
        {
            sum=sum+(i-f[a[i]]-1);
            per(j,i-1,f[a[i]]+1)
            swap(a[j],a[j+1]);
        }
        else
            f[a[i]]=i;
    }
    printf("%d\n",sum);
}


#include<bits/stdc++.h>
using namespace std;
#define MAXN 1005
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
int f[MAXN],a[MAXN];
int main()
{
    int n,sum=0;
    scanf("%d",&n);
    rep(i,1,n*2)
    scanf("%d",&a[i]);
    rep(i,1,n*2)
    {
        if(f[a[i]])
        {
//            printf("%d %d\n",a[i],f[a[i]]);
            sum=sum+(i-f[a[i]]-1);
            int z=f[a[i]];
            per(j,i-1,z+1)
            {
//                printf("%d %d++\n",a[j],j+1);
                f[a[j]]=j+1;
                swap(a[j],a[j+1]);
            }
        }
        else
            f[a[i]]=i;
    }
    printf("%d\n",sum);
}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second

using namespace std;

const double pi=acos(-1);
const int N=(int)1e6+7,mod=1000000007,M=2e9;

//scanf("%d",&);
int n,a[107],c;

void handle(int st,int en){
    for(int i=en;i>st+1;i--)swap(a[i],a[i-1]);
}

int main()
{
//    freopen(".txt","r",stdin);
    cin>>n;
    n*=2;
    for(int i=0;i<n;i++)cin>>a[i];
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]==a[j])c+=j-i-1,handle(i,j);
        }
    }
    cout<<c;
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second

using namespace std;

const double pi=acos(-1);
const int N=(int)1e6+7,mod=1000000007,M=2e9;

//scanf("%d",&);
int n,a[300],b[300],c,s;

void handle(int st,int en,bool x){
    if(x){
        for(int i=en;i>st+1;i--)swap(a[i],a[i-1]);
    }
    else{
        for(int i=en;i>st+1;i--)swap(b[i],b[i-1]);
    }
}

int main()
{
//    freopen(".txt","r",stdin);
    cin>>n;
    n*=2;
    for(int i=0;i<n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    reverse(b,b+n);
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]==a[j])c+=j-i-1,handle(i,j,1);
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(b[i]==b[j])s+=j-i-1,handle(i,j,0);
        }
    }
    cout<<min(c,s);
    return 0;
}


#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define ff first
#define ss second

using namespace std;

const double pi=acos(-1);
const int N=(int)1e6+7,mod=1000000007,M=2e9;

//scanf("%d",&);
int n,a[107],b[107],c,s;

void handle(int st,int en,bool x){
    if(x){
        for(int i=en;i>st+1;i--)swap(a[i],a[i-1]);
    }
    else{
        for(int i=en;i>st+1;i--)swap(b[i],b[i-1]);
    }
}

int main()
{
//    freopen(".txt","r",stdin);
    cin>>n;
    n*=2;
    for(int i=0;i<n;i++){
        cin>>a[i];
        b[i]=a[i];
    }
    reverse(b,b+n);
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]==a[j])c+=j-i-1,handle(i,j,1);
        }
    }
    for(int i=0;i<n;i++){
        for(int j=i+1;j<n;j++){
            if(b[i]==b[j])s+=j-i-1,handle(i,j,0);
        }
    }
    cout<<min(c,s);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> people;
    for (int i = 0; i < 2* n; i++){
        int x; cin >> x;
        people.push_back(x);
    }
    int count = 0;
    for (int i = 0; i < 2*n; i+=2){
        int start = i;
        int end;
        for (int j = i+1; j < 2*n; j++){
            if (people[j] == people[i]){
                end = j;
                break;
            }
        }
        for (int j = end; j > i + 1; j--){
            swap(people[j], people[j-1]);
            count++;
        }
    }
    cout << count << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

int main(){
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> people;
    for (int i = 0; i < 2* n; i++){
        int x; cin >> x;
        people.push_back(x);
    }
    int count = 0;
    for (int i = 0; i < 2*n; i+=2){
        int start = i;
        int end;
        for (int j = 0; j < n; j++){
            if (people[j] == people[i]){
                end = j;
            }
        }
        for (int j = end; end > i + 1; end--){
            swap(people[j], people[j-1]);
            count++;
        }
    }
    cout << count << endl;
}


#include <bits/stdc++.h>
using namespace std;

int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    cerr << 1;
    vector<int> a(n * 2), b(n + 1);
    for (int i = 0; i < n * 2; ++i) {
        cin >> a[i];
        b[a[i]] = i;
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        if (b[a[i]] == i + 1 || b[a[i]] == i) continue;
        while (b[a[i]] != i + 1) {
            swap(a[b[a[i]]], a[b[a[i]] - 1]);
            --b[a[i]];
            ++ans;
        }
    }
    cout << ans;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    vector<int> a(n * 2), b(n);
    for (int i = 0; i < n * 2; ++i) {
        cin >> a[i];
        --a[i];
        b[a[i]] = i;
    }
    //for (int i = 0; i < n * 2; ++i) {
    //    if (b[a[i]] == i) continue;
    //    b[a[i]] = i;
    //    a[i] = i;
   // }
    //for (const auto &j : a) cout << j << " " << b[j] << endl;
    //    cout << endl;
    int ans = 0;
    for (int i = 1; i < n * 2; ++i) {
        //while ()
        if (b[a[i]] == i + 1 || b[a[i]] == i) continue;
        for (int j = i + 1; j < b[a[i]]; ++j) {
            if (b[a[j]] == j) ++b[a[j]];
        }
        ans += (b[a[i]] - i - 1);
        a.erase(a.begin() + b[a[i]]);
        b[a[i]] = i + 1;
        a.insert(a.begin() + i + 1, a[i]);
        //for (const auto &j : a) cout << j << " " << b[j] << endl;
        //cout << endl;
    }
    cout << ans;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    cerr << 1;
    vector<int> a(n * 2), b(n + 1);
    for (int i = 0; i < n * 2; ++i) {
        cin >> a[i];
        b[a[i]] = i;
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        if (b[a[i]] == i + 1 || b[a[i]] == i) continue;
        while (b[a[i]] != i + 1) {
            if (b[a[b[a[i]] - 1]] == b[a[i]] - 1) ++b[a[b[a[i]] - 1]];
            swap(a[b[a[i]]], a[b[a[i]] - 1]);
            --b[a[i]];
            ++ans;
        }
    }
    cout << ans;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n;

int solve(vector<int> &a) {
    int ans = 0;
    for (int i = 0; i < n * 2; i += 2) {
        if (a[i] == a[i + 1]) continue;
        auto x = find(a.begin() + i + 1, a.end(), a[i]);
        ans += (x - a.begin() - i - 1);
        rotate(a.begin() + i + 1, x, x + 1);
    }
    return ans;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    vector<int> a(n * 2);
    for (int i = 0; i < n * 2; ++i) cin >> a[i];
    cout << solve(a);

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int n;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "rt", stdin);
  freopen("output.txt", "wt", stdout);
#endif

    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    cerr << 1;
    vector<int> a(n * 10), b(n * 10);
    for (int i = 0; i < n * 2; ++i) {
        cin >> a[i];
        b[a[i]] = i;
    }
    int ans = 0;
    for (int i = 0; i < n; ++i) {
        if (b[a[i]] == i + 1 || b[a[i]] == i) continue;
        for (int j = i + 1; j < b[a[i]]; ++j) {
            if (b[a[j]] == j) ++b[a[j]];
        }
        ans += (b[a[i]] - i - 1);
        a.erase(a.begin() + b[a[i]]);
        b[a[i]] = i + 1;
        a.insert(a.begin() + i + 1, a[i]);
        //for (const auto &j : a) cout << j << " " << b[j] << endl;
        //cout << endl;
    }
    cout << ans;

  return 0;
}


#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main(){
    long long n, i, j, ans = 0, k;
    cin >> n;
    vector <int> a(2 * n);
    for (i = 0; i < 2 * n; i++)
        cin >> a[i];
    for (i = 0; i < n; i += 2){
            j = find(a.begin() + i + 1, a.end(), a[i]) - a.begin();
            for (k = j; k > i + 1; k--){
                a[k] = a[k - 1];
                ans++;
            }
            a[i + 1] = a[i];
    }
    cout << ans;
}


#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main(){
    long long n, i, j, ans = 0, k;
    cin >> n;
    vector <int> a(2 * n);
    for (i = 0; i < 2 * n; i++)
        cin >> a[i];
    for (i = 0; i < n; i += 2){
            j = find(a.begin() + i + 1, a.end(), a[i]) - a.begin();
            for (k = j; k > i + 1; k--){
                a[k] = a[k - 1];
                ans++;
            }
            a[j] = a[i];
    }
    cout << ans;
}


#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

int main(){
    long long n, i, j, ans = 0, k;
    cin >> n;
    vector <int> a(2 * n);
    for (i = 0; i < 2 * n; i++)
        cin >> a[i];
    for (i = 0; i < n * 2; i += 2){
            j = find(a.begin() + i + 1, a.end(), a[i]) - a.begin();
            for (k = j; k > i + 1; k--){
                a[k] = a[k - 1];
                ans++;
            }
            a[i + 1] = a[i];
    }
    cout << ans;
}


#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
//    freopen("input5.txt", "r", stdin);
//    freopen("output5.txt", "w", stdout);
    int n;
    cin >> n;
    vector <int> a(n * 2);
    vector <bool> bb(n + 1, true);
    vector <pair <int, int> > b(n + 1);
    for (int i = 0; i < n * 2; i++)
    {
        cin >> a[i];
        if (bb[a[i]]) {bb[a[i]] = false; b[a[i]].ff = i;}
            else { b[a[i]].ss = i;}
    }
    int otv = 0;
    for (int i = 0; i < 2* n - 1; i++)
    {
        int s = b[a[i]].ss;
            if (a[i] != a[i + 1]){
                for (int j = s; j >= i + 2 ; j--)
                {
                    if (b[a[j - 1]].ff == j - 1) swap(b[j].ss, b[j - 1].ff);
                        else swap( b[j].ss, b[j - 1].ss  );
                    swap(a[j], a[j - 1]);
                    otv++;
                }
            }
                else i++;
    }
    cout << otv;
    return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
//    freopen("input5.txt", "r", stdin);
//    freopen("output5.txt", "w", stdout);
    int n;
    cin >> n;
    vector <int> a(n * 2);
    vector <bool> bb(n + 1, true);
    vector <pair <int, int> > b(n + 1);
    for (int i = 0; i < n * 2; i++)
    {
        cin >> a[i];
        if (bb[a[i]]) {bb[a[i]] = false; b[a[i]].ff = i;}
            else { b[a[i]].ss = i;}
    }
    int otv = 0;
    for (int i = 0; i < 2* n - 1; i++)
    {
        int s = b[a[i]].ss;
            if (a[i] != a[i + 1]){
                for (int j = s; j >= i + 2 ; j--)
                {
                    if (b[a[j - 1]].ff == j - 1) swap(b[j].ss, b[j - 1].ff);
                        else swap( b[j].ss, b[j - 1].ss  );
                    swap(a[j], a[j - 1]);
                    otv++;
                }
                i++;
            }
                else i++;
    }
    cout << otv;
    return 0;
}


#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
//    freopen("input5.txt", "r", stdin);
//    freopen("output5.txt", "w", stdout);
    int n;
    cin >> n;

    vector <int> a(n * 2);
    vector <pair <int, int> > b(n + 1, {-1, -1});
    for (int i = 0; i < n * 2; i++)
    {
        cin >> a[i];
        if (b[a[i]].ff == -1) { b[a[i]].ff = i;}
            else { b[a[i]].ss = i;}
    }
    int otv = 0;
    for (int i = 0; i < 2* n - 1; i++)
    {
        int f = b[a[i]].ff, s = b[a[i]].ss;
            if (a[i] != a[i + 1]){
                for (int j = s; j >= f + 2 ; j--)
                {
                    if (b[a[j - 1]].ff == j - 1) swap(b[a[j]].ss, b[a[j - 1]].ff);
                        else swap( b[a[j]].ss, b[a[j - 1]].ss  );
                    swap(a[j], a[j - 1]);
                    otv++;
                }
                i++;
            }
                else i++;
    }
    cout << otv;
    return 0;
}


/*

 Shahadat Hossain
 Department of I.C.T
 Comilla University

 Problem site:- Codeforces
 Problem name:- Bus Number(E)
 Codeforces Round #491 (Div. 2)

 */

#include<cstdio>
#include <stdio.h>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>

#define ll long long int
#define sotto 1073741823

using namespace std;
map<ll,ll> smap;
pair<ll, ll> pari[220];

int main(){

    ll n;
    cin>>n;
    ll num[220];

    ll assign = 1;
    for(int i = 1; i<=2*n; i++){
        cin>>num[i];
        if(smap[num[i]]  == 0){
            smap[num[i]] = assign;
            assign++;
        }
        num[i] = smap[num[i]];
        if(pari[num[i]].first == 0)
            pari[num[i]].first = i;
        else
            pari[num[i]].second = i;

    }

    ll sum = 0;
    for(int i = 1; i<=2*n; i+=2){
        int next = pari[num[i]].second;
        for(int j = next; j>=i+2; j--){
            sum ++;
            ll temp = num[j];
            num[j] =  num[j - 1];
            num[j - 1] = temp;
        }
    }

    cout<<sum<<endl;

}


/*

 Shahadat Hossain
 Department of I.C.T
 Comilla University

 Problem site:- Codeforces
 Problem name:- Bus Number(E)
 Codeforces Round #491 (Div. 2)

 */

#include<cstdio>
#include <stdio.h>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>

#define ll long long int
#define sotto 1073741823
using namespace std;
map<ll,ll> smap;
pair<ll, ll> pari[110];

int main(){

    ll n;
    cin>>n;
    ll num[110];

    ll assign = 1;
    for(int i = 1; i<=2*n; i++){
        cin>>num[i];
        if(smap[num[i]]  == 0){
            smap[num[i]] = assign;
            assign++;
        }
        num[i] = smap[num[i]];
        if(pari[num[i]].first == 0)
            pari[num[i]].first = i;
        else
            pari[num[i]].second = i;

    }

    ll sum = 0;
    for(int i = 1; i<=2*n; i+=2){
        int next = pari[num[i]].second;
        for(int j = next; j>=i+2; j--){
            sum ++;
            ll temp = num[j];
            num[j] =  num[j - 1];
            num[j - 1] = temp;
        }
    }

    cout<<sum<<endl;

}


/*

 Shahadat Hossain
 Department of I.C.T
 Comilla University

 Problem site:- Codeforces
 Problem name:- Bus Number(E)
 Codeforces Round #491 (Div. 2)

 */

#include<cstdio>
#include <stdio.h>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>

#define ll long long int
#define sotto 1073741823

using namespace std;
map<ll,ll> smap;

int main(){

    ll n;
    cin>>n;
    ll num[550];

    ll assign = 1;
    for(int i = 1; i<=2*n; i++){
        cin>>num[i];
        if(smap[num[i]]  == 0){
            smap[num[i]] = assign;
            assign++;
        }
        num[i] = smap[num[i]];
    }

    ll sum = 0, next = 0;
    for(int i = 1; i<=2*n; i+=2){

        for(int j = i+1; j<=2*n; j++){
            if(num[i] == num[j])
                next = j;
        }
        for(int j = next; j>=i+2; j--){
            sum ++;
            ll temp = num[j];
            num[j] =  num[j - 1];
            num[j - 1] = temp;

        }
    }

    cout<<sum<<endl;

}


# include <bits/stdc++.h>
using namespace std;

int n,a[200+10];

int main(){
	cin>>n;
	for(int i=1;i<=2*n;i++) cin>>a[i];
	int res=0;
	for(int i=1;i<=2*n;i+=2){
		int j;
		for(j=i+1;j<=2*n;j++){
			if(a[j]==a[i]) break;
		}
		res+=(j-i-1);
		for(;a[j-1]!=a[i];j--){
			swap(a[j],a[j-1]);
		}
	}
	cout<<res<<endl;
	return 0;
}


# include <bits/stdc++.h>
using namespace std;

int n,a[100+10];

int main(){
	cin>>n;
	for(int i=1;i<=2*n;i++) cin>>a[i];
	int res=0;
	for(int i=1;i<=2*n;i+=2){
		int j;
		for(j=i+1;j<=2*n;j++){
			if(a[j]==a[i]) break;
		}
		res+=(j-i-1);
		for(;a[j-1]!=a[i];j--){
			swap(a[j],a[j-1]);
		}
	}
	cout<<res<<endl;
	return 0;
}


#include <iostream>
using namespace std;
#define int int64_t
int n, a[200], cnt;
int32_t main(){
	cin >> n;
	n <<= 1;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	for(int i = 1; i <= n; i++){
		int f = -1;
		for(int j = i + 1; j <= n; j++)
			if(a[j] == a[i]){
				f = j;
				break;
			}
		for(; f != -1 and f != i + 1; f--)
			swap(a[f], a[f - 1]), cnt++;
	}
	cout << cnt << '\n';
}


#include <iostream>
using namespace std;
#define int int64_t
int n, a[209], cnt;
int32_t main(){
	cin >> n;
	n <<= 1;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	for(int i = 1; i <= n; i++){
		int f = -1;
		for(int j = i + 1; j <= n; j++)
			if(a[j] == a[i]){
				f = j;
				break;
			}
		for(; f != -1 and f != i + 1; f--)
			swap(a[f], a[f - 1]), cnt++;
	}
	cout << cnt << '\n';
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = (int)1e6 + 10;
const int INF = 1e9;

pair < int, int > all[MaxN];
int en, ma[MaxN], mi[MaxN];
int fenw[MaxN], n;

void upd(int r, int val) {
	while (r <= 2 * n) {
		fenw[r] += val;
		r |= r + 1;
	}
}

int get(int r) {
	int ans = 0;
	while (r >= 0) {
		ans += fenw[r];
		r &= r + 1, --r;
	}
	return ans;
}

long long calc(const vector < int > &v) {
	for (int i = 0; i < (int)v.size(); ++i) {
		ma[v[i]] = i + 1;
		mi[v[(int)v.size() - 1 - i]] = (int)v.size() - i;
	}
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += min(ma[i] - mi[i] + 1, 2e9) - 2;
		assert (mi[i] >= 1 && ma[i] <= 2 * n && mi[i] != ma[i]);
		all[i] = make_pair(mi[i], ma[i]);
	}
	sort(all + 1, all + n + 1);
	for (int i = 1; i <= n; ++i) {
		ans -= get(all[i].second) - get(all[i].first);
		upd(all[i].second, +1);
	}
	for (int i = 1; i <= n; ++i) {
		ma[i] = mi[i] = 0;
	}
	for (int i = 0; i <= 2 * n; ++i) {
		fenw[i] = 0;
	}
	return ans;
}

void solve() {
	scanf("%d", &n);
	assert (1 <= n && n <= 500000);
	en += n;
	assert (en <= 1000000);
	vector < int > v;
	for (int i = 1; i <= 2 * n; ++i) {
		int x;
		scanf("%d", &x);
		assert (1 <= x && x <= n);
		v.push_back(x);
	}
	printf("%I64d\n", calc(v));
}

int main() {
//	freopen("input.txt", "r", stdin);
	int t = 1;
	assert (1 <= t && t <= 1000);
	while (t --> 0) {
		solve();
	}
	return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long lint;

vector<int> merge(const vector<int> &a, const vector<int> &b) {
  vector<int> result(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), result.begin());
  return result;
}

struct segtree_t {
  struct node_t {
    vector<int> values;
    int query(int min_value, int max_value) {
      return upper_bound(values.begin(), values.end(), max_value) -
        lower_bound(values.begin(), values.end(), min_value);
    }
  };
  int n;
  vector<node_t> tree;
  vector<vector<int>> init_values;
  segtree_t(int _n): n(_n), tree(4 * n), init_values(n) {}
  void pre_add(int pos, int value) {
    init_values[pos].push_back(value);
  }
  void build() { build(0, 0, n - 1); }
  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].values = init_values[l];
      sort(tree[v].values.begin(), tree[v].values.end());
    } else {
      int m = l+(r-l)/2;
      build(2*v+1, l, m);
      build(2*v+2, m+1, r);
      tree[v].values = merge(tree[2 * v + 1].values,
                             tree[2 * v + 2].values);
    }
  }
  int query(int x0, int x1, int y0, int y1) {
    if (x0 > x1 || y0 > y1) return 0;
    return query(x0, x1, y0, y1, 0, 0, n - 1);
  }
  int query(int x0, int x1, int y0, int y1, int v, int l, int r) {
    if (x0 > r || x1 < l) return 0;
    else if (x0 <= l && r <= x1) return tree[v].query(y0, y1);
    else {
      int m = l+(r-l)/2;
      return query(x0, x1, y0, y1, 2*v+1, l, m) +
        query(x0, x1, y0, y1, 2*v+2, m+1, r);
    }
  }
};

int main() {
  int nt = 1;
 for (int t = 0; t < nt; ++t) {
    int n;
    scanf("%d", &n);
    vector<int> values(2 * n);
    vector<vector<int>> pos_of(n);
    for (int i = 0; i < 2 * n; ++i) {
      scanf("%d", &values[i]), --values[i];
      pos_of[values[i]].push_back(i);
    }

    segtree_t all_seg(2 * n);
    for (int i = 0; i < n; ++i) {
      assert(pos_of[i].size() == 2);
      sort(pos_of[i].begin(), pos_of[i].end());
      all_seg.pre_add(pos_of[i][0], pos_of[i][1]);
      all_seg.pre_add(pos_of[i][1], pos_of[i][0]);
    }
    all_seg.build();

    lint two_result = 0;
    vector<bool> is_out(n);
    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];

      int cost_in = all_seg.query(l + 1, r - 1, l + 1, r - 1) / 2;
      int cost_out =
        all_seg.query(0, l - 1, 0, l - 1) / 2 +
        all_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2 +
        all_seg.query(0, l - 1, r + 1, 2 * n - 1);

      is_out[i] = cost_out < cost_in;

      int non_interleaved = cost_in + cost_out;
      int interleaved = n - 1 - non_interleaved;

      two_result += interleaved;
      two_result += 2 * non_interleaved;
    }
    assert(two_result % 2 == 0);
    lint result = two_result / 2;

    segtree_t in_seg(2 * n);
    for (int i = 0; i < n; ++i)
      if (!is_out[i]) {
        in_seg.pre_add(pos_of[i][0], pos_of[i][1]);
        in_seg.pre_add(pos_of[i][1], pos_of[i][0]);
      }
    in_seg.build();

    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];
      if (1) {
        result -= 2 * (in_seg.query(l + 1, r - 1, l + 1, r - 1) / 2);
      } else {
        result -= 2 * (in_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2);
      }
    }

    printf("%lld\n", result);
  }
  return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long lint;

vector<int> merge(const vector<int> &a, const vector<int> &b) {
  vector<int> result(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), result.begin());
  return result;
}

struct segtree_t {
  struct node_t {
    vector<int> values;
    int query(int min_value, int max_value) {
      return upper_bound(values.begin(), values.end(), max_value) -
        lower_bound(values.begin(), values.end(), min_value);
    }
  };
  int n;
  vector<node_t> tree;
  vector<vector<int>> init_values;
  segtree_t(int _n): n(_n), tree(4 * n), init_values(n) {}
  void pre_add(int pos, int value) {
    init_values[pos].push_back(value);
  }
  void build() { build(0, 0, n - 1); }
  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].values = init_values[l];
      sort(tree[v].values.begin(), tree[v].values.end());
    } else {
      int m = l+(r-l)/2;
      build(2*v+1, l, m);
      build(2*v+2, m+1, r);
      tree[v].values = merge(tree[2 * v + 1].values,
                             tree[2 * v + 2].values);
    }
  }
  int query(int x0, int x1, int y0, int y1) {
    if (x0 > x1 || y0 > y1) return 0;
    return query(x0, x1, y0, y1, 0, 0, n - 1);
  }
  int query(int x0, int x1, int y0, int y1, int v, int l, int r) {
    if (x0 > r || x1 < l) return 0;
    else if (x0 <= l && r <= x1) return tree[v].query(y0, y1);
    else {
      int m = l+(r-l)/2;
      return query(x0, x1, y0, y1, 2*v+1, l, m) +
        query(x0, x1, y0, y1, 2*v+2, m+1, r);
    }
  }
};

int main() {
  int nt = 1;
  for (int t = 0; t < nt; ++t) {
    int n;
    scanf("%d", &n);
    vector<int> values(2 * n);
    vector<vector<int>> pos_of(n);
    for (int i = 0; i < 2 * n; ++i) {
      scanf("%d", &values[i]), --values[i];
      pos_of[values[i]].push_back(i);
    }

    segtree_t all_seg(2 * n);
    for (int i = 0; i < n; ++i) {
      assert(pos_of[i].size() == 2);
      sort(pos_of[i].begin(), pos_of[i].end());
      all_seg.pre_add(pos_of[i][0], pos_of[i][1]);
      all_seg.pre_add(pos_of[i][1], pos_of[i][0]);
    }
    all_seg.build();

    lint two_result = 0;
    vector<bool> is_out(n);
    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];

      int cost_in = all_seg.query(l + 1, r - 1, l + 1, r - 1) / 2;
      int cost_out =
        all_seg.query(0, l - 1, 0, l - 1) / 2 +
        all_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2 +
        all_seg.query(0, l - 1, r + 1, 2 * n - 1);

      is_out[i] = cost_out < cost_in;

      int non_interleaved = cost_in + cost_out;
      int interleaved = n - 1 - non_interleaved;

      two_result += interleaved;
      two_result += 2 * non_interleaved;
    }
    assert(two_result % 2 == 0);
    lint result = two_result / 2;

    segtree_t in_seg(2 * n);
    for (int i = 0; i < n; ++i)
      if (!is_out[i]) {
        in_seg.pre_add(pos_of[i][0], pos_of[i][1]);
        in_seg.pre_add(pos_of[i][1], pos_of[i][0]);
      }
    in_seg.build();

    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];
      if (is_out[i]) {
        result -= 2 * (in_seg.query(l + 1, r - 1, l + 1, r - 1) / 2);
      } else {
        result -= 2 * (in_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2);
      }
    }

    printf("%lld\n", result);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & (-(x)))
const int N = 501000;

int n, A[N * 2];
vector<int> db[N];
int id[N];
int S[N * 2];

void Set(int p, int v) {
	++p;
	for (; p < n * 2 + 10; p += lowbit(p))
		S[p] += v;
}

int Get(int p) {
	int ret = 0;
	++p;
	for (; p; p -= lowbit(p))
		ret += S[p];
	return ret;
}

int main() {
	int ncase = 1;
	while (ncase--) {
		long long ans = 0;
		scanf("%d", &n);
		memset(id, 0xff, sizeof(int) * n);
		for (int i = 0; i < 2 * n; i++) {
			scanf("%d", A + i), -- A[i];
			if (1) {
				ans -= Get(id[A[i]] - 1);
				Set(id[A[i]], -1);
				int p = 2 * n - (id[A[i]] - 3);
				ans += min(i - p, 2 * n + p - i) - 1;
			}
			else
				id[A[i]] = 2 * n - i + 3, Set(id[A[i]], 1);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = (int)1e6 + 10;
const int INF = 1e9;

pair < int, int > all[MaxN];
int en, ma[MaxN], mi[MaxN];
int fenw[MaxN], n;

void upd(int r, int val) {
	while (r <= 2 * n) {
		fenw[r] += val;
		r |= r + 1;
	}
}

int get(int r) {
	int ans = 0;
	while (r >= 0) {
		ans += fenw[r];
		r &= r + 1, --r;
	}
	return ans;
}

long long calc(const vector < int > &v) {
	for (int i = 0; i < (int)v.size(); ++i) {
		ma[v[i]] = i + 1;
		mi[v[(int)v.size() - 1 - i]] = (int)v.size() - i;
	}
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += min(ma[i] - mi[i] + 1, 1e18ll) - 2;
		assert (mi[i] >= 1 && ma[i] <= 2 * n && mi[i] != ma[i]);
		all[i] = make_pair(mi[i], ma[i]);
	}
	sort(all + 1, all + n + 1);
	for (int i = 1; i <= n; ++i) {
		ans -= get(all[i].second) - get(all[i].first);
		upd(all[i].second, +1);
	}
	for (int i = 1; i <= n; ++i) {
		ma[i] = mi[i] = 0;
	}
	for (int i = 0; i <= 2 * n; ++i) {
		fenw[i] = 0;
	}
	return ans;
}

void solve() {
	scanf("%d", &n);
	assert (1 <= n && n <= 500000);
	en += n;
	assert (en <= 1000000);
	vector < int > v;
	for (int i = 1; i <= 2 * n; ++i) {
		int x;
		scanf("%d", &x);
		assert (1 <= x && x <= n);
		v.push_back(x);
	}
	printf("%lld\n", calc(v));
}

int main() {
//	freopen("input.txt", "r", stdin);
	int t = 1;
	assert (1 <= t && t <= 1000);
	while (t --> 0) {
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = (int)1e6 + 10;
const int INF = 1e9;

pair < int, int > all[MaxN];
int en, ma[MaxN], mi[MaxN];
int fenw[MaxN], n;

void upd(int r, int val) {
	while (r <= 2 * n) {
		fenw[r] += val;
		r |= r + 1;
	}
}

int get(int r) {
	int ans = 0;
	while (r >= 0) {
		ans += fenw[r];
		r &= r + 1, --r;
	}
	return ans;
}

long long calc(const vector < int > &v) {
	for (int i = 0; i < (int)v.size(); ++i) {
		ma[v[i]] = i + 1;
		mi[v[(int)v.size() - 1 - i]] = (int)v.size() - i;
	}
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += min(ma[i] - mi[i] + 1, 1e18) - 2;
		assert (mi[i] >= 1 && ma[i] <= 2 * n && mi[i] != ma[i]);
		all[i] = make_pair(mi[i], ma[i]);
	}
	sort(all + 1, all + n + 1);
	for (int i = 1; i <= n; ++i) {
		ans -= get(all[i].second) - get(all[i].first);
		upd(all[i].second, +1);
	}
	for (int i = 1; i <= n; ++i) {
		ma[i] = mi[i] = 0;
	}
	for (int i = 0; i <= 2 * n; ++i) {
		fenw[i] = 0;
	}
	return ans;
}

void solve() {
	scanf("%d", &n);
	assert (1 <= n && n <= 500000);
	en += n;
	assert (en <= 1000000);
	vector < int > v;
	for (int i = 1; i <= 2 * n; ++i) {
		int x;
		scanf("%d", &x);
		assert (1 <= x && x <= n);
		v.push_back(x);
	}
	printf("%lld\n", calc(v));
}

int main() {
//	freopen("input.txt", "r", stdin);
	int t = 1;
	assert (1 <= t && t <= 1000);
	while (t --> 0) {
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & (-(x)))
const int N = 501000;

int n, A[N * 2];
vector<int> db[N];
int id[N];
int S[N * 2];

void Set(int p, int v) {
	++p;
	for (; p < n * 2 + 10; p += lowbit(p))
		S[p] += v;
}

int Get(int p) {
	int ret = 0;
	++p;
	for (; p; p -= lowbit(p))
		ret += S[p];
	return ret;
}

int main() {
	int ncase = 1;
	while (ncase--) {
		long long ans = 0;
		scanf("%d", &n);
		memset(id, 0xff, sizeof(int) * n);
		for (int i = 0; i < 2 * n; i++) {
			scanf("%d", A + i), -- A[i];
			if (0) {
				ans -= Get(id[A[i]] - 1);
				Set(id[A[i]], -1);
				int p = 2 * n - (id[A[i]] - 3);
				ans += min(i - p, 2 * n + p - i) - 1;
			}
			else
				id[A[i]] = 2 * n - i + 3, Set(id[A[i]], 1);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long lint;

vector<int> merge(const vector<int> &a, const vector<int> &b) {
  vector<int> result(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), result.begin());
  return result;
}

struct segtree_t {
  struct node_t {
    vector<int> values;
    int query(int min_value, int max_value) {
      return upper_bound(values.begin(), values.end(), max_value) -
        lower_bound(values.begin(), values.end(), min_value);
    }
  };
  int n;
  vector<node_t> tree;
  vector<vector<int>> init_values;
  segtree_t(int _n): n(_n), tree(4 * n), init_values(n) {}
  void pre_add(int pos, int value) {
    init_values[pos].push_back(value);
  }
  void build() { build(0, 0, n - 1); }
  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].values = init_values[l];
      sort(tree[v].values.begin(), tree[v].values.end());
    } else {
      int m = l+(r-l)/2;
      build(2*v+1, l, m);
      build(2*v+2, m+1, r);
      tree[v].values = merge(tree[2 * v + 1].values,
                             tree[2 * v + 2].values);
    }
  }
  int query(int x0, int x1, int y0, int y1) {
    if (x0 > x1 || y0 > y1) return 0;
    return query(x0, x1, y0, y1, 0, 0, n - 1);
  }
  int query(int x0, int x1, int y0, int y1, int v, int l, int r) {
    if (x0 > r || x1 < l) return 0;
    else if (x0 <= l && r <= x1) return tree[v].query(y0, y1);
    else {
      int m = l+(r-l)/2;
      return query(x0, x1, y0, y1, 2*v+1, l, m) +
        query(x0, x1, y0, y1, 2*v+2, m+1, r);
    }
  }
};

int main() {
  int nt = 1;
 for (int t = 0; t < nt; ++t) {
    int n;
    scanf("%d", &n);
    vector<int> values(2 * n);
    vector<vector<int>> pos_of(n);
    for (int i = 0; i < 2 * n; ++i) {
      scanf("%d", &values[i]), --values[i];
      pos_of[values[i]].push_back(i);
    }

    segtree_t all_seg(2 * n);
    for (int i = 0; i < n; ++i) {
      assert(pos_of[i].size() == 2);
      sort(pos_of[i].begin(), pos_of[i].end());
      all_seg.pre_add(pos_of[i][0], pos_of[i][1]);
      all_seg.pre_add(pos_of[i][1], pos_of[i][0]);
    }
    all_seg.build();

    lint two_result = 0;
    vector<bool> is_out(n);
    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];

      int cost_in = all_seg.query(l + 1, r - 1, l + 1, r - 1) / 2;
      int cost_out =
        all_seg.query(0, l - 1, 0, l - 1) / 2 +
        all_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2 +
        all_seg.query(0, l - 1, r + 1, 2 * n - 1);

      is_out[i] = cost_out < cost_in;

      int non_interleaved = cost_in + cost_out;
      int interleaved = n - 1 - non_interleaved;

      two_result += interleaved;
      two_result += 2 * non_interleaved;
    }
    assert(two_result % 2 == 0);
    lint result = two_result / 2;

    segtree_t in_seg(2 * n);
    for (int i = 0; i < n; ++i)
      if (!is_out[i]) {
        in_seg.pre_add(pos_of[i][0], pos_of[i][1]);
        in_seg.pre_add(pos_of[i][1], pos_of[i][0]);
      }
    in_seg.build();

    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];
      if (0) {
        result -= 2 * (in_seg.query(l + 1, r - 1, l + 1, r - 1) / 2);
      } else {
        result -= 2 * (in_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2);
      }
    }

    printf("%lld\n", result);
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = (int)1e6 + 10;
const int INF = 1e9;

pair < int, int > all[MaxN];
int en, ma[MaxN], mi[MaxN];
int fenw[MaxN], n;

void upd(int r, int val) {
	while (r <= 2 * n) {
		fenw[r] += val;
		r |= r + 1;
	}
}

int get(int r) {
	int ans = 0;
	while (r >= 0) {
		ans += fenw[r];
		r &= r + 1, --r;
	}
	return ans;
}

long long calc(const vector < int > &v) {
	for (int i = 0; i < (int)v.size(); ++i) {
		ma[v[i]] = i + 1;
		mi[v[(int)v.size() - 1 - i]] = (int)v.size() - i;
	}
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += min(ma[i] - mi[i] + 1, 2 * n - ma[i] + 1 + mi[i]) - 2;
		assert (mi[i] >= 1 && ma[i] <= 2 * n && mi[i] != ma[i]);
		all[i] = make_pair(mi[i], ma[i]);
	}
	sort(all + 1, all + n + 1);
	for (int i = 1; i <= n; ++i) {
		ans -= get(all[i].second) - get(all[i].first);
		upd(all[i].second, +1);
	}
	for (int i = 1; i <= n; ++i) {
		ma[i] = mi[i] = 0;
	}
	for (int i = 0; i <= 2 * n; ++i) {
		fenw[i] = 0;
	}
	return ans;
}

void solve() {
	scanf("%d", &n);
	assert (1 <= n && n <= 500000);
	en += n;
	assert (en <= 1000000);
	vector < int > v;
	for (int i = 1; i <= 2 * n; ++i) {
		int x;
		scanf("%d", &x);
		assert (1 <= x && x <= n);
		v.push_back(x);
	}
	printf("%lld\n", calc(v));
}

int main() {
//	freopen("input.txt", "r", stdin);
	int t = 1;
	assert (1 <= t && t <= 1000);
	while (t --> 0) {
		solve();
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int MaxN = (int)1e6 + 10;
const int INF = 1e9;

pair < int, int > all[MaxN];
int en, ma[MaxN], mi[MaxN];
int fenw[MaxN], n;

void upd(int r, int val) {
	while (r <= 2 * n) {
		fenw[r] += val;
		r |= r + 1;
	}
}

int get(int r) {
	int ans = 0;
	while (r >= 0) {
		ans += fenw[r];
		r &= r + 1, --r;
	}
	return ans;
}

long long calc(const vector < int > &v) {
	for (int i = 0; i < (int)v.size(); ++i) {
		ma[v[i]] = i + 1;
		mi[v[(int)v.size() - 1 - i]] = (int)v.size() - i;
	}
	long long ans = 0;
	for (int i = 1; i <= n; ++i) {
		ans += ma[i] - mi[i] + 1 - 2;
		assert (mi[i] >= 1 && ma[i] <= 2 * n && mi[i] != ma[i]);
		all[i] = make_pair(mi[i], ma[i]);
	}
	sort(all + 1, all + n + 1);
	for (int i = 1; i <= n; ++i) {
		ans -= get(all[i].second) - get(all[i].first);
		upd(all[i].second, +1);
	}
	for (int i = 1; i <= n; ++i) {
		ma[i] = mi[i] = 0;
	}
	for (int i = 0; i <= 2 * n; ++i) {
		fenw[i] = 0;
	}
	return ans;
}

void solve() {
	scanf("%d", &n);
	assert (1 <= n && n <= 500000);
	en += n;
	assert (en <= 1000000);
	vector < int > v;
	for (int i = 1; i <= 2 * n; ++i) {
		int x;
		scanf("%d", &x);
		assert (1 <= x && x <= n);
		v.push_back(x);
	}
	printf("%I64d\n", calc(v));
}

int main() {
//	freopen("input.txt", "r", stdin);
	int t = 1;
	assert (1 <= t && t <= 1000);
	while (t --> 0) {
		solve();
	}
	return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long lint;
#define int long long

vector<int> merge(const vector<int> &a, const vector<int> &b) {
  vector<int> result(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), result.begin());
  return result;
}

struct segtree_t {
  struct node_t {
    vector<int> values;
    int query(int min_value, int max_value) {
      return upper_bound(values.begin(), values.end(), max_value) -
        lower_bound(values.begin(), values.end(), min_value);
    }
  };
  int n;
  vector<node_t> tree;
  vector<vector<int>> init_values;
  segtree_t(int _n): n(_n), tree(4 * n), init_values(n) {}
  void pre_add(int pos, int value) {
    init_values[pos].push_back(value);
  }
  void build() { build(0, 0, n - 1); }
  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].values = init_values[l];
      sort(tree[v].values.begin(), tree[v].values.end());
    } else {
      int m = l+(r-l)/2;
      build(2*v+1, l, m);
      build(2*v+2, m+1, r);
      tree[v].values = merge(tree[2 * v + 1].values,
                             tree[2 * v + 2].values);
    }
  }
  int query(int x0, int x1, int y0, int y1) {
    if (x0 > x1 || y0 > y1) return 0;
    return query(x0, x1, y0, y1, 0, 0, n - 1);
  }
  int query(int x0, int x1, int y0, int y1, int v, int l, int r) {
    if (x0 > r || x1 < l) return 0;
    else if (x0 <= l && r <= x1) return tree[v].query(y0, y1);
    else {
      int m = l+(r-l)/2;
      return query(x0, x1, y0, y1, 2*v+1, l, m) +
        query(x0, x1, y0, y1, 2*v+2, m+1, r);
    }
  }
};

 main() {
  int nt = 1;
 for (int t = 0; t < nt; ++t) {
    int n;
    scanf("%d", &n);
    vector<int> values(2 * n);
    vector<vector<int>> pos_of(n);
    for (int i = 0; i < 2 * n; ++i) {
      scanf("%d", &values[i]), --values[i];
      pos_of[values[i]].push_back(i);
    }

    segtree_t all_seg(2 * n);
    for (int i = 0; i < n; ++i) {
      assert(pos_of[i].size() == 2);
      sort(pos_of[i].begin(), pos_of[i].end());
      all_seg.pre_add(pos_of[i][0], pos_of[i][1]);
      all_seg.pre_add(pos_of[i][1], pos_of[i][0]);
    }
    all_seg.build();

    lint two_result = 0;
    vector<bool> is_out(n);
    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];

      int cost_in = all_seg.query(l + 1, r - 1, l + 1, r - 1) / 2;
      int cost_out =
        all_seg.query(0, l - 1, 0, l - 1) / 2 +
        all_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2 +
        all_seg.query(0, l - 1, r + 1, 2 * n - 1);

      is_out[i] = cost_out < cost_in;

      int non_interleaved = cost_in + cost_out;
      int interleaved = n - 1 - non_interleaved;

      two_result += interleaved;
      two_result += 2 * non_interleaved;
    }
    assert(two_result % 2 == 0);
    lint result = two_result / 2;

    segtree_t in_seg(2 * n);
    for (int i = 0; i < n; ++i)
      if (!is_out[i]) {
        in_seg.pre_add(pos_of[i][0], pos_of[i][1]);
        in_seg.pre_add(pos_of[i][1], pos_of[i][0]);
      }
    in_seg.build();

    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];
      if (0) {
        result -= 2 * (in_seg.query(l + 1, r - 1, l + 1, r - 1) / 2);
      } else {
        result -= 2 * (in_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2);
      }
    }

    printf("%lld\n", result);
  }
  return 0;
}


#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>

using namespace std;

typedef long long lint;
#define int long long

vector<int> merge(const vector<int> &a, const vector<int> &b) {
  vector<int> result(a.size() + b.size());
  merge(a.begin(), a.end(), b.begin(), b.end(), result.begin());
  return result;
}

struct segtree_t {
  struct node_t {
    vector<int> values;
    int query(int min_value, int max_value) {
      return upper_bound(values.begin(), values.end(), max_value) -
        lower_bound(values.begin(), values.end(), min_value);
    }
  };
  int n;
  vector<node_t> tree;
  vector<vector<int>> init_values;
  segtree_t(int _n): n(_n), tree(4 * n), init_values(n) {}
  void pre_add(int pos, int value) {
    init_values[pos].push_back(value);
  }
  void build() { build(0, 0, n - 1); }
  void build(int v, int l, int r) {
    if (l == r) {
      tree[v].values = init_values[l];
      sort(tree[v].values.begin(), tree[v].values.end());
    } else {
      int m = l+(r-l)/2;
      build(2*v+1, l, m);
      build(2*v+2, m+1, r);
      tree[v].values = merge(tree[2 * v + 1].values,
                             tree[2 * v + 2].values);
    }
  }
  int query(int x0, int x1, int y0, int y1) {
    if (x0 > x1 || y0 > y1) return 0;
    return query(x0, x1, y0, y1, 0, 0, n - 1);
  }
  int query(int x0, int x1, int y0, int y1, int v, int l, int r) {
    if (x0 > r || x1 < l) return 0;
    else if (x0 <= l && r <= x1) return tree[v].query(y0, y1);
    else {
      int m = l+(r-l)/2;
      return query(x0, x1, y0, y1, 2*v+1, l, m) +
        query(x0, x1, y0, y1, 2*v+2, m+1, r);
    }
  }
};

 main() {
  int nt = 1;
 for (int t = 0; t < nt; ++t) {
    int n;
    scanf("%d", &n);
    vector<int> values(2 * n);
    vector<vector<int>> pos_of(n);
    for (int i = 0; i < 2 * n; ++i) {
      scanf("%d", &values[i]), --values[i];
      pos_of[values[i]].push_back(i);
    }

    segtree_t all_seg(2 * n);
    for (int i = 0; i < n; ++i) {
      assert(pos_of[i].size() == 2);
      sort(pos_of[i].begin(), pos_of[i].end());
      all_seg.pre_add(pos_of[i][0], pos_of[i][1]);
      all_seg.pre_add(pos_of[i][1], pos_of[i][0]);
    }
    all_seg.build();

    lint two_result = 0;
    vector<bool> is_out(n);
    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];

      int cost_in = all_seg.query(l + 1, r - 1, l + 1, r - 1) / 2;
      int cost_out =
        all_seg.query(0, l - 1, 0, l - 1) / 2 +
        all_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2 +
        all_seg.query(0, l - 1, r + 1, 2 * n - 1);

      is_out[i] = cost_out < cost_in;

      int non_interleaved = cost_in + cost_out;
      int interleaved = n - 1 - non_interleaved;

      two_result += interleaved;
      two_result += 2 * non_interleaved;
    }
    assert(two_result % 2 == 0);
    lint result = two_result / 2;

    segtree_t in_seg(2 * n);
    for (int i = 0; i < n; ++i)
      if (!is_out[i]) {
        in_seg.pre_add(pos_of[i][0], pos_of[i][1]);
        in_seg.pre_add(pos_of[i][1], pos_of[i][0]);
      }
    in_seg.build();

    for (int i = 0; i < n; ++i) {
      int l = pos_of[i][0], r = pos_of[i][1];
      if (0) {
        result -= 2 * (in_seg.query(l + 1, r - 1, l + 1, r - 1) / 2);
      } else {
        result -= 2 * (in_seg.query(r + 1, 2 * n - 1, r + 1, 2 * n - 1) / 2);
      }
    }

    printf("%I64d\n", result);
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & (-(x)))
const int N = 501000;

int n, A[N * 2];
vector<int> db[N];
int id[N];
int S[N * 2];

void Set(int p, int v) {
	++p;
	for (; p < n * 2 + 10; p += lowbit(p))
		S[p] += v;
}

int Get(int p) {
	int ret = 0;
	++p;
	for (; p; p -= lowbit(p))
		ret += S[p];
	return ret;
}

int main() {
	int ncase = 1;
	while (ncase--) {
		long long ans = 0;
		scanf("%d", &n);
		memset(id, 0xff, sizeof(int) * n);
		for (int i = 0; i < 2 * n; i++) {
			scanf("%d", A + i), -- A[i];
			ans -= Get(id[A[i]] - 1);
				Set(id[A[i]], -1);
				int p = 2 * n - (id[A[i]] - 3);
				ans += min(i - p, 2 * n + p - i) - 1;

		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x) & (-(x)))
const int N = 501000;

int n, A[N * 2];
vector<int> db[N];
int id[N];
int S[N * 2];

void Set(int p, int v) {
	++p;
	for (; p < n * 2 + 10; p += lowbit(p))
		S[p] += v;
}

int Get(int p) {
	int ret = 0;
	++p;
	for (; p; p -= lowbit(p))
		ret += S[p];
	return ret;
}

int main() {
	int ncase = 1;
	while (ncase--) {
		long long ans = 0;
		scanf("%d", &n);
		memset(id, 0xff, sizeof(int) * n);
		for (int i = 0; i < 2 * n; i++) {
			scanf("%d", A + i), -- A[i];
			if (id[A[i]] != -1) {
				ans -= Get(id[A[i]] - 1);
				Set(id[A[i]], -1);
				int p = 2 * n - (id[A[i]] - 3);
				ans += min(i - p, 2 * n + p - i) - 1;
			}
			else
				id[A[i]] = 2 * n - i + 3, Set(id[A[i]], 1);
		}
		printf("%lld\n", ans);
	}
	return 0;
}


#include <deque>
#include <limits>
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>

using namespace std;

#define len(x) ((int)x.size())

typedef vector<int> vi;

deque<int> arr;
deque<deque<int> > pos;
int n;

bool verify() {
  for (int i = 0; i < (2 * n) - 1; i += 2) {
    if (arr[i] != arr[i + 1]) {
      return false;
    }
  }

  return true;
}

int main() {
#ifndef DEBUG
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
#endif
  cin >> n;
  pos.assign(n + 1, deque<int>());
  deque<int> sum(n + 1, 0);
  for (int i = 0; i < 2 * n; i++) {
    int u; cin >> u;
    //printf("li %d\n", u);
    arr.push_back(u);
    pos[u].push_back(i);
  }
  deque<int> fixed(n + 1, 0);
  if (!verify()) {
    int ans = 0;
    for (int i = 0; i < (2 * n) - 1; i += 1) {
      if (fixed[arr[i]]) {
        sum[i + 1] = sum[i];
        continue;
      }

      if (arr[i] != arr[i + 1]) {
        //printf("wrong %d %d at %d and %d\n", arr[i], arr[i + 1], i, i + 1);
        //printf("     second %d is at %d\n", arr[i], pos[arr[i]][1]);
        sum[i + 1] = sum[i] + 1;
        //printf("will add %d - %d = %d\n", (pos[arr[i]][1] - i - 1), sum[i], (pos[arr[i]][1] - i - 1) - sum[i]);
        ans += (pos[arr[i]][1] - i - 1) - sum[i];
        fixed[arr[i]] = true;
      } else {
        fixed[arr[i]] = true;
      }
    }
    cout << ans << '\n';
  } else {
    cout << 0 << '\n';
  }
  return 0;
}


#include <deque>
#include <limits>
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>

using namespace std;

#define len(x) ((int)x.size())

typedef vector<int> vi;

deque<int> arr;
deque<deque<int> > pos;
int n;

bool verify() {
  for (int i = 0; i < (2 * n) - 1; i += 2) {
    if (arr[i] != arr[i + 1]) {
      return false;
    }
  }

  return true;
}

int main() {
#ifndef DEBUG
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
#endif
  cin >> n;
  pos.assign(n + 1, deque<int>());
  for (int i = 0; i < 2 * n; i++) {
    int u; cin >> u;
    arr.push_back(u);
    pos[u].push_back(i);
  }
  if (!verify()) {
    int ans = 0;
    while(!verify()) {
      for (int i = 0; i < (2 * n) - 1; i += 2) {
        if (arr[i] != arr[i + 1]) {
          //printf("%d %d on %d and %d\n", arr[i], arr[i + 1], i, i + 1);
          int where = pos[arr[i]][1];
          //printf("the second %d is in %d\n", arr[i], where);
          for (int k = where; k > i; k--) {
            //printf("        changed %d with %d\n", arr[k], arr[k - 1]);
            swap(arr[k], arr[k - 1]);
            ans++;
          }
          ans--;
          break;
        }
      }
    }
    cout << ans << '\n';
  } else {
    cout << 0 << '\n';
  }
  return 0;
}


#include <deque>
#include <limits>
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>

using namespace std;

#define len(x) ((int)x.size())

typedef vector<int> vi;

deque<int> arr;
deque<deque<int> > pos;
int n;

bool verify() {
  for (int i = 0; i < (2 * n) - 1; i += 2) {
    if (arr[i] != arr[i + 1]) {
      return false;
    }
  }

  return true;
}

int main() {
#ifndef DEBUG
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
#endif
  cin >> n;
  pos.assign(n + 1, deque<int>());
  for (int i = 0; i < 2 * n; i++) {
    int u; cin >> u;
    arr.push_back(u);
    pos[u].push_back(i);
  }
  if (!verify()) {
    int ans = 0;
    while(!verify()) {
      for (int i = 0; i < (2 * n) - 1; i += 2) {
        if (arr[i] != arr[i + 1]) {
          //printf("%d %d on %d and %d\n", arr[i], arr[i + 1], i, i + 1);
          int where = pos[arr[i]][1];
          //printf("the second %d is in %d\n", arr[i], where);
          for (int k = where; k > i + 1; k--) {
            //printf("        changed %d with %d\n", arr[k], arr[k - 1]);
            swap(arr[k], arr[k - 1]);
            ans++;
          }
          break;
        }
      }
    }
    cout << ans << '\n';
  } else {
    cout << 0 << '\n';
  }
  return 0;
}


#include <deque>
#include <limits>
#include <cstdio>
#include <iostream>
#include <vector>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>

using namespace std;

#define len(x) ((int)x.size())

typedef vector<int> vi;

deque<int> arr;
deque<deque<int> > pos;
int n;

bool verify() {
  for (int i = 0; i < (2 * n) - 1; i += 2) {
    if (arr[i] != arr[i + 1]) {
      return false;
    }
  }

  return true;
}

int main() {
#ifndef DEBUG
  ios::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
#endif
  cin >> n;
  pos.assign(n + 1, deque<int>());
  for (int i = 0; i < 2 * n; i++) {
    int u; cin >> u;
    arr.push_back(u);
    pos[u].push_back(i);
  }
  if (!verify()) {
    int ans = 0;
    while(!verify()) {
      for (int i = 0; i < (2 * n) - 1; i += 2) {
        if (arr[i] != arr[i + 1]) {
          //printf("%d %d on %d and %d\n", arr[i], arr[i + 1], i, i + 1);
          int where = pos[arr[i]][1];
          //printf("the second %d is in %d\n", arr[i], where);
          for (int k = where; k > i + 1; k--) {
            //printf("        changed %d (in %d) with %d (in %d)\n", arr[k], k, arr[k - 1], k -1);
            swap(arr[k], arr[k - 1]);
            ans++;
          }
          //ans--;
          pos.assign(n + 1, deque<int>());
          for (int i = 0; i < 2 * n; i++) {
            pos[arr[i]].push_back(i);
          }
          break;
        }
      }
      /*printf("array now is:\n");
      for (int x : arr) {
        cout << x << " ";
      } cout << endl;*/
    }
    cout << ans << '\n';
  } else {
    cout << 0 << '\n';
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
const int MAX_N = 101;
int N;

int tab[MAX_N];

void affiche(){
    for(int i = 0; i < N; i++)
        cout << tab[i] << " ";

    cout << endl;
}

void deswap(int a,int k){
    //cout << "Deswap" << endl;
    //cout << a << " " << k << endl;
    //affiche();
    for(int i = 0 ; i < k;i++)
        swap(tab[a+i],tab[a+i+1]);
    //affiche();
}

int change(int i, int x){
    //cout << "swap" << endl;
    //affiche();

    int j = 0;
    for(int k = i+1 ; k < N;k++)
        if(tab[k] == x){
            j = k;
            break;
        }

    for(int k = j-i-1; k >=0 ;k--){
        //cout << k << endl;
        swap(tab[i+k],tab[i+k+1]);
        //affiche();
    }

    //cout << i << " " << j << " " << (j-i) << endl;
    //affiche();
    return(j-i);

}

int rec(int a){
    //cout << a << endl;

    if(a == N-2)
        return 0;

    if(tab[a]==tab[a+1])
        return rec(a+2);

    int res1 = 0;
    int temp = 0;
    int res2 = 0;

    res1 = change(a+1,tab[a]);
    temp = res1;
    res1 += rec(a+2);
    deswap(a+1,temp);

    swap(tab[a],tab[a+1]);
    res2 = change(a+1,tab[a]);
    temp = res2;
    res2 += (rec(a+2)+1);
    deswap(a+1,temp);
    swap(tab[a],tab[a+1]);

    //cout << min(res1,res2);
    return min(res1,res2);
}

int main(){
    cin >> N;
    N= 2*N;

    for(int i = 0; i < N;i++)
        cin >> tab[i];

    cout << rec(0) <<endl;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
const int MAX_N = 202;
int N;

int tab[MAX_N];

void affiche(){
    for(int i = 0; i < N; i++)
        cout << tab[i] << " ";

    cout << endl;
}

void deswap(int a,int k){
    //cout << "Deswap" << endl;
    //cout << a << " " << k << endl;
    //affiche();
    for(int i = 0 ; i < k;i++)
        swap(tab[a+i],tab[a+i+1]);
    //affiche();
}

int change(int i, int x){
    //cout << "swap" << endl;
    //affiche();

    int j = 0;
    for(int k = i+1 ; k < N;k++)
        if(tab[k] == x){
            j = k;
            break;
        }

    for(int k = j-i-1; k >=0 ;k--){
        //cout << k << endl;
        swap(tab[i+k],tab[i+k+1]);
        //affiche();
    }

    //cout << i << " " << j << " " << (j-i) << endl;
    //affiche();
    return(j-i);

}

int rec(int a){
    //cout << a << endl;

    if(a == N-2)
        return 0;

    if(tab[a]==tab[a+1])
        return rec(a+2);

    int res1 = 0;
    int temp = 0;
    int res2 = 0;

    res1 = change(a+1,tab[a]);
    temp = res1;
    res1 += rec(a+2);
    deswap(a+1,temp);

    swap(tab[a],tab[a+1]);
    res2 = change(a+1,tab[a]);
    temp = res2;
    res2 += (rec(a+2)+1);
    deswap(a+1,temp);
    swap(tab[a],tab[a+1]);

    //cout << min(res1,res2);
    return min(res1,res2);
}

int main(){
    cin >> N;
    N= 2*N;

    for(int i = 0; i < N;i++)
        cin >> tab[i];

    cout << rec(0) <<endl;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

const int MAX_N = 201;
pair<int,int> tab[MAX_N];
int N,n;

int rappel[MAX_N];
int pos[MAX_N][2];

bool echangeable(int i, int j, bool deux){
    int p1 = pos[tab[i].first][tab[i].second];
    int p2 = pos[tab[i].first][1-tab[i].second];

    int q1 = pos[tab[j].first][tab[j].second];
    int q2 = pos[tab[j].first][1-tab[j].second];

    //cout << p1 << " " << p2 << " " << q1 << " " << q2 << endl;

    if( (p1 > q2 && p1 < q1 && q1 < p2) || (p1 < q1 && q1 < q2 && q2 < p2))
        return true;

    if(deux)
        return false;

    return echangeable(j,i,true);
}

void actualise(int &d){
    d = 0;
    for(int i = 0 ; i< N; i++){
        pos[tab[i].first][tab[i].second] = i;
        //cout << "(" << tab[i].first << "," << tab[i].second << "," << pos[tab[i].first][tab[i].second] << ") ";
    }
    //cout << endl;

    /*
    cout << "Pos_Deb" << endl;
    for(int i = 0; i < n;i++)
        cout << pos[i][0]  << " " << pos[i][1] << endl;
    cout << "Pos_fin" << endl;
    */

    for(int i = 0 ; i <= n;i++)
        d+=(abs(pos[i][0] - pos[i][1]));

}

void doNextSwap(){

    for(int i = 0 ; i < (N-1);i++){
        if(tab[i].first != tab[i+1].first){
            if(echangeable(i,i+1,false)){
                swap(tab[i],tab[i+1]);
                return;
            }
        }
    }

}

void affiche(){

    cout << "tab_Debut" <<endl;
    for(int i = 0; i < N; i++)
        cout << tab[i].first << "  ";
    cout << endl;

    for(int i = 1 ;i <= n;i++){
        cout << pos[i][0] <<" " << pos[i][1] << endl;
    }
    cout << "tab_fin" << endl;
}

int solve(){
    int d = 0;
    int res = 0;

    actualise(d);

    while(d > n){
        //affiche();
        doNextSwap();
        actualise(d);
        res++;
        //cout << d << endl;
    }

    return res;
}

int main(){
    cin >> n;
    N = 2*n;
    int x;
    int nb;

    for(int i = 0; i < N; i++){
        cin >> x;
        nb = rappel[x];
        rappel[x]++;
        tab[i]=make_pair(x,nb);
        pos[x][nb] = i;
    }

    /*for(int i = 0 ; i < N; i++){
        cout <<  "(" <<  tab[i].first << "," << tab[i].second << ")";
    }
    cout << endl;*/

    cout << solve() << endl;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
const int MAX_N = 101;
int N;

int tab[MAX_N];

void affiche(){
    for(int i = 0; i < N; i++)
        cout << tab[i] << " ";

    cout << endl;
}

void deswap(int a,int k){
    //cout << "Deswap" << endl;
    //cout << a << " " << k << endl;
    //affiche();
    for(int i = 0 ; i < k;i++)
        swap(tab[a+i],tab[a+i+1]);
    //affiche();
}

int change(int i, int x){
    //cout << "swap" << endl;
    //affiche();

    int j = 0;
    for(int k = i+1 ; k < N;k++)
        if(tab[k] == x){
            j = k;
            break;
        }

    for(int k = j-i-1; k >=0 ;k--){
        //cout << k << endl;
        swap(tab[i+k],tab[i+k+1]);
        //affiche();
    }

    //cout << i << " " << j << " " << (j-i) << endl;
    //affiche();
    return(j-i);

}

int rec(int a){
    //cout << a << endl;

    if(a == N-2)
        return 0;

    if(tab[a]==tab[a+1])
        return rec(a+2);

    int res1 = 0;
    int temp = 0;
    int res2 = 0;

    res1 = change(a+1,tab[a]);
    temp = res1;
    res1 += rec(a+2);
    deswap(a+1,temp);

    swap(tab[a],tab[a+1]);
    res2 = change(a+1,tab[a]);
    temp = res2;
    res2 += (rec(a+2)+1);
    deswap(a,temp);
    swap(tab[a],tab[a+1]);

    return min(res1,res2);
}

int main(){
    cin >> N;
    N= 2*N;

    for(int i = 0; i < N;i++)
        cin >> tab[i];

    cout << rec(0) <<endl;

}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;
const int MAX_N = 101;
int N;

int tab[MAX_N];

void affiche(){
    for(int i = 0; i < N; i++)
        cout << tab[i] << " ";

    cout << endl;
}

void deswap(int a,int k){
    //cout << "Deswap" << endl;
    //cout << a << " " << k << endl;
    //affiche();
    for(int i = 0 ; i < k;i++)
        swap(tab[a+i],tab[a+i+1]);
    //affiche();
}

int change(int i, int x){
    //cout << "swap" << endl;
    //affiche();

    int bonus = 0;
    int deb = 0;
    if(i%2 == 0)
        deb = i+2;
    else
        deb = i+1;

    int j = 0;
    for(int k = deb ; k < N;k++)
        if(tab[k] == x){
            j = k;
            break;
        }

    if(i%2==0){
        bonus=1;
        swap(tab[i],tab[i+1]);
    }

    for(int k = j-i-1; k >=0 ;k--){
        //cout << k << endl;
        swap(tab[i+k],tab[i+k+1]);
        //affiche();
    }

    //cout << i << " " << j << " " << (j-i) << endl;
    //affiche();
    return(j-i);

}

int rec(int a){
    //cout << a << endl;

    if(a == N-2)
        return 0;

    if(tab[a]==tab[a+1])
        return rec(a+2);

    int res1 = 0;
    int temp = 0;
    int res2 = 0;

    res1 = change(a+1,tab[a]);
    temp = res1;
    res1 += rec(a+2);
    deswap(a+1,temp);

    res2 = change(a,tab[a+1]);
    temp = res2;
    res2 += rec(a+2);
    deswap(a,temp);

    return min(res1,res2);
}

int main(){
    cin >> N;
    N= 2*N;

    for(int i = 0; i < N;i++)
        cin >> tab[i];

    cout << rec(0) <<endl;

}


#include <bits/stdc++.h>
using namespace std;

#define elif else if
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=a-1;i>=n;i--)
#define pf(x) printf("%.15f\n", x)
#define pb(x) push_back(x)
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define files freopen("input.in","r",stdin);freopen("output.out","w",stdout)
#define bs(s,n) binary_search(s.begin(),s.end(),n)
#define rs(s) s.begin(),s.end()
#define sz(s) s.size()
#define fi first
#define se second

typedef long long ll;
const long double M = 144115188075855872;

ll a[100010];

int main()
{
    IOS;
    int n,a[110],b[110]={},d[110],k=0;
    bool c[110];
    cin>>n;
    for(int i=1;i<=2*n;i++){
        d[i]=-1;
     c[i]=false;
    }
    for(int i=1;i<=2*n;i++){
        cin>>a[i];
        d[a[i]]=i-d[a[i]];
        if(c[a[i]])
            b[i]=-1;
        else{
            b[i]=1;
            c[a[i]]=true;
        }
    }

    for(int i=1;i<=n;i++){
            if(b[i]<0)
                continue;
                int p=i+d[a[i]];
        for(int j=i+1;j<=p;j++){
            if(b[j]<0 || j+d[a[j]]<p)
                k++;
        }

    }
    cout<<k;
}


#include <bits/stdc++.h>
using namespace std;

#define elif else if
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=a-1;i>=n;i--)
#define pf(x) printf("%.15f\n", x)
#define pb(x) push_back(x)
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define files freopen("input.in","r",stdin);freopen("output.out","w",stdout)
#define bs(s,n) binary_search(s.begin(),s.end(),n)
#define rs(s) s.begin(),s.end()
#define sz(s) s.size()
#define fi first
#define se second

typedef long long ll;
const long double M = 144115188075855872;

ll a[100010];

int main()
{
    IOS;
    int n,a[210],b[210]={},d[210],k=0;
    bool c[210];
    cin>>n;
    for(int i=1;i<=2*n;i++){
        d[i]=-1;
     c[i]=false;
    }
    for(int i=1;i<=2*n;i++){
        cin>>a[i];
        d[a[i]]=i-d[a[i]];
        if(c[a[i]])
            b[i]=-1;
        else{
            b[i]=1;
            c[a[i]]=true;
        }
    }

    for(int i=1;i<=2*n;i++){
            if(b[i]<0)
                continue;
                int p=i+d[a[i]];
        for(int j=i+1;j<=p;j++){
            if(b[j]<0 || j+d[a[j]]<p){
                k++;
            }
        }

    }
    cout<<k;
}


#include <bits/stdc++.h>
using namespace std;

#define elif else if
#define rep(i,a,n) for (ll i=a;i<n;i++)
#define per(i,a,n) for (ll i=a-1;i>=n;i--)
#define pf(x) printf("%.15f\n", x)
#define pb(x) push_back(x)
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define files freopen("input.in","r",stdin);freopen("output.out","w",stdout)
#define bs(s,n) binary_search(s.begin(),s.end(),n)
#define rs(s) s.begin(),s.end()
#define sz(s) s.size()
#define fi first
#define se second

typedef long long ll;
const long double M = 144115188075855872;

ll a[100010];

int main()
{
    IOS;
    int n,a[110],b[110]={},d[110],k=0;
    bool c[110];
    cin>>n;
    for(int i=1;i<=2*n;i++){
        d[i]=-1;
     c[i]=false;
    }
    for(int i=1;i<=2*n;i++){
        cin>>a[i];
        d[a[i]]=i-d[a[i]];
        if(c[a[i]])
            b[i]=-1;
        else{
            b[i]=1;
            c[a[i]]=true;
        }
    }

    for(int i=1;i<=2*n;i++){
            if(b[i]<0)
                continue;
                int p=i+d[a[i]];
        for(int j=i+1;j<=p;j++){
            if(b[j]<0 || j+d[a[j]]<p){
                k++;
            }
        }

    }
    cout<<k;
}


#include<iostream>
using namespace std;
int main()
{
    int n,i,j,ans=0,k;
    cin>>n;
    int a[2*n];
    for(i=0;i<2*n;i++)
        cin>>a[i];
    for(i=0;i<2*n;i+=2)
    {
        j=i+1;
        while(a[j]!=a[i])
            j++;
        for(k=j;k>i;k--)
            a[k]=a[k-1];
        ans+=j-i-1;
    }
    cout<<ans;
}


#include<iostream>
using namespace std;
int main()
{
    int n,i,j,ans=0,k;
    cin>>n;
    int a[2*n];
    for(i=0;i<2*n;i++)
        cin>>a[i];
    for(i=0;i<n;i+=2)
    {
        j=i+1;
        while(a[j]!=a[i])
            j++;
        for(k=j;k>i;k--)
            a[k]=a[k-1];
        ans+=j-i-1;
    }
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
    int n;
    scanf("%d",&n);
    n*=2;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
        int ans=0;
        for(int i=n;i>=1;i-=2)
            for(int j=i-1;j>1;j--)
        {
            if(a[i]==a[j]&&i==j+1)
                continue;
            else if(a[i]==a[j])
            {
                for(int k=j;k<i-1;k++){
                    swap(a[k],a[k+1]);
                ans++;
                }
            }
        }
        printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
    int n;
    scanf("%d",&n);
    n*=2;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
        int ans=0;
        for(int i=n;i>=1;i-=2){
                if(a[i]==a[i-1])
                continue;
            for(int j=i-1;j>=1;j--)
        {
            if(a[i]==a[j])
            {
                for(int k=j;k<i-1;k++){
                    swap(a[k],a[k+1]);
                ans++;
                }
            }
        }
        }
        printf("%d\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main()
{
    int n;
    scanf("%d",&n);
    n*=2;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
        int ans=0;
        for(int i=n;i>=1;i-=2)
            for(int j=i-1;j>=1;j--)
        {
            if(a[i]==a[j]&&i==j+1)
                continue;
            else if(a[i]==a[j])
            {
                for(int k=j;k<i-1;k++){
                    swap(a[k],a[k+1]);
                ans++;
                }
            }
        }
        printf("%d\n",ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 200;
int n, a[MAXN + 5], ind;
long long ans;

int main() {
    cin >> n;
    for(int i = 0; i < 2*n; i++)
        cin >> a[i];

    for(int i = 0; i < 2*n; i++)
    {
        if((i==0 || a[i]!=a[i-1]) && (i==2*n-1 || a[i]!=a[i+1]))
        {
            for(int j = i+1; j<2*n; j++)
                if(a[j]==a[i])
                {
                    ind = j;
                    break;
                }
            for(int j = ind; j>i+1; j--)
            {
                swap(a[j], a[j-1]);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100;
int n, a[MAXN + 5], ans, ind;

int main() {
    cin >> n;
    for(int i = 0; i < 2*n; i++)
        cin >> a[i];

    for(int i = 0; i < 2*n; i++)
    {
        if((i==0 || a[i]!=a[i-1]) && (i==2*n-1 || a[i]!=a[i+1]))
        {
            for(int j = i+1; j<2*n; j++)
                if(a[j]==a[i])
                {
                    ind = j;
                    break;
                }
            for(int j = ind; j>i+1; j--)
            {
                swap(a[j], a[j-1]);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100;
int n, a[MAXN + 5], ans, ind;

int main() {
    cin >> n;
    for(int i = 0; i < 2*n; i++)
    {
        cin >> a[i];
    }
    for(int i = 0; i < 2*n-1; i++)
    {
        if(a[i]!=a[i-1] && a[i]!=a[i+1])
        {
            for(int j = i+1; j<2*n; j++)
                if(a[j]==a[i])
                {
                    ind = j;
                    break;
                }
            for(int j = ind; j>i+1; j--)
            {
                swap(a[j], a[j-1]);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100;
int n, a[MAXN + 5], ind;
long long ans;

int main() {
    cin >> n;
    for(int i = 0; i < 2*n; i++)
        cin >> a[i];

    for(int i = 0; i < 2*n; i++)
    {
        if((i==0 || a[i]!=a[i-1]) && (i==2*n-1 || a[i]!=a[i+1]))
        {
            for(int j = i+1; j<2*n; j++)
                if(a[j]==a[i])
                {
                    ind = j;
                    break;
                }
            for(int j = ind; j>i+1; j--)
            {
                swap(a[j], a[j-1]);
                ans++;
            }
        }
    }

    cout << ans << endl;
    return 0;
}


#include <iostream>

using namespace std;
const int Nmax = 105;
int n, a[Nmax], sol;
int findPair(int nr, int poz)
{
    for(int i = poz + 1;i <= 2 * n; ++ i)
        if(a[i] == nr)
            return i;
}
int main()
{
    cin >> n;
    for(int i = 1;i <= 2 * n; ++ i)cin >> a[i];
    for(int i = 1;i <= 2 * n; ++ i)
    {
        if(a[i-1] != a[i])
        {
            int nextpoz = findPair(a[i], i);
            while(nextpoz > i + 1)
            {
                swap(a[nextpoz], a[nextpoz-1]);
                -- nextpoz;
                ++ sol;
            }
        }
    }
    cout << sol;
    return 0;
}


#include <iostream>

using namespace std;
const int Nmax = 205;
int n, a[Nmax], sol;
int findPair(int nr, int poz)
{
    for(int i = poz + 1;i <= 2 * n; ++ i)
        if(a[i] == nr)
            return i;
}
int main()
{
    cin >> n;
    for(int i = 1;i <= 2 * n; ++ i)cin >> a[i];
    for(int i = 1;i <= 2 * n; ++ i)
    {
        if(a[i-1] != a[i])
        {
            int nextpoz = findPair(a[i], i);
            while(nextpoz > i + 1)
            {
                swap(a[nextpoz], a[nextpoz-1]);
                -- nextpoz;
                ++ sol;
            }
        }
    }
    cout << sol;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int a[110];

int main()
{
	int n,ans=0;
	cin >> n;
	for(int i = 0;i<2*n;i++)
	{
		cin >> a[i];
	}
	for(int i = 0;i<2*n;)
	{
		for(int j = i+1;j<2*n;j++)
		{
			if(a[i] == a[j])
			{
				int temp = a[j];
				for(int k = i+2;k<j;k++)
				{
					a[k] = a[k-1];
				}
				a[i+1] = temp;
				ans += j-(i+1);
				break;
			}
		}
		i += 2;
	}
	cout << ans << endl;
}


#include<bits/stdc++.h>
using namespace std;
int a[110];
int main()
{
	int n,ans=0;
	cin >> n;
	for(int i = 0;i<2*n;i++)
	{
		cin >> a[i];
	}
	for(int i = 0;i<2*n;)
	{
		for(int j = i+1;j<2*n;j++)
		{
			if(a[i] == a[j])
			{
				int temp=a[j];;
				for(int k = j;k>=i+2;k--)
				{
					a[k] = a[k-1];
				}
				a[i+1] = temp;
				ans += j-(i+1);
				break;
			}
		}
		i += 2;
	}
	cout << ans << endl;
}


using System;
using System.Collections.Generic;

namespace tr13 {
    internal class Program {
        private static void Main(string[] args) {
            string str = Console.ReadLine();
            string[] ss1 = str.Split(' ');
            int a = int.Parse(ss1[0]);
            int b = int.Parse(ss1[1]);
            int n = int.Parse(Console.ReadLine());
            SortedDictionary<int, int> result = new SortedDictionary<int, int>();
            int min = Math.Min(a, b);
            for (int i = 1; i <= Math.Sqrt(min); i++) {
                if (a%i == 0 && b%i == 0) {
                    result.Add(i, 0);
                }
                if (a % i == 0) {
                    int t = a/i;
                    if (a%t == 0 && b%t == 0 && !result.ContainsKey(t)) result.Add(t, 0);
                }
                if (b % i == 0) {
                    int t = b/i;
                    if (a%t == 0 && b%t == 0 && !result.ContainsKey(t)) result.Add(t, 0);
                }
            }
            int[] cache = new int[result.Count];
            int count = 0;
            foreach (KeyValuePair<int, int> pair in result) {
                cache[count] = pair.Key;
                count++;
            }
            for (int i = 0; i < n; i++) {
                string s = Console.ReadLine();
                string[] ss = s.Split(' ');
                int low = int.Parse(ss[0]);
                int high = int.Parse(ss[1]);

                int ans = -1;

                int lowIndex = Search(cache, low);
                int highIndex = Search(cache, high);

                if (lowIndex == cache.Length) {
                    Console.WriteLine(ans);
                    continue;
                }
                if (highIndex == 0 && cache[0] > high) {
                    Console.WriteLine(ans);
                    continue;
                }
                if (highIndex == cache.Length) highIndex--;
                else if (cache[highIndex] != high) {
                    highIndex--;
                    if (highIndex < 0) {
                        Console.WriteLine(ans);
                        continue;
                    }
                }
                if (lowIndex <= highIndex)
                    Console.WriteLine(cache[highIndex]);
                else Console.WriteLine(-1);
            }
        }

        private static int Search(int[] a, int x) {
            int first = 0;
            int last = a.Length;

            if (a[0] > x) return 0;
            if (a[last - 1] < x) return last;

            while (first < last) {
                int mid = first + (last - first)/2;

                if (x <= a[mid])
                    last = mid;
                else
                    first = mid + 1;
            }

            if (a[last] == x) return last;
            return last;
        }
    }
}


using System;
using System.Collections.Generic;

namespace tr13 {
    internal class Program {
        private static void Main(string[] args) {
            string str = Console.ReadLine();
            string[] ss1 = str.Split(' ');
            int a = int.Parse(ss1[0]);
            int b = int.Parse(ss1[1]);
            int n = int.Parse(Console.ReadLine());
            SortedDictionary<int, int> result = new SortedDictionary<int, int>();
            int min = Math.Min(a, b);
            for (int i = 1; i <= Math.Sqrt(min); i++) {
                if (a%i == 0 && b%i == 0) {
                    result.Add(i, 0);
                    int t = a/i;
                    if (a%t == 0 && b%t == 0 && !result.ContainsKey(t)) result.Add(t, 0);
                    t = b/i;
                    if (a%t == 0 && b%t == 0 && !result.ContainsKey(t)) result.Add(t, 0);
                }
            }
            int[] cache = new int[result.Count];
            int count = 0;
            foreach (KeyValuePair<int, int> pair in result) {
                cache[count] = pair.Key;
                count++;
            }
            for (int i = 0; i < n; i++) {
                string s = Console.ReadLine();
                string[] ss = s.Split(' ');
                int low = int.Parse(ss[0]);
                int high = int.Parse(ss[1]);

                int ans = -1;

                int lowIndex = Search(cache, low);
                int highIndex = Search(cache, high);

                if (lowIndex == cache.Length) {
                    Console.WriteLine(ans);
                    continue;
                }
                if (highIndex == 0 && cache[0] > high) {
                    Console.WriteLine(ans);
                    continue;
                }
                if (highIndex == cache.Length) highIndex--;
                else if (cache[highIndex] != high) {
                    highIndex--;
                    if (highIndex < 0) {
                        Console.WriteLine(ans);
                        continue;
                    }
                }
                if (lowIndex <= highIndex)
                    Console.WriteLine(cache[highIndex]);
                else Console.WriteLine(-1);
            }
        }

        private static int Search(int[] a, int x) {
            int first = 0;
            int last = a.Length;

            if (a[0] > x) return 0;
            if (a[last - 1] < x) return last;

            while (first < last) {
                int mid = first + (last - first)/2;

                if (x <= a[mid])
                    last = mid;
                else
                    first = mid + 1;
            }

            if (a[last] == x) return last;
            return last;
        }
    }
}


using System;
using System.Collections.Generic;

namespace tr12 {
    internal class Program {
        private static void Main(string[] args) {
            string name = Console.ReadLine();
            int n = int.Parse(Console.ReadLine());
            Dictionary<string, int> peoples = new Dictionary<string, int>();
            for (int i = 0; i < n; i++) {
                string str = Console.ReadLine();
                string[] ss = str.Split(' ');
                string first = ss[0];
                string second = ss[ss.Length - 2];
                second = second.Remove(second.Length - 2, 2);
                if (!first.Equals(name) && !peoples.ContainsKey(first)) peoples.Add(first, 0);
                if (!second.Equals(name) && !peoples.ContainsKey(second)) peoples.Add(second, 0);
                if (first.Equals(name) && !second.Equals(name)) peoples[second] += GetValue(ss[1]);
                if (!first.Equals(name) && second.Equals(name)) peoples[first] += GetValue(ss[1]);
            }
            string[] names = new string[peoples.Count];
            int[] nums = new int[peoples.Count];
            int count = 0;
            foreach (KeyValuePair<string, int> pair in peoples) {
                names[count] = pair.Key;
                nums[count] = pair.Value;
                count++;
            }
            for (int i = 0; i < names.Length; i++) {
                for (int j = i + 1; j < names.Length; j++) {
                    if (nums[i] < nums[j] || nums[i] == nums[j] && string.Compare(names[i], names[j]) == 1) {
                        string tempStr = names[i];
                        names[i] = names[j];
                        names[j] = tempStr;

                        int temp = nums[i];
                        nums[i] = nums[j];
                        nums[j] = temp;
                    }
                }
            }
            for (int i = 0; i < names.Length; i++) {
                Console.WriteLine(names[i]);
            }
        }

        private static int GetValue(string str) {
            if (str.Equals("posted")) {
                return 15;
            }
            if (str.Equals("commented")) {
                return 10;
            }
            if (str.Equals("likes")) {
                return 5;
            }
            return 0;
        }
    }
}


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Resources;
using System.Text;

namespace Codeforces
{
	class Program
	{
		static void Main(string[] args)
		{
			string input1 = Console.ReadLine();
			string input2 = Console.ReadLine();

			//string input3 = Console.ReadLine();
			//string input4 = Console.ReadLine();

			var inputs1 = input1.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
			//var inputs2 = input2.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();

			long a = inputs1[0];
			long b = inputs1[1];
			long n = long.Parse(input2);

			var list = new List<Tuple<long,long>>();
			for (long i = 0; i < n; i++)
			{
				var inputs = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
				list.Add(new Tuple<long, long>(inputs[0], inputs[1]));
			}

			var min = Math.Min(a, b);
			var ds = new List<long>();
			for (long i = 2; i <= min / 2; i++)
			{
				if (a % i == 0 && b % i == 0)
				{
					ds.Add(i);
				}
			}

			if (a % min == 0 && b % min == 0)
			{
				ds.Add(min);
			}

			var results = new List<long>();
			foreach (var item in list)
			{
				var dss = ds.Where(i => i >= item.Item1 && i <= item.Item2).ToList();
				if (dss.Count > 0)
				{
					results.Add(dss.FirstOrDefault());
				}
				else
				{
					results.Add(-1);
				}
			}

			foreach (var result in results)
			{
				Console.WriteLine(result);
			}
		}

		public static void Print(int[] number)
		{
			var stringBuilder = new StringBuilder();
			foreach (var i in number)
			{
				if (i == 0 && stringBuilder.Length == 0)
				{
					continue;
				}

				stringBuilder.Append(i);
			}

			Console.WriteLine(stringBuilder.ToString());
		}

		public void Method1(ref int n1, ref int n2, ref int k1, ref int k2)
		{
			double a = (double)n1 / k1;
			double b = (double)n2 / k2;
			if (n1 >= n2 || (a >= b && a < 2 * b))
			{

			}
		}

		public void Method2(ref int n1, ref int n2, ref int k1, ref int k2)
		{
			double a = (double)n1 / k1;
			double b = (double)n2 / k2;
			if (n2 >= n1 || (b >= a && b < 2 * a))
			{

			}
		}

		public class Node
		{
			public int Count { get; set; }

			public int FirstIndex { get; set; } = -1;

			public int LastIndex { get; set; } = -1;
		}

		//public class Node<T>
		//{
		//	public Node<T> Left { get; set; }

		//	public Node<T> Right { get; set; }

		//	public T Value { get; set; }

		//	public Node(T value)
		//	{
		//		Value = value;
		//	}
		//}

		//public class Tree<T>
		//{
		//	public Node<T> Root { get; set; }

		//	public Tree(T value)
		//	{
		//		Root = new Node<T>(value);
		//	}
		//}

		private static long GCD(long a, long b)
		{
			long max = Math.Max(a, b);
			long min = Math.Min(a, b);

			if (min == 0)
			{
				return max;
			}

			a = min;
			b = max % min;
			return GCD(a, b);
		}

		private static long LCM(long a, long b)
		{
			return Math.Abs(a * b) / GCD(a, b);
		}

		//private static int Length(short[] a)
		//{
		//	int firstItemIndex = 0;
		//	for (int i = 0; i < a.Length; i++)
		//	{
		//		if (a[i] == 0)
		//		{
		//			continue;
		//		}

		//		firstItemIndex = i;
		//		break;
		//	}

		//	return a.Length - firstItemIndex;
		//}

		//private static short[] Subtract(short[] a, short[] b)
		//{
		//	if (a.Length != b.Length)
		//	{
		//		throw new ArgumentException();
		//	}

		//	short[] result = new short[a.Length];
		//	Array.Copy(a, result, a.Length);
		//	for (int i = result.Length - 1; i >= 0; i--)
		//	{
		//		if (result[i] < b[i])
		//		{
		//			int j = i - 1;
		//			while (result[j] == 0)
		//			{
		//				result[j--] = 9;
		//			}

		//			result[j]--;
		//			result[i] += 10;
		//		}

		//		result[i] -= b[i];
		//	}

		//	return result;
		//}

		//private static short[] Multiply(short[] a, int b)
		//{
		//	short[] result = new short[a.Length];
		//	for (int i = a.Length - 1; i > 0; i--)
		//	{
		//		int temp = a[i] * b;
		//		result[i] = (short)(result[i] + temp % 10);
		//		result[i - 1] = (short)(result[i - 1] + temp / 10);
		//	}

		//	return result;
		//}

		//private static int Mod(short[] a, int b)
		//{
		//	int rest = 0, i = 0;
		//	while (i < a.Length)
		//	{
		//		int d = 10 * rest + a[i];
		//		while (d < b && i < a.Length - 1)
		//		{
		//			d = 10 * d + a[++i];
		//		}

		//		rest = d % b;
		//		i++;
		//	}

		//	return rest;
		//}

		//private static short[] Divide(short[] a, int b)
		//{
		//	short[] result = new short[a.Length];
		//	int rest = 0, i = 0;
		//	while (i < a.Length)
		//	{
		//		int d = 10 * rest + a[i];
		//		while (d < b && i < a.Length - 1)
		//		{
		//			d = 10 * d + a[++i];
		//		}

		//		result[i] = (short)(d / b);
		//		rest = d % b;
		//		i++;
		//	}

		//	return result;
		//}
	}
}


using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Resources;
using System.Text;

namespace Codeforces
{
	class Program
	{
		static void Main(string[] args)
		{
			string input1 = Console.ReadLine();
			string input2 = Console.ReadLine();

			//string input3 = Console.ReadLine();
			//string input4 = Console.ReadLine();

			var inputs1 = input1.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
			//var inputs2 = input2.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();

			long a = inputs1[0];
			long b = inputs1[1];
			long n = long.Parse(input2);

			var list = new List<Tuple<long,long>>();
			for (long i = 0; i < n; i++)
			{
				var inputs = Console.ReadLine().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(long.Parse).ToArray();
				list.Add(new Tuple<long, long>(inputs[0], inputs[1]));
			}

			var gcd = GCD(a, b);
			var ds = new List<long>();
			int index = 0;
			long border = gcd;
			for (long i = 2; i < border; i++)
			{
				if (gcd % i == 0)
				{
					ds.Insert(index, gcd / i);
					ds.Insert(index, i);
					i++;
					border = gcd / i;
				}
			}

			ds.Add(gcd);

			var results = new List<long>();
			foreach (var item in list)
			{
				var dss = ds.Where(i => i >= item.Item1 && i <= item.Item2).ToList();
				if (dss.Count > 0)
				{
					results.Add(dss.LastOrDefault());
				}
				else
				{
					results.Add(-1);
				}
			}

			foreach (var result in results)
			{
				Console.WriteLine(result);
			}
		}

		public static void Print(int[] number)
		{
			var stringBuilder = new StringBuilder();
			foreach (var i in number)
			{
				if (i == 0 && stringBuilder.Length == 0)
				{
					continue;
				}

				stringBuilder.Append(i);
			}

			Console.WriteLine(stringBuilder.ToString());
		}

		public void Method1(ref int n1, ref int n2, ref int k1, ref int k2)
		{
			double a = (double)n1 / k1;
			double b = (double)n2 / k2;
			if (n1 >= n2 || (a >= b && a < 2 * b))
			{

			}
		}

		public void Method2(ref int n1, ref int n2, ref int k1, ref int k2)
		{
			double a = (double)n1 / k1;
			double b = (double)n2 / k2;
			if (n2 >= n1 || (b >= a && b < 2 * a))
			{

			}
		}

		public class Node
		{
			public int Count { get; set; }

			public int FirstIndex { get; set; } = -1;

			public int LastIndex { get; set; } = -1;
		}

		//public class Node<T>
		//{
		//	public Node<T> Left { get; set; }

		//	public Node<T> Right { get; set; }

		//	public T Value { get; set; }

		//	public Node(T value)
		//	{
		//		Value = value;
		//	}
		//}

		//public class Tree<T>
		//{
		//	public Node<T> Root { get; set; }

		//	public Tree(T value)
		//	{
		//		Root = new Node<T>(value);
		//	}
		//}

		private static long GCD(long a, long b)
		{
			long max = Math.Max(a, b);
			long min = Math.Min(a, b);

			if (min == 0)
			{
				return max;
			}

			a = min;
			b = max % min;
			return GCD(a, b);
		}

		private static long LCM(long a, long b)
		{
			return Math.Abs(a * b) / GCD(a, b);
		}

		//private static int Length(short[] a)
		//{
		//	int firstItemIndex = 0;
		//	for (int i = 0; i < a.Length; i++)
		//	{
		//		if (a[i] == 0)
		//		{
		//			continue;
		//		}

		//		firstItemIndex = i;
		//		break;
		//	}

		//	return a.Length - firstItemIndex;
		//}

		//private static short[] Subtract(short[] a, short[] b)
		//{
		//	if (a.Length != b.Length)
		//	{
		//		throw new ArgumentException();
		//	}

		//	short[] result = new short[a.Length];
		//	Array.Copy(a, result, a.Length);
		//	for (int i = result.Length - 1; i >= 0; i--)
		//	{
		//		if (result[i] < b[i])
		//		{
		//			int j = i - 1;
		//			while (result[j] == 0)
		//			{
		//				result[j--] = 9;
		//			}

		//			result[j]--;
		//			result[i] += 10;
		//		}

		//		result[i] -= b[i];
		//	}

		//	return result;
		//}

		//private static short[] Multiply(short[] a, int b)
		//{
		//	short[] result = new short[a.Length];
		//	for (int i = a.Length - 1; i > 0; i--)
		//	{
		//		int temp = a[i] * b;
		//		result[i] = (short)(result[i] + temp % 10);
		//		result[i - 1] = (short)(result[i - 1] + temp / 10);
		//	}

		//	return result;
		//}

		//private static int Mod(short[] a, int b)
		//{
		//	int rest = 0, i = 0;
		//	while (i < a.Length)
		//	{
		//		int d = 10 * rest + a[i];
		//		while (d < b && i < a.Length - 1)
		//		{
		//			d = 10 * d + a[++i];
		//		}

		//		rest = d % b;
		//		i++;
		//	}

		//	return rest;
		//}

		//private static short[] Divide(short[] a, int b)
		//{
		//	short[] result = new short[a.Length];
		//	int rest = 0, i = 0;
		//	while (i < a.Length)
		//	{
		//		int d = 10 * rest + a[i];
		//		while (d < b && i < a.Length - 1)
		//		{
		//			d = 10 * d + a[++i];
		//		}

		//		result[i] = (short)(d / b);
		//		rest = d % b;
		//		i++;
		//	}

		//	return result;
		//}
	}
}