using
System;
using
System.Collections.Generic;
class
Pair
{
public
int
first;
public
int
second;
public
Pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
public
override
int
GetHashCode()
{
return
(
this
.first,
this
.second).GetHashCode();
}
public
override
bool
Equals(
object
obj)
{
if
(
this
== obj)
return
true
;
if
(!(obj
is
Pair))
return
false
;
Pair other = (Pair)obj;
return
this
.first == other.first &&
this
.second == other.second;
}
}
class
Program
{
static
void
Make(
int
i, Dictionary<Pair, Pair> parent)
{
parent[
new
Pair(i * 2, i * 2 + 1)] =
new
Pair(i * 2, i * 2 + 1);
}
static
Pair Find(Pair v, Dictionary<Pair, Pair> parent)
{
if
(parent[v].Equals(v))
return
v;
parent[v] = Find(parent[v], parent);
return
parent[v];
}
static
void
Union(Pair a, Pair b, Dictionary<Pair, Pair> parent)
{
a = Find(a, parent);
b = Find(b, parent);
if
(!a.Equals(b))
parent[b] = a;
}
static
int
Solve(
int
[] arr,
int
n)
{
var
parent =
new
Dictionary<Pair, Pair>();
for
(
int
i = 0; i < n; i++)
{
Make(i, parent);
}
for
(
int
i = 0; i < n; i++)
{
for
(
int
j = 0; j < n; j++)
{
if
(i == j)
continue
;
Pair a =
new
Pair(i * 2, i * 2 + 1);
Pair b =
new
Pair(j * 2, j * 2 + 1);
if
((arr[i * 2] / 2 == arr[j * 2] / 2) ||
(arr[i * 2] / 2 == arr[j * 2 + 1] / 2) ||
(arr[i * 2 + 1] / 2 == arr[j * 2] / 2) ||
(arr[i * 2 + 1] / 2 == arr[j * 2 + 1] / 2))
{
Union(a, b, parent);
}
}
}
var
comp =
new
Dictionary<Pair,
int
>();
for
(
int
i = 0; i < n; i++)
{
Pair component = Find(
new
Pair(i * 2, i * 2 + 1), parent);
comp[component] = comp.ContainsKey(component) ? comp[component] + 1 : 1;
}
return
n - comp.Count;
}
static
void
Main(
string
[] args)
{
int
N = 2;
int
[] arr = { 3, 0, 2, 1 };
Console.WriteLine(Solve(arr, N));
}
}