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 MAX = 3000300;
	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("");

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

		TreeSet<Integer> set = new TreeSet<Integer>();
		int lo = (int)2e9, hi = -lo;
		for(int i = 0; i < n; i++) {
			days[i] = fs.nextIntArray(2);
			set.add(days[i][0]); set.add(days[i][1]);
		}
		
		HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
		HashMap<Integer, Integer> rap = new HashMap<Integer, Integer>();
		while(!set.isEmpty()) {
			int put = map.size();
			map.put(set.first(), put);
			rap.put(put, set.first());
			set.pollFirst();
		}
		
		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.get(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;
				if(!b2) marked[c1] = curTime;
				else if(!b1) marked[c2] = curTime;
				for(int x : adj[c1]) {
					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;
	}
	
	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;
		}
		
	}

	
}