10
4 2
7 4
2 6
2 5
4 8
10 3
2 9
9 1
5 10
 10
5 10
7 8
8 3
2 6
3 2
9 7
4 5
10 1
6 4
 4
4 1
4 3
4 2
 3
3 1
1 2
 9
1 2
1 3
1 4
1 5
1 6
6 7
6 8
8 9
 5
1 5
2 3
2 4
1 2
 8
6 1
4 7
4 8
8 5
7 6
4 3
4 2
 7
5 6
5 7
5 1
7 4
6 3
3 2
 3
1 3
3 2
 2
1 2
 5
5 4
4 3
3 1
5 2
 6
1 6
3 1
6 4
5 3
2 5
 11
8 9
2 7
1 11
3 2
9 1
8 5
8 6
5 4
4 10
8 3
 11
11 9
6 7
7 1
8 11
5 6
3 5
9 3
10 8
2 4
4 10
 12
12 6
6 7
8 11
4 8
10 4
12 3
2 10
6 2
12 9
4 1
9 5

-1
 9
 1
 1
 3
 3
 -1
 -1
 1
 1
 1
 5
 1
 5
 -1

import java.util.*;

public class ProbE {
	
	static long scount = 0;
	
	public static void main(String[] args) {
		ProbE sol = new ProbE();
		sol.start();
	}

	public void print(Object o){ 
//		System.out.print(o); 
	}
	public void println(Object o) {
//		System.out.println(o); 
	}

	public void start(){
//		ProbE.scount = 0;
//		test();
		submit();
	}
	
	class GraphNode{
		int idx;
		HashSet<GraphNode> nhbr;
		Verge verge;
		public GraphNode(int i) {
			idx = i;
			nhbr = new HashSet();
			verge = null;
		}
		
	}
	
	class SortedVerge implements Comparable<SortedVerge>{
		int size;
		Verge verge;
		public SortedVerge(int s, Verge v) { size = s; verge = v; }
		public int compareTo(SortedVerge v){
			if(size==v.size) return 0;
			else if(size>v.size) return -1;
			else return 1;
		}
	}
	
	class Verge{
		GraphNode verge;
		GraphNode prev; 
			//prev should not participate in equals() and hashCode()
		int vergedist;
		public Verge(GraphNode v, GraphNode p, int d){ verge = v; prev = p; vergedist = d; }
		public boolean equals(Object o){
			if(o instanceof Verge){
				Verge v = (Verge) o;
				return (verge==v.verge && vergedist==v.vergedist);
			}
			return false;
		}
		public int hashCode(){ return vergedist * verge.idx; }
		public String toString() { return "("+verge.idx+","+vergedist+")"; }
	}
	
	public void test(){
		scount = 0;
		int n = 5000;
		n *= 40;
		int[][] edges = new int[n-1][2];
		edges[0][0] = 0;
		edges[0][1] = 1;
		for(int i=1;i<n-1;i++){
			edges[i][0] = i + 1;
			edges[i][1] = (int) (Math.random() * (i+1));
		}
//		for(int[] edge : edges) System.out.println(edges[i][0]+" "+edges[i][1]);
		int ret = merge(edges);
		System.out.println(ret);
		System.out.println(" "+n+","+scount);
	}
	
	public void submit(){
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[][] edges = new int[n-1][2];
		for(int i=0;i<n-1;i++){
			edges[i][0] = scan.nextInt() - 1;
			edges[i][1] = scan.nextInt() - 1;
		}
		scan.close();
		
		int ret = merge(edges);
		System.out.println(ret);
		System.out.flush();
	}
	
	public int merge(int[][] edges){
		boolean debug = false;
		int n = edges.length + 1;
		GraphNode[] nodes = new GraphNode[n];
		for(int i=0;i<n;i++){
			nodes[i] = new GraphNode(i);
		}
		for(int i=0;i<n-1;i++){
			int j = edges[i][0];
			int k = edges[i][1];
			beNeighbour(nodes[j], nodes[k]);
		}
		
		HashMap<Verge, HashSet<GraphNode>> map = new HashMap();
		PriorityQueue<SortedVerge> svq = new PriorityQueue();
		HashMap<Verge, SortedVerge> svs = new HashMap();
		for(GraphNode node : nodes) if(node.nhbr.size()==1) {
			findVerge(node);
			HashSet<GraphNode> set = map.get(node.verge);
			if(set==null){
				set = new HashSet();
				map.put(node.verge, set);
			}
//			scount += 1;
			set.add(node);
		}
		
		for(Map.Entry<Verge, HashSet<GraphNode>> entry : map.entrySet()){
//			scount += 1;
			Verge v = entry.getKey();
			HashSet<GraphNode> set = entry.getValue();
			SortedVerge sv = new SortedVerge(set.size(), v);
			svq.add(sv);
			svs.put(v, sv);
		}
		
		while(true){
			if(debug){
				for(Verge tmp : map.keySet()){
					print("("+tmp.verge.idx+","+tmp.vergedist+"): " + " {");
					HashSet<GraphNode> set = map.get(tmp);
					for(GraphNode node : set) print(" "+node.idx+" ");
					println("}");
				}
			}
			Verge nextVerge = null;
			
			
			SortedVerge sVerge = svq.poll();
			svs.remove(sVerge.verge);
			if(sVerge.size>=2) nextVerge = sVerge.verge;
			
//			for(Map.Entry<Verge, HashSet<GraphNode>> entry : map.entrySet()){
//				scount += 1;
//				if(entry.getValue().size()>=2){
//					nextVerge = entry.getKey();
//					break;
//				}
//			}
			
			if(nextVerge==null){
				if(map.size()!=2) return -1;
				Integer ret = null;
				for(Verge verge : map.keySet()){
//					scount += 1;
					if(ret==null || ret<verge.vergedist) ret = verge.vergedist;
				}
				while(ret%2==0) ret /= 2;
				return ret;
			}
			
			HashSet<GraphNode> set = map.get(nextVerge);
			if(map.size()==1 && set.size()==2){
				int ret = nextVerge.vergedist;
				while(ret%2==0) ret /= 2;
				return ret;
			}
			
			GraphNode firstNode = null;
			for(GraphNode node : set){
//				scount += 1;
				if(firstNode==null) firstNode = node;
				else removeToVerge(node);
			}
			findVerge(firstNode, firstNode.verge);
			print("firstNode="+firstNode.idx+" verge=");
			println(firstNode.verge);
			map.remove(nextVerge);
			set = map.get(firstNode.verge);
			if(set==null){
				set = new HashSet();
				map.put(firstNode.verge, set);
			}
//			scount += 1;
			set.add(firstNode);
			
			sVerge = svs.get(firstNode.verge);
			if(sVerge!=null){
				svq.remove(sVerge);
				sVerge.size += 1;
			}
			else sVerge = new SortedVerge(1, firstNode.verge);
			svs.put(firstNode.verge, sVerge);
			svq.add(sVerge);
		}
		
	}
	
