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.*;
import java.util.*;

public class F implements Runnable {
	public static void main (String[] args) {new Thread(null, new F(), "_cf", 1 << 28).start();}
	
	int n, curTime = 0;
	int[] queue, isDone, marked, freq;
	int[][] days, adj;
	DSU ds;
	
	public void run() {
		FastScanner fs = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		System.err.println("");

		n = fs.nextInt();
		queue = new int[2*n];
		isDone = new int[n];
		marked = new int[2*n];
		freq = new int[2*n];
		days = new int[n][2];

		int[] set = new int[2*n];
		int lo = (int)2e9, hi = -lo;
		for(int i = 0; i < n; i++) {
			days[i] = fs.nextIntArray(2);
			set[i] = days[i][0];
			set[i+n] = days[i][1];
		}
		sort(set);
		
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		int[] rap = new int[2*n];
		for(int i = 0; i < set.length; i++) {
			int put = map.size();
			map.put(set[i], put);
			rap[put] = set[i];
			int j = i;
			while(j < set.length && set[j] == set[i]) j++;
			i = j-1;
		}
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < 2; j++)
				days[i][j] = map.get(days[i][j]);
			for(int j = 0; j < 2; j++)
				freq[days[i][j]]++;
			lo = Math.min(lo, days[i][0]);
			hi = Math.max(hi, days[i][1]);
		}
		
		adj = new int[map.size()][];
		for(int i = 0; i < map.size(); i++) {
			adj[i] = new int[freq[i]];
			freq[i] = 0;
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < 2; j++) {
				int val = days[i][j];
				adj[val][freq[val]++] = i;
			}
		}
		ds = new DSU(map.size());
		
		int res = Integer.MAX_VALUE;
		for(int bs = 0; bs < 50 && lo <= hi; bs++) {
			curTime++;
			int mid = (lo+hi)/2;
			boolean good = check(mid);
			if(good) {
				res = mid;
				hi = mid-1;
			}
			else {
				lo = mid+1;
			}
		}
		if(res == Integer.MAX_VALUE) out.println(-1);
		else out.println(rap[res]);
		
		out.close();
	}
	
	boolean check(int max) {
		for(int i = 0; i < n; i++) if(isDone[i] != curTime) {
			if(days[i][0] > max) return false;
			if(days[i][1] <= max) continue;
			if(marked[days[i][0]] == curTime) return false;
			int tail = 0;
			queue[tail++] = i;
			for(int j = 0; j < tail; j++) {
				int u = queue[j];
				isDone[u] = curTime;
				int c1 = days[u][0], c2 = days[u][1];
				boolean b1 = marked[c2] == curTime || c2 > max;
				boolean b2 = marked[c1] == curTime || c1 > max;
				if(b1 && b2) return false;
				int which = -1;
				if(!b2) {
					marked[c1] = curTime;
					which = c1;
				}
				else if(!b1) {
					marked[c2] = curTime;
					which = c2;
				}
				for(int x : adj[which]) {
					if(isDone[x] == curTime) continue;
					isDone[x] = curTime;
					queue[tail++] = x;
				}
			}
		}
		
		ds.clear();
		for(int i = 0; i < n; i++) if(isDone[i] != curTime) {
			boolean get = ds.union(days[i][0], days[i][1]);
			if(!get) return false;
		}
		
		return true;
	}
	
	void sort(int[] a) {
		Random rand = new Random();
		for(int i = 0; i < a.length; i++) {
			int x = rand.nextInt(a.length);
			int y = rand.nextInt(a.length);
			int t = a[x]; a[x] = a[y]; a[y] = t;
		}
		Arrays.sort(a);
	}
	
	class DSU {
		int n;
		int[] par, rank, size, attempt;
		DSU(int a) {
			n = a;
			par = new int[n];
			rank = new int[n];
			size = new int[n];
			attempt = new int[n];
			for(int i = 0; i < n; i++) {
				par[i] = i;
				size[i] = 1;
			}
		}
		int find(int u) {
			if(par[u] == u) return u;
			return par[u] = find(par[u]);
		}
		boolean union(int u, int v) {
			int pu = find(u), pv = find(v);
			if(pu == pv) {
				attempt[pu]++;
				if(attempt[pu] > size[pu]) return false;
				return true;
			}
			if(rank[pu] >= rank[pv]) {
				if(rank[pu] == rank[pv]) rank[pu]++;
				par[pv] = pu;
				size[pu] += size[pv];
				attempt[pu] += attempt[pv]+1;
				if(attempt[pu] > size[pu]) return false;
			}
			else {
				par[pu] = pv;
				size[pv] += size[pu];
				attempt[pv] += attempt[pu]+1;
				if(attempt[pv] > size[pv]) return false;
			}
			return true;
		}
		void clear() {
			for(int i = 0; i < n; i++) {
				par[i] = i;
				attempt[i] = 0;
				size[i] = 1;
				rank[i] = 0;
			}
		}
	}
	
	class FastScanner {
		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 FastScanner() {
			in = new BufferedInputStream(System.in, BS);
		}

		public FastScanner(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() {
			double cur = nextLong();
			return c!='.' ? cur:cur+nextLong()/num;
		}

		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;
			}
		}
		
		public int[] nextIntArray(int n) {
			int[] res = new int[n];
			for(int i = 0; i < n; i++) res[i] = nextInt();
			return res;
		}
		
	}

	
}