Polycarp studies in Berland State University. Soon he will have to take his exam. He has to pass exactly n exams.

For the each exam i there are known two days: a_i — day of the first opportunity to pass the exam, b_i — day of the second opportunity to pass the exam (a_i < b_i). Polycarp can pass at most one exam during each day. For each exam Polycarp chooses by himself which day he will pass this exam. He has to pass all the n exams.

Polycarp wants to pass all the exams as soon as possible. Print the minimum index of day by which Polycarp can pass all the n exams, or print -1 if he cannot pass all the exams at all.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 10^6) — the number of exams.

The next n lines contain two integers each: a_i and b_i (1 ≤ a_i < b_i ≤ 10^9), where a_i is the number of day of the first passing the i-th exam and b_i is the number of day of the second passing the i-th exam.

Output

If Polycarp cannot pass all the n exams, print -1. Otherwise print the minimum index of day by which Polycarp can do that.

Examples

Input

2
1 5
1 7


Output

5


Input

3
5 13
1 5
1 7


Output

7


Input

3
10 40
40 80
10 80


Output

80


Input

3
99 100
99 100
99 100


Output

-1
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class F5 {

	static int N,f[],l[];
	static int rev[];
	static int ref[];
	static int uniq;
	static int iter = 0;
	public static void main(String[] args) {
		JS in = new JS();
		N = in.nextInt();
		f = new int[N];
		l = new int[N];
		data dats[] = new data[2*N];
		for(int i = 0; i < N; i++) {
			f[i] = in.nextInt();
			l[i] = in.nextInt();
			dats[2*i] = new data(f[i], i, true);
			dats[2*i+1] = new data(l[i], i, false);
		}
		rev = new int[2*N+1];
		Arrays.sort(dats);
		uniq = 0;
		int prev = -1;
		for(int i = 0; i < 2*N; i++) {
			data d = dats[i];
			if(d.value == prev) {
				if(d.first) f[d.id] = uniq-1;
				else l[d.id] = uniq-1;
			}
			else {
				prev = d.value;
				rev[uniq] = d.value;
				if(d.first) f[d.id] = uniq;
				else l[d.id] = uniq;
				uniq++;
			}
		}

		int lo = -1;
		int hi = -1;
		ref = new int[uniq];
		for(int i = 0; i < N; i++) {
			//System.out.println(f[i]+" "+l[i]);
			ref[f[i]]++;
			ref[l[i]]++;
			lo = Math.max(lo, f[i]);
			hi = Math.max(hi, l[i]);
		}

		int res = -1;
		while(true) {
			if(lo==hi) {
				if(good(lo))res = rev[lo];
				break;
			}
			int mid = (lo+hi)/2;
			if(good(mid)) {
				hi = mid;
			}
			else {
				lo = mid+1;
			}
		}
		System.out.println(res);
	}

	static boolean good(int val) {
		iter++;
		//Get all the things done that we HAVE to
		DisjointSet dsu = new DisjointSet(val+1);
		int size[] = new int[val+1];
		int choices[] = new int[val+1];
		for(int i = 0; i <= val; i++) {
			size[i] = ref[i];
			choices[i] = 1;
		}
		for(int i = 0; i < N; i++) {
			if(l[i] > val) continue;
			int p1 = dsu.find(f[i]);
			int p2 = dsu.find(l[i]);
			if(p1==p2) size[p1]--;
			else {
				dsu.union(p1, p2);
				size[dsu.find(p1)] = size[p1]+size[p2]-1;
				choices[dsu.find(p1)] = choices[p1]+choices[p2];
			}
		}
		for(int i = 0; i <= val; i++) { 
			if(size[dsu.find(i)] > choices[dsu.find(i)]) return false;
		}
		return true;
	}

	static class DisjointSet {
		int[] s;
		
		public DisjointSet(int n) {
			Arrays.fill(s = new int[n], -1);
		}
		public int find(int i) {
			return s[i] < 0 ? i : (s[i] = find(s[i]));
		}
		
		public boolean union(int a, int b) {
			if ((a = find(a)) == (b = find(b))) return false;
			if(s[a] == s[b]) s[a]--;
			if(s[a] <= s[b]) s[b] = a; else s[a] = b;
			return true;
		}
	}
	
	static class queue{
		int v[];
		int lo, hi;
		public queue(int n) {
			v = new int[n];
			lo=0;
			hi=0;
		}
		public boolean isEmpty() {
			return lo==hi;
		}
		public void clear() {
			lo=0;
			hi=0;
		}
		public void add(int x) {
			v[hi] = x;
			hi++;
		}
		public int poll() {
			return v[lo++];
		}
	}
	static class data implements Comparable<data>{
		int value;
		int id;
		boolean first;
		public data(int vv, int ii, boolean ff) {
			value=vv;
			id=ii;
			first=ff;
		}
		@Override
		public int compareTo(data o) {
			return this.value-o.value;
		}
	}
	
	static class JS{
		public int BS = 1<<16;
		public char NC = (char)0;
		byte[] buf = new byte[BS];
		int bId = 0, size = 0;
		char c = NC;
		double num = 1;
		BufferedInputStream in;
		
		public JS() {
			in = new BufferedInputStream(System.in, BS);
		}
		
		public JS(String s) throws FileNotFoundException {
			in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
		}
		
		public char nextChar(){
			while(bId==size) {
				try {
					size = in.read(buf);
				}catch(Exception e) {
					return NC;
				}				
				if(size==-1)return NC;
				bId=0;
			}
			return (char)buf[bId++];
		}
		
		public int nextInt() {
			return (int)nextLong();
		}
		
		public long nextLong() {
			num=1;
			boolean neg = false;
			if(c==NC)c=nextChar();
			for(;(c<'0' || c>'9'); c = nextChar()) {
				if(c=='-')neg=true;
			}
			long res = 0;
			for(; c>='0' && c <='9'; c=nextChar()) {
				res = (res<<3)+(res<<1)+c-'0';
				num*=10;
			}
			return neg?-res:res;
		}
		
		public double nextDouble() {
		    while(c!='.'&&c!='-'&&(c <'0' || c>'9')) c = nextChar();
		    boolean neg = c=='-';
		    if(neg)c=nextChar();
		    boolean fl = c=='.';
		    double cur = nextLong();
		    if(fl) return neg ? -cur/num : cur/num;
		    if(c == '.') {
			double next = nextLong();
			return neg ? -cur-next/num : cur+next/num;
		    }
		    else return neg ? -cur : cur;
		}
		
		public String next() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c>32) {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}
		
		public String nextLine() {
			StringBuilder res = new StringBuilder();
			while(c<=32)c=nextChar();
			while(c!='\n') {
				res.append(c);
				c=nextChar();
			}
			return res.toString();
		}

		public boolean hasNext() {
			if(c>32)return true;
			while(true) {
				c=nextChar();
				if(c==NC)return false;
				else if(c>32)return true;
			}
		}
	}
	
}
