Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.

According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.

Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

Input

The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.

Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

Output

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

Examples

Input

3
5 2
3 1
4 2


Output

2


Input

3
6 1
5 2
4 3


Output

6

Note

In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.

In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
/**
 * Created by Grand on 27.07.2015.
 */
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class tr implements Runnable {

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok = new StringTokenizer("");

    public static void main(String[] args) {
        new Thread(null, new tr(), "", 128 * (1L << 20)).start();

    }

    void init() throws FileNotFoundException {
        Locale.setDefault(Locale.US);
        /*if (ONLINE_JUDGE) {
            in = new BufferedReader(new FileReader("island2.in"));
            out = new PrintWriter("island2.out");
        } else {*/
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        //}
    }

    long timeBegin, timeEnd;

    void time() {
        timeEnd = System.currentTimeMillis();
        System.err.println("Time = " + (timeEnd - timeBegin));
    }

    public void run() {
        try {
            timeBegin = System.currentTimeMillis();

            init();
            solve();

            out.close();
            time();
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }


    String readString() throws IOException {

        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine());
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    String readString(String s) throws IOException {
        while (!tok.hasMoreTokens()) {
            try {
                tok = new StringTokenizer(in.readLine(), s + "\n \t");
            } catch (Exception e) {
                return null;
            }
        }
        return tok.nextToken();
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    int readInt() throws IOException {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    int readInt(String s) throws IOException {
        return Integer.parseInt(readString(s));
    }

    boolean isPrime(int a) throws IOException {
        int del = 0;
        if (a == 2) {
            return false;
        }
        for (int i = 2; i < Math.sqrt(a) + 1; i++) {
            if (a % i == 0) {
                del = i;
                break;
            }
        }
        if (del > 0) {
            return false;
        } else return true;
    }

    public BigInteger bigFactorial(int num) throws IOException {
        BigInteger fact = new BigInteger("1");
        BigInteger i = new BigInteger("1");
        BigInteger one = new BigInteger("1");
        for (int j = 0; j < num; j++) {
            fact = fact.multiply(i);
            i = i.add(one);
//out.println(fact.toString()+" "+i.toString());
        }
//fact = fact.add(one);

        return fact;
    }

    public long factorial(int num) throws IOException {
        long res = 1;
        for (int i = 2; i <= num; i++) {
            res *= i;
        }
        return res;
    }

    //Random rd=new Random(System.currentTimeMillis());
    public int[] readIntArr(int n) throws IOException {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = readInt();
            //a[n-i-1] = - a[i];
        }
        return a;
    }

    class Pair implements Comparable {
        int norm, pred;

        public Pair(int sumue, int start) {
            norm = sumue;
            pred = start;
        }

        public int getnorm() {
            return norm;
        }

        public void setnorm(int sumue) {
            this.norm = sumue;
        }

        public int getpred() {
            return pred;
        }

        public void setpred(int q) {
            this.pred = q;
        }

        @Override
        public int compareTo(Object o) {
            if (this.norm > ((Pair) o).norm) {
                return 1;
            }
            if (this.norm < ((Pair) o).norm) {
                return -1;
            }
            if (this.norm == ((Pair) o).norm && this.pred > ((Pair) o).pred) {
                return 1;
            }
            if (this.norm == ((Pair) o).norm && this.pred < ((Pair) o).pred) {
                return -1;
            }

            return 0;
        }

        @Override
        public String toString() {
            return "Pair{" +
                    "norm=" + norm +
                    ", pred=" + pred +
                    '}';
        }
    }

    Random rnd = new Random();

    class two {
        int sum;
        int sumpr;

        public two(int sum, int sumpr) {
            this.sum = sum;
            this.sumpr = sumpr;
        }

        public int getSum() {
            return sum;
        }

        public void setSum(int sum) {
            this.sum = sum;
        }

        public int getSumpr() {
            return sumpr;
        }

        public void setSumpr(int sumpr) {
            this.sumpr = sumpr;
        }
    }


    class four implements Comparable<four> {
        int l, r, price;
        String lic;

        public four(int l, int r, int price, String lic) {
            this.l = l;
            this.r = r;
            this.price = price;
            this.lic = lic;
        }

        public int getL() {
            return l;
        }

        public void setL(int l) {
            this.l = l;
        }

        public int getR() {
            return r;
        }

        public void setR(int r) {
            this.r = r;
        }

        public int getPrice() {
            return price;
        }

        public void setPrice(int price) {
            this.price = price;
        }

        public String getLic() {
            return lic;
        }

        public void setLic(String lic) {
            this.lic = lic;
        }

        @Override
        public int compareTo(four four) {
            return this.r - four.r;
        }
    }


    void solve() throws Exception {
        int n=readInt();
        Pair[] a = new Pair[n];
        for (int i = 0; i < n; i++) {
            int norm  = readInt();
            int pred = readInt();
            a[i]=new Pair(norm, pred);
        }
        Arrays.sort(a);
        int minnorm=0, minpred=0;
        minnorm = a[0].getnorm();
        minpred = a[0].getpred();
        for (int i = 1; i < n; i++) {
            if(a[i].getnorm()>=minnorm && a[i].getpred()>=minpred){
                minnorm = a[i].getnorm();
                minpred = a[i].getpred();
            }else{
                minnorm = a[i].getnorm();
                minpred = a[i].getnorm();
            }
        }
        System.out.println(minpred);
    }


}
