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.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class Edu_49F_A {
	static int nC;
	static int[][] choices = new int[1_000_001][2];

	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());

		ArrayList<Integer> numbers = new ArrayList<Integer>();
		for (int i = 0; i < nC; i++) {
			String nLine = reader.readLine();
			int num0 = 0;
			int j;
			for (j = 0; j < nLine.length() && nLine.charAt(j) != ' '; j++) {
				num0 *= 10;
				num0 += nLine.charAt(j) - '0';
			}

			int num1 = 0;
			j++;
			for (; j < nLine.length(); j++) {
				num1 *= 10;
				num1 += nLine.charAt(j) - '0';
			}

			choices[i][0] = num0;
			choices[i][1] = num1;
			numbers.add(choices[i][0]);
			numbers.add(choices[i][1]);
		}

		Collections.sort(numbers);

		int nV = 0;
		int pre = -1;
		for (int number : numbers) {
			if (number != pre) {
				nV++;
				pre = number;
			}
		}

		HashMap<Integer, Integer> map = new HashMap<>();
		int[] rMap = new int[nV];

		int ind = 0;
		pre = -1;
		for (int number : numbers) {
			if (number != pre) {
				map.put(number, ind);
				rMap[ind] = number;
				ind++;
				pre = number;
			}
		}

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

		int low = nC - 1;
		int high = nV;

		for (int i = 0; i < nC; i++) {
			low = Math.max(low, choices[i][0]);
		}

		if (low > high) {
			printer.println(-1);
			printer.close();
		}

		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 boolean possible(int last) {
		if (last == nV) {
			return true;
		}
		for (int i = 0; i <= 2_000_000; i++) {
			full[i] = false;
		}
		for (int i = 0; i <= 2_000_000; i++) {
			rank[i] = -1;
		}

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

			if (choices[i][1] <= last) {
				if (!merge(choices[i][0], choices[i][1])) {
					return false;
				}
			} else {
				if (full[fRoot(choices[i][0])]) {
					return false;
				}
				full[fRoot(choices[i][0])] = true;
			}
		}
		return true;
	}

	// static int[] stack = new int[1_000_001];

	static boolean[] full = new boolean[2_000_001];
	static int[] rank = new int[2_000_001];

	static boolean merge(int a, int b) {
		a = fRoot(a);
		b = fRoot(b);

		if (a != b) {
			if (rank[b] < rank[a]) {
				int t = a;
				a = b;
				b = t;
			}
			rank[a] += rank[b];
			rank[b] = a;

			full[a] = full[a] || full[b];
		} else {
			if (full[a]) {
				return false;
			}
			full[a] = true;
		}

		return !full[a];
	}

	static int fRoot(int cur) {
		return rank[cur] < 0 ? cur : (rank[cur] = fRoot(rank[cur]));
	}
}