// package training;

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

public class Sol {

    static ArrayList<Integer> alldays;
    public static void main(String[] args) throws Throwable {
        MyScanner sc = new MyScanner();
        PrintWriter pw = new PrintWriter(System.out);

        int n = sc.nextInt();
        int[] d1 = new int[n];
        int[] d2 = new int[n];
        alldays = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            d1[i] = sc.nextInt();
            d2[i] = sc.nextInt();
            alldays.add(d1[i]);
            alldays.add(d2[i]);
        }

        Collections.sort(alldays);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < alldays.size(); i++) {
            map.put(alldays.get(i), i);
        }

        for (int i = 0; i < n; i++) {
            d1[i] = map.get(d1[i]);
            d2[i] = map.get(d2[i]);
        }

        int lo = 0;
        int hi = alldays.size();
        int ans = -1;
        while(lo < hi){
            int mid = lo + (hi-lo)/2;
            if(ok(mid, d1, d2)) {
                ans = mid;
                hi = mid;
            }
            else
                lo = mid+1;
        }

        if(ans == -1)
            pw.println(ans);
        else
            pw.println(alldays.get(ans));

        pw.flush();
        pw.close();
    }

    static boolean ok(int days, int[] d1, int[] d2){
        int n = d1.length;
        boolean taken[] = new boolean[n];
        HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
        HashSet<Integer> takenDays = new HashSet<>();
        int[] q = new int[2*n+1];
        int qen = 0, qs = 1;
        for (int i = 0; i < d1.length; i++) {
            if(d1[i] > days)
                return false;
            if(!map.containsKey(d1[i]))
                map.put(d1[i], new ArrayList<>());
            if(!map.containsKey(d2[i]))
                map.put(d2[i], new ArrayList<>());
            map.get(d1[i]).add(i);
            map.get(d2[i]).add(i);
            if(d2[i] > days)
                q[qen++] = i;
        }

        while(qs < qen){
            int exam = q[qs++];
            if(takenDays.contains(d1[exam]) && takenDays.contains(d2[exam]))
                return false;
            int day = d1[exam];
            if(takenDays.contains(d1[exam]) || day > days){
                day = d2[exam];
            }

            if(day > days || takenDays.contains(day)) {
                return false;
            }
            taken[exam] = true;
            for (int v: map.get(day)) {
                if(taken[v]) {
                    continue;
                }
                q[qen++] = v;
            }

            takenDays.add(day);
        }

        // construct graph
        ArrayList<Integer>[] adj = new ArrayList[alldays.size()];
        for (int i = 0; i < n; i++) {
            if(taken[i])
                continue;
            int fst = d1[i];
            int snd = d2[i];
            if(adj[fst] == null)
                adj[fst] = new ArrayList<>();
            if(adj[snd] == null)
                adj[snd] = new ArrayList<>();
            adj[fst].add(snd);
            adj[snd].add(fst);
        }

        // check components
        boolean[] vis = new boolean[alldays.size()];
        int[] bfsq = new int[alldays.size()];
        int s = 0, e = 0;
        for (int i = 0; i < alldays.size(); i++) {
            if(vis[i])
                continue;
            bfsq[e++] = i;
            vis[i] = true;
            int nodescnt = 0, ecnt = 0;
            for (; s < e; s++) {
                int u = bfsq[s];
                nodescnt++;
                if(adj[u] == null)
                    continue;
                ecnt += adj[u].size();
                for (int v: adj[u]) {
                    if(!vis[v]){
                        bfsq[e++] = v;
                        vis[v] = true;
                    }
                }
            }

            ecnt /= 2;
            if(nodescnt < ecnt)
                return false;
        }

        return true;

    }


    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;
        }
    }
}