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.util.Iterator;
import java.util.Set;
import java.util.HashMap;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.util.Map;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.InputStream;

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

    static class TaskE {
        public void solve(int testNumber, MyBufferedReader in, PrintWriter out) {
            int n = in.getAnInt();
            Set<TaskE.GNode> leaves = new HashSet<>();
            TaskE.GNode[] nodes = new TaskE.GNode[n + 1];
            for (int i = 0; i <= n; i++) {
                nodes[i] = new TaskE.GNode(i);
            }

            for (int i = 0; i < n - 1; i++) {
                int[] data = in.getALineOfInts(2);
                int v1 = data[0];
                int v2 = data[1];
                TaskE.GNode v1Node = nodes[v1];
                TaskE.GNode v2Node = nodes[v2];
                if (!v1Node.ns.containsKey(1)) {
                    v1Node.ns.put(1, new HashSet<>());
                }

                v1Node.add(v2Node, 1);

                v2Node.add(v1Node, 1);
            }

            for (int i = 1; i <= n; i++) {
                if (nodes[i].numNs == 1) {
                    leaves.add(nodes[i]);
                }
            }

            int nextId = n + 1;
            Set<TaskE.GNode> remLeaves = new HashSet<>();
            int compDist = 1;
            while (leaves.size() > 0) {
                Map<TaskE.GNode, Set<TaskE.GNode>> toParents = new HashMap<>();
                for (TaskE.GNode l : leaves) {
                    TaskE.GNode parent = null;
                    for (Map.Entry<Integer, Set<TaskE.GNode>> gn : l.ns.entrySet()) {
                        if (gn.getValue().size() > 0) {
                            parent = gn.getValue().iterator().next();
                        }
                    }

                    if (!toParents.containsKey(parent)) {
                        toParents.put(parent, new HashSet<>());
                    }

                    toParents.get(parent).add(l);
                }

                for (Map.Entry<TaskE.GNode, Set<TaskE.GNode>> ls : toParents.entrySet()) {
                    TaskE.GNode parent = ls.getKey();
                    Set<TaskE.GNode> lss = ls.getValue();
                    if (lss.size() > 1) {
                        parent.removeAll(compDist);
                        leaves.removeAll(lss);
                        TaskE.GNode newLeave = new TaskE.GNode(nextId++);
                        newLeave.add(parent, compDist);
                        parent.add(newLeave, compDist);
                        leaves.add(newLeave);
                    }
                }

                Set<TaskE.GNode> newLeaves = new HashSet<>();
                for (TaskE.GNode l : leaves) {
                    compressLeave(l, newLeaves, remLeaves);
                }

                leaves = newLeaves;
                compDist++;
            }

            if (leaves.size() + remLeaves.size() > 2) {
                out.println(-1);
                return;
            }

            int pathSum = 0;
            TaskE.GNode curr = leaves.isEmpty() ? remLeaves.iterator().next() : leaves.iterator()
                .next();
            TaskE.GNode prev = null;
            do {
                TaskE.GNode other = null;
                int otherDist = 0;
                for (Map.Entry<Integer, Set<TaskE.GNode>> gn : curr.ns.entrySet()) {
                    for (TaskE.GNode nbr : gn.getValue()) {
                        if (!nbr.equals(prev)) {
                            other = nbr;
                            otherDist = gn.getKey();
                        }
                    }
                }

                pathSum += otherDist;
                prev = curr;
                curr = other;
            } while (curr.numNs > 1);

            out.println(pathSum);
        }

        private boolean compressLeave(TaskE.GNode l, Set<TaskE.GNode> leaves, Set<TaskE.GNode>
            remLeaves) {
            TaskE.GNode parent = null;
            int dist = 0;
            for (Map.Entry<Integer, Set<TaskE.GNode>> gn : l.ns.entrySet()) {
                if (gn.getValue().size() > 0) {
                    parent = gn.getValue().iterator().next();
                    dist = gn.getKey();
                }
            }

            boolean res = false;
            if (parent.numNs == 2) {
                res = true;
                parent.ns.get(dist).remove(l);
                parent.numNs--;

                TaskE.GNode grandpa = null;
                int grandpaToPa = 0;
                for (Map.Entry<Integer, Set<TaskE.GNode>> gn : parent.ns.entrySet()) {
                    if (gn.getValue().size() > 0) {
                        grandpa = gn.getValue().iterator().next();
                        grandpaToPa = gn.getKey();
                    }
                }

                grandpa.ns.get(grandpaToPa).remove(parent);
                grandpa.numNs--;
                parent.removeAll(grandpaToPa);

                leaves.add(parent);
                int newDist = grandpaToPa + dist;
                parent.add(grandpa, newDist);
                grandpa.add(parent, newDist);
            } else {
                remLeaves.add(l);
            }

            return res;
        }

        static class GNode {
            int id;
            Map<Integer, Set<TaskE.GNode>> ns;
            int numNs;

            public GNode(int id) {
                this.id = id;
                ns = new HashMap<>();
                numNs = 0;
            }

            void add(TaskE.GNode g, int dist) {
                if (!ns.containsKey(dist)) {
                    ns.put(dist, new HashSet<>());
                }

                ns.get(dist).add(g);
                numNs++;
            }

            void removeAll(int dist) {
                if (ns.containsKey(dist)) {
                    numNs -= ns.get(dist).size();
                    ns.put(dist, new HashSet<>());
                }
            }

        }

    }

    static class MyBufferedReader {
        BufferedReader in;

        public MyBufferedReader(InputStream s) {
            this.in = new BufferedReader(new InputStreamReader(s));
        }

        public int getAnInt() {
            int res = -1;
            try {
                res = Integer.parseInt(new StringTokenizer(in.readLine()).nextToken());
            } catch (IOException e) {
                e.printStackTrace();
            }
            return res;
        }

        public int[] getALineOfInts(int numExpected) {
            int[] res = new int[numExpected];
            StringTokenizer st = null;
            try {
                st = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < numExpected; i++)
                res[i] = Integer.parseInt(st.nextToken());
            return res;
        }

    }
}

