import java.util.*;
import java.io.*; 
import java.text.*;
//Solution Credits: Taranpreet Singh
public class Main{
    //SOLUTION BEGIN
    void solve(int TC) throws Exception{
        int n = ni();
        long[][] exam = new long[n][];
        TreeSet<Long> set = new TreeSet<>();
        HashMap<Long, Integer> map = new HashMap<>();
        long[] rmap = new long[MAX];
        for(int i = 0; i< n; i++){
            exam[i] = new long[]{nl(), nl()};
            set.add(exam[i][0]);
            set.add(exam[i][1]);
        }
        int c = 0;
        for(long l:set){
            rmap[c] = l;
            map.put(l, c);c++;
        }
        int[][] ex = new int[n][2];
        ArrayList<Integer>[] adj = new ArrayList[c];
        for(int i = 0; i< c; i++)adj[i] = new ArrayList<>();
        for(int i = 0; i< n;i++){
            ex[i][0] = map.get(exam[i][0]);
            ex[i][1] = map.get(exam[i][1]);
            adj[ex[i][0]].add(i);
            adj[ex[i][1]].add(i);
        }
        q = new int[MAX];
        if(!check(ex,adj, c-1, c))pn(-1);
        else if(check(ex, adj, 0, c))pn(rmap[0]);
        else {
            int lo = 0, hi = c;
            while(lo<hi){
                int mid = (lo+hi)/2;
                if(check(ex,adj, mid, c))hi = mid;
                else lo = mid+1;
            }
            pn(rmap[hi]);
        }
    }
    int[] q;
    boolean check(int[][] exam,ArrayList<Integer>[] adj, int lim, int c){
        int l = 0,r = -1, n = exam.length;
        boolean[] used = new boolean[c];
        boolean[][] inv = new boolean[n][2];
        for(int i = 0; i< n; i++){
            if(exam[i][0]>lim)return false;
            if(exam[i][1]>lim){
                q[++r] = i;
                inv[i][1] = true;
            }
        }
        boolean[] done = new boolean[n];
        while(l<=r){
            int x = q[l++];
            int cur = -1;
            if(inv[x][0])cur = exam[x][1];
            else cur = exam[x][0];
            if(used[cur])return false;
            used[cur] = true;
            done[x] = true;
            for(int i:adj[cur]){
                if(!done[i]){
                    q[++r] = i;
                    if(exam[i][0]==cur)inv[i][0] = true;
                    else inv[i][1] = true;
                }
            }
        }
        int[] set = new int[c], cnt = new int[c];
        for(int i = 0; i< n; i++){
            if(!done[i]){
                cnt[exam[i][0]]++;
                union(set, exam[i][0], exam[i][1]);
            }
        }
        for(int i = 0; i< c; i++){
            if(set[i]==i){
                cnt[i]--;
            }else cnt[find(set, i)]+= cnt[i]-1;
        }
        for(int i = 0; i< c; i++)if(set[i]==i && cnt[i]>0)return false;
        return true;
    }
    int find(int[] set, int u){
        if(set[u]!=u)set[u] = find(set, set[u]);
        return set[u];
    }
    void union(int[] set, int u, int v){
        u = find(set, u);v = find(set,v);
        set[u] = v;
    }
    //SOLUTION ENDS
    long mod = (int)1e9+7, IINF = (long)1e12;
    final int MAX = (int)2e6+1, INF = (int)1e9, root = 3;
    DecimalFormat df = new DecimalFormat("0.00000000");
    double PI = 3.141592653589793238462643383279502884197169399375105820974944;
    static boolean multipleTC = false, memory = true;
    FastReader in;PrintWriter out;
    void run() throws Exception{
        in = new FastReader();
        out = new PrintWriter(System.out);
        //Solution Credits: Taranpreet Singh
        for(int i = 1, T= (multipleTC)?ni():1; i<= T; i++)solve(i);
        out.flush();
        out.close();
    }
    public static void main(String[] args) throws Exception{
        if(memory)new Thread(null, new Runnable() {public void run(){try{new Main().run();}catch(Exception e){e.printStackTrace();}}}, "1", 1 << 28).start();
        else new Main().run();
    }
    long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
    int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
    int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
    void p(Object o){out.print(o);}
    void pn(Object o){out.println(o);}
    void pni(Object o){out.println(o);out.flush();}
    String n(){return in.next();}
    String nln(){return in.nextLine();}
    int ni(){return Integer.parseInt(in.next());}
    long nl(){return Long.parseLong(in.next());}
    double nd(){return Double.parseDouble(in.next());}

    class FastReader{
        BufferedReader br;
        StringTokenizer st;
        public FastReader(){
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        public FastReader(String s) throws Exception{
            br = new BufferedReader(new FileReader(s));
        }

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

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