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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author littlesheep
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskE solver = new TaskE();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskE {
        private int[] dp1;
        private int[] dp2;
        private Edge[] first;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt();
            first = new Edge[n];
            dp1 = new int[n];
            dp2 = new int[n];

            for (int i = 0; i < n - 1; ++i) {
                int u = in.nextInt() - 1;
                int v = in.nextInt() - 1;
                new Edge(u, v);
                new Edge(v, u);
            }

            dfs1(0, -1);
            dfs2(0, -1);
            int maxDist = Integer.MAX_VALUE;
            for (int i = 0; i < n; ++i) {
                dp2[i] = Math.max(dp2[i], dp1[i]);
                if (dp2[i] < maxDist) {
                    maxDist = dp2[i];
                }
            }
//        System.err.print(Arrays.toString(dp2));
            ArrayList<Integer> roots = new ArrayList<>();
            for (int i = 0; i < n; ++i) {
                if (dp2[i] == maxDist) {
                    roots.add(i);
                }
            }
            if (roots.size() == 2) {
                int u = dfs(roots.get(0), roots.get(1));
                int v = dfs(roots.get(1), roots.get(0));
                if (u != -1 && u == v) {
                    out.println(maxDist * 2 - 1);
                } else {
                    out.println(-1);
                }
            } else {
                boolean noSolution = false;
                TreeSet<Integer> children = new TreeSet<>();
                int root = roots.get(0);
//            System.err.println("root = " + root);
                for (Edge e = first[root]; e != null; e = e.next) {
                    int cl = dfs(e.to, root) + 1;
//                System.err.println("CL = " + cl);
                    if (cl == 0) {
                        noSolution = true;
                        break;
                    }
                    children.add(cl);
                }
                if (children.size() > 2) {
                    noSolution = true;
                }
                if (noSolution) {
                    out.println(-1);
                    return;
                }

                int value = children.first() + children.last();
                while (value % 2 == 0) {
                    value /= 2;
                }
                out.println(value);
            }
        }

        private int dfs(int x, int fa) {
            int childLength = -1;
            for (Edge e = first[x]; e != null; e = e.next) {
                if (e.to == fa) {
                    continue;
                }
                int cl2 = dfs(e.to, x) + 1;
                if (cl2 == 0) {
                    return -1;
                }
                if (childLength == -1) {
                    childLength = cl2;
                }
                if (cl2 != childLength) {
                    return -1;
                }
            }
            return childLength == -1 ? 0 : childLength;
        }

        private void dfs1(int x, int fa) {
            dp1[x] = 0;
            for (Edge e = first[x]; e != null; e = e.next) {
                if (e.to == fa) {
                    continue;
                }
                dfs1(e.to, x);
                dp1[x] = Math.max(dp1[x], dp1[e.to] + 1);
            }
        }

        private void dfs2(int x, int fa) {
            List<Integer> children = new ArrayList<>();

            for (Edge e = first[x]; e != null; e = e.next) {
                if (e.to == fa) {
                    continue;
                }
                children.add(e.to);
            }

            int current = dp2[x];
            for (Integer child : children) {
                dp2[child] = Math.max(dp2[child], current + 1);
                current = Math.max(current, dp1[child] + 1);
            }

            current = dp2[x];
            for (int i = children.size() - 1; i >= 0; --i) {
                int child = children.get(i);
                dp2[child] = Math.max(dp2[child], current + 1);
                current = Math.max(current, dp1[child] + 1);
            }

            for (Integer child : children) {
                dfs2(child, x);
            }
        }

        class Edge {
            int to;
            Edge next;

            Edge(int from, int to) {
                this.to = to;
                this.next = first[from];
                first[from] = this;
            }

        }

    }

    static class InputReader {
        BufferedReader reader;
        StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream));
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (Exception e) {
                    throw new UnknownError();
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}

