import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Edu_49F {
	static int nC;
	static int[][] choices;

	static int nV;

	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		nC = Integer.parseInt(reader.readLine());
		choices = new int[nC][2];

		TreeSet<Integer> unique = new TreeSet<Integer>();
		for (int i = 0; i < nC; i++) {
			StringTokenizer inputData = new StringTokenizer(reader.readLine());
			choices[i][0] = Integer.parseInt(inputData.nextToken());
			choices[i][1] = Integer.parseInt(inputData.nextToken());
			unique.add(choices[i][0]);
			unique.add(choices[i][1]);
		}

		TreeMap<Integer, Integer> map = new TreeMap<>();
		int[] rMap = new int[unique.size()];

		int ind = 0;
		for (int number : unique) {
			map.put(number, ind);
			rMap[ind] = number;
			ind++;
		}

		for (int i = 0; i < nC; i++) {
			choices[i][0] = map.get(choices[i][0]);
			choices[i][1] = map.get(choices[i][1]);
		}

		nV = unique.size();

		nxt = new int[nV];
		taken = new boolean[nV];

		int low = 0;
		int high = nV;

		while (low != high) {
			int mid = (low + high) >> 1;
			if (possible(mid)) {
				high = mid;
			} else {
				low = mid + 1;
			}
		}

		if (low == nV) {
			printer.println(-1);
		} else {
			printer.println(rMap[low]);
		}
		printer.close();
	}

	static int[] nxt;
	static boolean[] taken;

	static boolean possible(int last) {
		if (last == nV) {
			return true;
		}
		Arrays.fill(nxt, -1);
		Arrays.fill(taken, false);

		for (int i = 0; i < nC; i++) {
			if (choices[i][0] > last) {
				return false;
			}
			if (!taken[choices[i][0]]) {
				taken[choices[i][0]] = true;

				if (choices[i][1] <= last) {
					nxt[choices[i][0]] = choices[i][1];
				}
			} else if (choices[i][1] <= last && !taken[choices[i][1]]) {
				taken[choices[i][1]] = true;
				nxt[choices[i][1]] = choices[i][0];
			} else {
				int end0 = fEnd(choices[i][0]);
				if (end0 == -1) {
					return false;
				}
				if (!taken[end0]) {
					taken[end0] = true;
					if (choices[i][1] <= last) {
						nxt[end0] = choices[i][1];
					}
				} else {
					if (choices[i][1] <= last) {
						int end1 = fEnd(choices[i][1]);
						if (end1 == -1) {
							return false;
						}
						if (!taken[end1]) {
							taken[end1] = true;
							// no point in setting nxt to be other choice because that one is already taken
						} else {
							return false;
						}
					} else {
						return false;
					}
				}
			}
		}

		return true;
	}

	static int[] stack = new int[1000001];

	static int[] visited = new int[1000001];
	static int cVisit = 1;

	static int fEnd(int cur) {
		int sSize = 0;
		while (nxt[cur] != -1) {
			stack[sSize++] = cur;
			cur = nxt[cur];
			if (visited[cur] == cVisit) {
				return -1;
			}
			visited[cur] = cVisit;
		}

		for (int i = 0; i < sSize; i++) {
			nxt[stack[i]] = cur;
		}
		cVisit++;
		return cur;
	}
}
