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.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.PriorityQueue;



public final class CF_397_TreeFolding{

	static void log(int[] X){
		int L=X.length;
		for (int i=0;i<L;i++){
			logWln(X[i]+" ");
		}
		log("");
	}


	static void log(long[] X){
		int L=X.length;
		for (int i=0;i<L;i++){
			logWln(X[i]+" ");
		}
		log("");
	}
	static void log(Object[] X){
		int L=X.length;
		for (int i=0;i<L;i++){
			logWln(X[i]+" ");
		}
		log("");
	}


	static void log(Object o){
		logWln(o+"\n");
	}

	static void logWln(Object o){
		System.err.print(o);
		//outputWln(o);

	}
	static void info(Object o){
		System.err.println(o);
		//output(o);
	}

	static void output(Object o){

		outputWln(""+o+"\n");

	}

	static void outputWln(Object o){
		// System.out.print(o);

		try {
			out.write(""+ o);
		} catch (Exception e) {

		}
	}

	static int[] anc,h,stack,maxH;

	static ArrayList<Integer>[] friends;
	static int N;
	static int deepest,maxDep;

	static void buildGraphStack(int root){
		// manque l'update des sommes
		anc=new int[N];
		stack=new int[2*N];
		h=new int[N];
		maxH=new int[N];
		h[root]=0;
		deepest=-1;
		maxDep=-1;
		
		int r=0;
		int w=0;
		int p=0;
		stack[p++]=root;
		anc[root]=-1;
		boolean[] visited=new boolean[N];
		while (p>0){
			int u=stack[p-1];
			//log("u:"+u+" visited[u]:"+visited[u]);
			if (!visited[u]){
				visited[u]=true;
				int L=friends[u].size();
				
				for (int v:friends[u]){
					if (v!=anc[u]){
						anc[v]=u;
						h[v]=h[u]+1;
						//log("Adding v:"+v);
						stack[p++]=v;
					}
				}
			} else {
				// on processe au retour
				for (int v:friends[u]){
					if (v!=anc[u]){
						maxH[u]=Math.max(maxH[u],1+maxH[v]);
					}
				}
				if (maxH[u]>maxDep){
					deepest=u;
					maxDep=maxH[u];
				}
				p--;
			}
		}
	}

	static void solveStack(){
		int p=0;
		HashSet<Integer> list=new HashSet<Integer>();
		stack[p++]=0;
		//HashSet<Integer>[] up=new HashSet[N];

//		up[0]=new HashSet<Integer>();
	//	up[0].add(0);
		int[] up=new int[N];
		boolean[] visited=new boolean[N];
		boolean ok=true;
		loop:while (p>0){
			
			
			int u=stack[p-1];
			//log("u:"+u+" visited[u]:"+visited[u]);
			if (!visited[u]){
				visited[u]=true;
				HashSet<Integer> tmp=new HashSet<Integer>();
				for (int v:friends[u]){
					if (v!=anc[u]){
						tmp.add(maxH[v]);
						
					}
				}
				if (u!=0)
					tmp.add(up[u]);
				if (tmp.size()>2){
					ok=false;
					break loop;
				}
				for (int v:friends[u]){
					if (v!=anc[u]){
						int max=0;
						for (int w:friends[u]){
							if (w!=v && w!=anc[u]){
								max=Math.max(max, maxH[w]);
							}
						}
						max-=h[u];
						max+=1;
						max=Math.max(max, up[u]+1);
						up[v]=max;
					}
				}
				
			} else {
				// on processe au retour
				p--;
			}
		}
		if (ok){
			//output("OK");
			// Ok 
			// now need to compute diameter of tree
			buildGraphStack(deepest);
			output(maxDep);
		
			
		} else {
			output("-1");
		}
		
		
	}



	// Global vars
	static BufferedWriter out;

	static void process() throws Exception {


		Locale.setDefault(Locale.US);

		out = new BufferedWriter(new OutputStreamWriter(System.out));
		InputReader reader=new InputReader(System.in);

		N=reader.readInt();
		friends=new ArrayList[N];
		for (int i=0;i<N;i++)
			friends[i]=new ArrayList<Integer>();
		for (int i=0;i<N-1;i++){
			int u=reader.readInt()-1;
			int v=reader.readInt()-1;
			friends[u].add(v);
			friends[v].add(u);
		}
	
		buildGraphStack(0);
		solveStack();
		
		
		try {
			out.close();
		}
		catch (Exception e){}


	}



	public static void main(String[] args) throws Exception {
		process();


	}
	static final class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		private int read() throws IOException {
			if (curChar >= numChars) {
				curChar = 0;
				numChars = stream.read(buf);
				if (numChars <= 0) {
					return -1;
				}
			}
			return buf[curChar++];
		}


		public final String readString() throws IOException {
			int c = read();
			while (isSpaceChar(c)) {
				c = read();
			}
			StringBuilder res=new StringBuilder();
			do {
				res.append((char)c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public final int readInt() throws IOException {
			int c = read();
			boolean neg=false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d=(char)c;
			if (d=='-') {
				neg=true;
				c = read();
			}
			int res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			//log("res:"+res);
			if (neg)
				return -res;
			return res;

		}

		public final long readLong() throws IOException {
			int c = read();
			boolean neg=false;
			while (isSpaceChar(c)) {
				c = read();
			}
			char d=(char)c;
			if (d=='-') {
				neg=true;
				c = read();
			}
			long res = 0;
			do {
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			if (neg)
				return -res;
			return res;

		}




		private boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}
	}

}

