import java.io.*;
import java.util.*;


public class ExF {

    public static class ExamNode {
        public int id;
        public LinkedList<TimeNode> slots;

        boolean isSolved;

        TimeNode match;

        public ExamNode(int id, TimeNode tnA, TimeNode tnB) {
            this.id = id;
            slots = new LinkedList<>();
            slots.add(tnA);
            slots.add(tnB);
        }

    }

    public static class TimeNode {
        public long id;
        public LinkedList<ExamNode> exams;

        boolean seen;
        ExamNode matchR;
        boolean blocked;

        public TimeNode(long id) {
            this.id = id;
            exams = new LinkedList<>();
        }
        public void addExam( ExamNode ex ) {
            exams.add(ex);
        }
    }

    public static class Match {
        boolean isPos;
        long max;
    }

    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));

        /*Solution starts here*/

        int n = sc.nextInt();
        int N = n;
        ExamNode[] exams = new ExamNode[n];
        HashMap<Long, TimeNode> timeNodes = new HashMap<>();

        for (int i = 0; i < n; i++) {
            long a = sc.nextLong();
            long b = sc.nextLong();

            TimeNode tmA = timeNodes.get(a);
            if (tmA == null) {
                tmA = new TimeNode(a);
                timeNodes.put(a, tmA);
            }
            TimeNode tmB = timeNodes.get(b);
            if (tmB == null) {
                tmB = new TimeNode(b);
                timeNodes.put(b, tmB);
            }

            exams[i] = new ExamNode(i, tmA, tmB);
            tmA.addExam(exams[i]);
            tmB.addExam(exams[i]);
        }

        int nMatches = maxBPM(exams, timeNodes, N);
        if (nMatches != N)
            out.println("-1");
        else {
            long max = 0;
            boolean noBetterSol = false;
            checkbettersol:
            do {
                long prevMax = max;
                max = 0;
                for (int i = 0; i < n; i++) {
                    if (exams[i].match.id > max) {
                        max = exams[i].match.id;
                    }
                }
                if (prevMax == max)
                    break checkbettersol;

                HashSet<ExamNode> solvedExNodes = new HashSet<>();
                HashSet<TimeNode> solvedTmNodes = new HashSet<>();

                for (TimeNode tn : timeNodes.values()) {
                    if (tn.id >= max) {
                        solvedTmNodes.add(tn);
                    }
                }

                while (!solvedTmNodes.isEmpty()) {
                    for (TimeNode tn : solvedTmNodes) {
                        tn.blocked = true;
                        solvedExNodes.addAll(tn.exams);
                        for (ExamNode exam : tn.exams) {
                            exam.slots.remove(tn);
                        }
                    }
                    solvedTmNodes.clear();

                    for (ExamNode exam : solvedExNodes) {
                        if (exam.isSolved) continue;
                        if (exam.slots.size() == 0) {
                            break checkbettersol;
                        }
                        TimeNode tmNode = exam.slots.getFirst();
                        if (tmNode.blocked) {
                            noBetterSol = true;
                            break checkbettersol;
                        }
                        tmNode.blocked = true;
                        exam.isSolved = true;
                        exam.match = tmNode;
                        int exId = exam.id;
                        exams[exId] = exams[N-1];
                        exams[exId].id = exId;
                        N--;
                        solvedTmNodes.add(tmNode);
                    }
                    solvedExNodes.clear();
                }

                //System.out.println("MAX = " + max + " -> Recalculate BPM");
                nMatches = maxBPM(exams, timeNodes, N);
                //System.out.println("nMatches = " + nMatches);
            } while (nMatches == N && N > 0);

            if (N == 0 && !noBetterSol) {
                max = 0;
                for (int i = 0; i < n; i++) {
                    if (exams[i].match.id > max) {
                        max = exams[i].match.id;
                    }
                }
            }

            out.println(max);
        }

        /*Solution ends here*/

        out.close();
    }

    public static int maxBPM(ExamNode[] exams, HashMap<Long, TimeNode> timeSlots, int N) {
        int result = 0;

        timeSlots.forEach( (aLong, tn) -> tn.matchR = null );

        for (int i = 0; i < N; i++) {
            timeSlots.forEach( (aLong, tn) -> tn.seen = false );

            if (bpm(exams, timeSlots, i))
                result++;
        }
        return result;
    }

    // Bipartite Matching by DFS
    public static boolean bpm(ExamNode[] exams, HashMap<Long, TimeNode> timeSlots, int u ) {
        // If applicant u is interested
        // in job v and v is not visited

        for (TimeNode tn : exams[u].slots) {
            if (tn.seen) continue;

            tn.seen = true;

            if (!tn.blocked && (tn.matchR == null || bpm(exams, timeSlots, tn.matchR.id))) {
                tn.matchR = exams[u];
                exams[u].match = tn;
                return true;
            }
        }
        return false;
    }

    /**
     * CodeForces Competition
     * IO Functionality by Flatfoot <= thx (http://codeforces.com/blog/entry/7018)
     */
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;

    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;

        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

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

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

    }

}
