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 here[][];
	static int uniq;
	static int iter = 0;
	static int[] usedNode, used;
	static queue q;
	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++;
			}
		}

		here = new int[uniq][];
		int lo = -1;
		int hi = -1;
		int 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]);
		}
		for(int i = 0; i < uniq; i++) here[i] = new int[ref[i]];
		for(int i = 0; i < N; i++) {
			here[f[i]][--ref[f[i]]] = i;
			here[l[i]][--ref[l[i]]] = i;
		}
		q = new queue(N);
		usedNode = new int[N];
		used = new int[uniq];
		
		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(uniq);
		int size[] = new int[uniq];
		int choices[] = new int[uniq];
		for(int i = 0; i < uniq; i++) {
			size[i] = here[i].length;
			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 < uniq; 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;
			}
		}
	}
	
}
