CF BUDDY
← Problems·

1182D · Complete Mirror

2400 · constructive algorithms, dfs and similar, dp

Problem: You have given tree consist of nn vertices. Select a vertex as root vertex that satisfies the condition below.

  • For all vertices v1v_{1} and v2v_{2}, if distancedistance(rootroot, v1v_{1}) =distance= distance(rootroot, v2)v_{2}) then degreedegree(v1v_{1}) =degree= degree(v2v_{2}), where degreedegree means the number of vertices connected to that vertex, and distancedistance means the number of edges between two vertices.

Determine and find if there is such root vertex in the tree. If there are multiple answers, find any of them.

Input Format: The first line contains a single integer nn (1n1051 \le n \le 10^{5}) — the number of vertices.

Each of the next n1n-1 lines contains two integers viv_{i} and uiu_{i} (1vi<uin1 \le v_{i} \lt u_{i} \le n) — it means there is an edge exist between viv_{i} and uiu_{i}. It is guaranteed that the graph forms tree.

Output Format: If there is such root vertex exists, print any of them. Otherwise, print 1-1.

Note: This is the picture for the first example. 11, 55, 77 also can be a valid answer.

This is the picture for the second example. You can see that it's impossible to find such root vertex.

Sample Cases

Case 1

Input

7
1 2
2 3
3 4
4 5
3 6
6 7

Output

3

Case 2

Input

6
1 3
2 3
3 4
4 5
4 6

Output

-1

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback