using
System;
using
System.Collections;
using
System.Collections.Generic;
class
GFG
{
static
int
MAX = 100005;
static
int
totBackEdges;
static
int
[]countAdj =
new
int
[MAX];
static
int
[]small =
new
int
[MAX];
static
int
[]isPossible =
new
int
[MAX];
static
int
[]depth =
new
int
[MAX];
static
ArrayList adj =
new
ArrayList();
static
int
[]vis =
new
int
[MAX];
class
pair
{
public
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
void
change(
ref
pair p,
int
x)
{
if
(p.second > x)
p.second = x;
if
(p.first > p.second)
{
int
tmp = p.first;
p.first = p.second;
p.second = tmp;
}
}
static
pair dfs(
int
v,
int
p = -1,
int
de = 0)
{
pair answer =
new
pair(100000000, 100000000);
depth[v] = de;
vis[v] = 1;
isPossible[v] = 1;
foreach
(
int
u
in
(ArrayList)adj[v]) {
if
((u ^ p) != 0) {
if
(vis[u] == 0) {
pair x = dfs(u, v, de + 1);
small[v] += small[u];
change(
ref
answer, x.second);
change(
ref
answer, x.first);
if
(x.second < de)
isPossible[v] = 0;
}
else
if
(vis[u] == 1) {
totBackEdges++;
countAdj[v]++;
countAdj[u]++;
small[p]++;
small[u]--;
change(
ref
answer, depth[u]);
}
}
}
vis[v] = 2;
return
answer;
}
static
int
minNodetoRemove(
int
n,
ArrayList edges)
{
for
(
int
i = 0; i < edges.Count; i++) {
((ArrayList)adj[((pair)edges[i]).first])
.Add(((pair)edges[i]).second);
((ArrayList)adj[((pair)edges[i]).second])
.Add(((pair)edges[i]).first);
}
Array.Fill(vis, 0);
totBackEdges = 0;
for
(
int
v = 1; v <= n; v++) {
if
(vis[v] == 0)
dfs(v);
}
if
(totBackEdges == 0)
return
-1;
int
node = -1;
for
(
int
v = 1; v <= n; v++) {
if
((countAdj[v] + small[v] == totBackEdges) && isPossible[v] != 0) {
node = v;
}
if
(node != -1)
break
;
}
return
node;
}
static
void
Main()
{
int
N = 5;
ArrayList edges =
new
ArrayList();
for
(
int
i = 0; i < MAX; i++)
{
adj.Add(
new
ArrayList());
}
edges.Add(
new
pair(5, 1));
edges.Add(
new
pair(5, 2));
edges.Add(
new
pair(1, 2));
edges.Add(
new
pair(2, 3));
edges.Add(
new
pair(2, 4));
Console.Write(minNodetoRemove(N, edges));
}
}