	private void beNeighbour(GraphNode a, GraphNode b){ a.nhbr.add(b); b.nhbr.add(a); }
	private GraphNode removeEnd(GraphNode end){
		if(end.nhbr.size()!=1){
			System.out.println("Wrong in removeEnd");
			System.exit(-1);
		}
		GraphNode next = null;
		for(GraphNode tmp : end.nhbr) {next = tmp;}
		end.nhbr.remove(next);
		next.nhbr.remove(end);
//		scount += 1;
		return next;
	}
	private void removeToVerge(GraphNode end){
		while(end.nhbr.size()==1) {
			end = removeEnd(end);
//			scount += 1;
		}
	}
	
	private void findVerge(GraphNode node, Verge oldVerge){
		int vergedist = oldVerge.vergedist;
		GraphNode cur = oldVerge.verge, prev = oldVerge.prev;
		while(cur.nhbr.size()==2){
//			scount += 1;
			vergedist += 1;
			GraphNode next = null;
			for(GraphNode tmp : cur.nhbr) if(tmp!=prev) next = tmp;
			prev = cur;
			cur = next;
		}
		println("finding verge for "+node.idx);
		println("cur="+cur.idx+", size="+cur.nhbr.size());
		node.verge = new Verge(cur, prev, vergedist);
	}
	
	private void findVerge(GraphNode node){
		int vergedist = 1;
		GraphNode cur = null, prev = node;
		for(GraphNode tmp : node.nhbr) cur = tmp;
		while(cur.nhbr.size()==2){
//			scount += 1;
			vergedist += 1;
			GraphNode next = null;
			for(GraphNode tmp : cur.nhbr) if(tmp!=prev) next = tmp;
			prev = cur;
			cur = next;
		}
		println("finding verge for "+node.idx);
		println("cur="+cur.idx+", size="+cur.nhbr.size());
		node.verge = new Verge(cur, prev, vergedist);
	}
	
	public int mergeBackup(int[][] edges){
		boolean debug = false;
		int n = edges.length + 1;
		GraphNode[] nodes = new GraphNode[n];
		for(int i=0;i<n;i++){
			nodes[i] = new GraphNode(i);
		}
		for(int i=0;i<n-1;i++){
			int j = edges[i][0];
			int k = edges[i][1];
			beNeighbour(nodes[j], nodes[k]);
		}
		
		HashMap<Verge, HashSet<GraphNode>> map = new HashMap();
		for(GraphNode node : nodes) if(node.nhbr.size()==1) {
			findVerge(node);
			HashSet<GraphNode> set = map.get(node.verge);
			if(set==null){
				set = new HashSet();
				map.put(node.verge, set);
			}
//			scount += 1;
			set.add(node);
		}
		
		while(true){
			if(debug){
				for(Verge tmp : map.keySet()){
					print("("+tmp.verge.idx+","+tmp.vergedist+"): " + " {");
					HashSet<GraphNode> set = map.get(tmp);
					for(GraphNode node : set) print(" "+node.idx+" ");
					println("}");
				}
			}
			Verge nextVerge = null;
			
			
			for(Map.Entry<Verge, HashSet<GraphNode>> entry : map.entrySet()){
				scount += 1;
				if(entry.getValue().size()>=2){
					nextVerge = entry.getKey();
					break;
				}
			}
			
			if(nextVerge==null){
				if(map.size()!=2) return -1;
				Integer ret = null;
				for(Verge verge : map.keySet()){
//					scount += 1;
					if(ret==null || ret<verge.vergedist) ret = verge.vergedist;
				}
				while(ret%2==0) ret /= 2;
				return ret;
			}
			
			HashSet<GraphNode> set = map.get(nextVerge);
			if(map.size()==1 && set.size()==2){
				int ret = nextVerge.vergedist;
				while(ret%2==0) ret /= 2;
				return ret;
			}
			
			GraphNode firstNode = null;
			for(GraphNode node : set){
//				scount += 1;
				if(firstNode==null) firstNode = node;
				else removeToVerge(node);
			}
			findVerge(firstNode, firstNode.verge);
			print("firstNode="+firstNode.idx+" verge=");
			println(firstNode.verge);
			map.remove(nextVerge);
			set = map.get(firstNode.verge);
			if(set==null){
				set = new HashSet();
				map.put(firstNode.verge, set);
			}
//			scount += 1;
			set.add(firstNode);
		}
		
	}
}