Vanya wants to minimize a tree. He can perform the following operation multiple times: choose a vertex v, and two disjoint (except for v) paths of equal length a_0 = v, a_1, ..., a_{k}, and b_0 = v, b_1, ..., b_{k}. Additionally, vertices a_1, ..., a_{k}, b_1, ..., b_{k} must not have any neighbours in the tree other than adjacent vertices of corresponding paths. After that, one of the paths may be merged into the other, that is, the vertices b_1, ..., b_{k} can be effectively erased: [Image] 

Help Vanya determine if it possible to make the tree into a path via a sequence of described operations, and if the answer is positive, also determine the shortest length of such path.


-----Input-----

The first line of input contains the number of vertices n (2 ≤ n ≤ 2·10^5).

Next n - 1 lines describe edges of the tree. Each of these lines contains two space-separated integers u and v (1 ≤ u, v ≤ n, u ≠ v) — indices of endpoints of the corresponding edge. It is guaranteed that the given graph is a tree.


-----Output-----

If it is impossible to obtain a path, print -1. Otherwise, print the minimum number of edges in a possible path.


-----Examples-----
Input
6
1 2
2 3
2 4
4 5
1 6

Output
3

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

Output
-1



-----Note-----

In the first sample case, a path of three edges is obtained after merging paths 2 - 1 - 6 and 2 - 4 - 5.

It is impossible to perform any operation in the second sample case. For example, it is impossible to merge paths 1 - 3 - 4 and 1 - 5 - 6, since vertex 6 additionally has a neighbour 7 that is not present in the corresponding path.

Difficulty: HARD
Tags: Graph traversal

Implementation

Greedy algorithms

Tree algorithms

Dynamic programming
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, sys, random, time
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20
mod = 10 ** 9 + 7

def LI():
	return list(map(int, input().split()))

def II():
	return int(input())

def LS():
	return input().split()

def S():
	return input()

def main():
	n = II()
	d = collections.defaultdict(set)
	for _ in range(n - 1):
		(a, b) = LI()
		d[a].add(b)
		d[b].add(a)
	memo = {}

	def path(t, s):
		ps = set()
		dt = list(d[t])
		for k in dt:
			if k not in memo:
				continue
			if memo[k] in ps:
				d[k] -= set([t])
				d[t] -= set([k])
			ps.add(memo[k])
		if s == -1 and len(ps) == 2:
			memo[t] = sum(ps) + 2
			return memo[t]
		if len(ps) > 1:
			return -t
		if len(ps) == 0:
			memo[t] = 0
			return 0
		memo[t] = list(ps)[0] + 1
		return memo[t]

	def _path(tt, ss):
		q = [(tt, ss)]
		tq = []
		qi = 0
		while len(q) > qi:
			(t, s) = q[qi]
			for k in d[t]:
				if k == s:
					continue
				q.append((k, t))
			qi += 1
		for (t, s) in q[::-1]:
			r = path(t, s)
			if r < 0:
				return r
		return memo[tt]

	def _path2(tt, ss):
		q = [(tt, ss)]
		tq = []
		qi = 0
		while len(q) > qi:
			(t, s) = q[qi]
			for k in d[t]:
				if k == s or k in memo:
					continue
				q.append((k, t))
			qi += 1
		for (t, s) in q[::-1]:
			r = path(t, s)
			if r < 0:
				return r
		return memo[tt]
	t = _path(1, -1)
	if t < 0:
		t = _path2(-t, -1)
	if t > 0:
		while t % 2 == 0:
			t //= 2
		return t
	return -1
print(main())