using
System;
using
System.Collections.Generic;
class
GFG{
static
int
NN = (
int
)(1e5 + 5);
static
List<
int
>[] G =
new
List<
int
>[ NN ];
static
void
dfs(
int
node,
int
parent,
bool
check,
int
current_colour,
int
[] colours)
{
check = check && (colours[node] == current_colour);
foreach
(
int
a
in
G[node])
{
if
(a != parent)
{
dfs(a, node, check,
current_colour, colours);
}
}
}
static
bool
checkPossibility(
int
root,
int
[] colours)
{
bool
ans =
true
;
foreach
(
int
a
in
G[root])
{
int
current_colour = colours[a];
bool
check =
true
;
dfs(a, root, check, current_colour, colours);
ans = ans && check;
}
return
ans;
}
static
void
addedge(
int
x,
int
y)
{
G[x].Add(y);
G[y].Add(x);
}
static
void
solve(
int
[] colours,
int
N)
{
int
root1 = -1;
int
root2 = -1;
for
(
int
i = 1; i <= N; i++)
{
foreach
(
int
a
in
G[i])
{
if
(colours[a] != colours[i])
{
root1 = a;
root2 = i;
break
;
}
}
}
if
(root1 == -1)
{
Console.WriteLine(
"1"
+
"\n"
);
}
else
if
(checkPossibility(root1, colours))
{
Console.Write(root1 +
"\n"
);
}
else
if
(checkPossibility(root2, colours))
{
Console.Write(root2 +
"\n"
);
}
else
{
Console.Write(
"-1"
+
"\n"
);
}
}
public
static
void
Main(String[] args)
{
int
N = 8;
for
(
int
i = 0; i < G.Length; i++)
G[i] =
new
List<
int
>();
addedge(1, 2);
addedge(1, 3);
addedge(2, 4);
addedge(2, 7);
addedge(3, 5);
addedge(3, 6);
addedge(6, 8);
int
[] colours = {0, 1, 1, 1, 1, 1, 2, 1, 3};
solve(colours, N);
}
}