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

public class Main {
    static class Pair implements Comparable<Pair> {
        int a, b;
        Pair(int aa, int bb) {
            a = aa;
            b = bb;
        }

        @Override
        public int compareTo(Pair o) {
            return a != o.a ? a - o.a : b - o.b;
        }
    }
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = sc.nextInt();
        TreeSet<Integer> set = new TreeSet<>();
        int[] a = new int[n], b = new int[n];
        int[] rev = new int[n << 1];
        for(int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
            b[i] = sc.nextInt();
            set.add(a[i]);
            set.add(b[i]);
        }

        HashMap<Integer,Integer> map = new HashMap<>();
        for(int x : set) {
            rev[map.size()] = x;
            map.put(x, map.size());
        }

        ArrayList<Integer> students[] = new ArrayList[map.size()];
        for(int i = 0; i < map.size(); i++) students[i] = new ArrayList<>();
        for(int i = 0; i < n; i++) {
            students[a[i] = map.get(a[i])].add(i);
            students[b[i] = map.get(b[i])].add(i);
        }

        int lo = 0, hi = map.size(), best = -1;
        boolean[] found = new boolean[n << 1];
        boolean[] finished = new boolean[n];
        while(lo <= hi) {
            int mid = lo + hi >> 1;
            boolean ok = true;
            Arrays.fill(found, false);
            Arrays.fill(finished, false);
            Queue<Integer> q = new LinkedList<>();
            for(int i = 0; i < n; i++) {
                if(a[i] > mid) ok = false;
                else if(b[i] > mid) {
                    q.add(i);
                }
            }
            while(!q.isEmpty()) {
                int student = q.poll();
                int taken = -1;
                if(!found[a[student]] && a[student] <= mid) {
                    found[a[student]] = true;
                    finished[student] = true;
                    taken = a[student];
                } else if(!found[b[student]] && b[student] <= mid) {
                    found[b[student]] = true;
                    finished[student] = true;
                    taken = b[student];
                } else {
                    ok = false;
                    break;
                }
                for(int sad : students[taken])
                    if(!finished[sad]) {
                        finished[sad] = true;
                        q.add(sad);
                    }
            }
            TreeSet<Pair> found2 = new TreeSet<>();
            for(int i = 0; i < n; i++) {
                if(!finished[i] && a[i] <= mid && b[i] <= mid) {
                    if(found2.contains(new Pair(a[i], b[i])))
                        ok = false;
                    else {
                        found2.add(new Pair(a[i], b[i]));
                    }
                }
            }

            if(ok) {
                best = mid;
                hi = mid - 1;
            } else {
                lo = mid + 1;
            }
        }

        out.println(best == -1 ? -1 : rev[best]);
        out.flush();
        out.close();
    }
    static class Scanner
    {
        StringTokenizer st;
        BufferedReader br;

        public Scanner(InputStream s){	br = new BufferedReader(new InputStreamReader(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 String nextLine() throws IOException {return br.readLine();}
    }
}