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.Random;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

public class Edu_49F_A {
	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();

		anc = new int[nV];
		full = new boolean[nV];
		rank = new int[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 boolean possible(int last) {
		if (last == nV) {
			return true;
		}
		Arrays.fill(anc, -1);
		Arrays.fill(full, false);
		Arrays.fill(rank, 0);

		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 (!merge(choices[i][0], choices[i][0])) {
					return false;
				}
			}
		}

		return true;
	}

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

	static int[] anc;
	static boolean[] full;
	static int[] rank;

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

		if (aRoot == bRoot) {
			if (full[aRoot]) {
				return false;
			}
			full[aRoot] = true;
			return true;
		}

		if (rank[a] < rank[b]) {
			if (full[aRoot] && full[bRoot]) {
				return false;
			}
			anc[aRoot] = bRoot;
			full[aRoot] = !full[aRoot] && !full[bRoot];
		} else {
			if (rank[a] == rank[b]) {
				if (full[aRoot] && full[bRoot]) {
					return false;
				}
				anc[aRoot] = bRoot;
				rank[aRoot]++;
				full[aRoot] = !full[aRoot] && !full[bRoot];
			} else {
				anc[bRoot] = aRoot;
				full[bRoot] = !full[aRoot] && !full[bRoot];
			}
		}
		return true;
	}

	static int fRoot(int cur) {
		int sSize = 0;
		while (anc[cur] != -1) {
			stack[sSize++] = cur;
			cur = anc[cur];
		}
		for (int i = 0; i < sSize; i++) {
			anc[stack[i]] = cur;
		}
		return cur;
	}

}