//package com.company;

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

public class Main {
    static long TIME_START, TIME_END;
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        //        Scanner sc = new Scanner(new FileInputStream("Test.in"));
        PrintWriter pw = new PrintWriter(System.out);
        //        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.out"));
        //        PrintWriter pw = new PrintWriter(new FileOutputStream("Test.in"));

        Runtime runtime = Runtime.getRuntime();
        long usedMemoryBefore = runtime.totalMemory() - runtime.freeMemory();
        TIME_START = System.currentTimeMillis();
        Task t = new Task();
        t.solve(sc, pw);
        TIME_END = System.currentTimeMillis();
        long usedMemoryAfter = runtime.totalMemory() - runtime.freeMemory();
        pw.close();
        System.out.println("Memory increased:" + (usedMemoryAfter-usedMemoryBefore) / 1000000 );
        System.out.println("Time used: " + (TIME_END - TIME_START) + ".");
    }

    public static class Task {

        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            int n = sc.nextInt();
            int[][] examToDay = new int[n][2];
            Set<Integer> daysSet = new HashSet<>();
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 2; j++) {
                    int a = sc.nextInt();
                    examToDay[i][j] = a;
                    daysSet.add(a);
                }
            }
            int[] daysArray = daysSet.stream().mapToInt(Number::intValue).toArray();
            Arrays.sort(daysArray);
            int daysSize = daysArray.length;
            Map<Integer, Integer> daysMap = new HashMap<>(daysSize);
            for (int i = 0; i < daysArray.length; i++) {
                daysMap.put(daysArray[i], i);
            }
            int[] daysDegree = new int[daysSize];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 2; j++) {
                    examToDay[i][j] = daysMap.get(examToDay[i][j]);
                    daysDegree[examToDay[i][j]]++;
                }
            }
            int[][] daysGraph = new int[daysSize][];
            for (int i = 0; i < daysSize; i++) {
                daysGraph[i] = new int[daysDegree[i]];
            }
            int[] daysGraphIdx = new int[daysSize];
            for (int i = 0; i < n; i++) {
                int a = examToDay[i][0];
                int b = examToDay[i][1];
                daysGraph[a][daysGraphIdx[a]++] = i;
                daysGraph[b][daysGraphIdx[b]++] = i;
            }
            boolean[] visitExam = new boolean[n];
            boolean[] visitDay = new boolean[daysSize];
            for (int i = 0; i < n; i++) {
                if (!visitExam[i]) {
                    Set<Integer> visitedExam = new HashSet<>();
                    Set<Integer> visitedDay = new HashSet<>();
                    boolean b = tryDfs(i, examToDay[i][0], visitExam, visitDay, examToDay, daysGraph, visitedExam, visitedDay);
                    if (!b) {
                        for (int j : visitedExam) {
                            visitExam[j] = false;
                        }
                        for (int j : visitedDay) {
                            visitDay[j] = false;
                        }
                        b = forceDfs(i, examToDay[i][1], visitExam, visitDay, examToDay, daysGraph);
                        if (!b) {
                            pw.println(-1);
                            return;
                        }
                    }
                }
            }

            Arrays.fill(visitDay, false);
            Arrays.fill(visitExam, false);

            for (int d = daysSize - 1; d >= 0; d--) {
                if (!visitDay[d]) {
                    for (int i : daysGraph[d]) {
                        if (!visitExam[i]) {
                            boolean b = forceDfs(i, d, visitExam, visitDay, examToDay, daysGraph);
                            if (!b) {
                                pw.println(daysArray[d]);
                                return;
                            }
                        }
                    }
                } else {
                    pw.println(daysArray[d]);
                    return;
                }
            }
            int idx = daysSize - 1;
            while (idx >= 0 && !visitDay[idx]) {
                idx--;
            }
            pw.println(daysArray[idx]);

        }

        public boolean tryDfs(int u, int remove, boolean[] visitExam, boolean[] visitDay, int[][] examToDay,
                              int[][] daysGraph, Set<Integer> visitedExam, Set<Integer> visitedDay) {
            int otherDay = examToDay[u][0] == remove ? examToDay[u][1] : examToDay[u][0];
            if (visitDay[otherDay]) {
                return false;
            } else {
                visitExam[u] = true;
                visitDay[otherDay] = true;
                visitedExam.add(u);
                visitedDay.add(otherDay);
                for (int exam : daysGraph[otherDay]) {
                    if (!visitExam[exam]) {
                        boolean b = tryDfs(exam, otherDay, visitExam, visitDay, examToDay, daysGraph, visitedExam, visitedDay);
                        if (!b) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }

        public boolean forceDfs(int u, int remove, boolean[] visitExam, boolean[] visitDay, int[][] examToDay,
                              int[][] daysGraph) {
            int otherDay = examToDay[u][0] == remove ? examToDay[u][1] : examToDay[u][0];
            if (visitDay[otherDay]) {
                return false;
            } else {
                visitExam[u] = true;
                visitDay[otherDay] = true;
                for (int exam : daysGraph[otherDay]) {
                    if (!visitExam[exam]) {
                        boolean b = forceDfs(exam, otherDay, visitExam, visitDay, examToDay, daysGraph);
                        if (!b) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }

    static class Scanner {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){  br = new BufferedReader(new InputStreamReader(s));}

        public Scanner(FileReader s) throws FileNotFoundException {br = new BufferedReader(s);}

        public String next() throws IOException
        {
            while (st == null || !st.hasMoreTokens())
                st = new StringTokenizer(br.readLine());
            return st.nextToken();
        }

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

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

        public String nextLine() throws IOException {return br.readLine();}

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

        public boolean ready() throws IOException {return br.ready();}
    }
}