import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.StringTokenizer;

// resubmission of uwi's solution to see if it runs in time

public class Composite {

	void solve() throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

		int n = Integer.parseInt(reader.readLine());
		int[] a = new int[2 * n];
		for (int i = 0; i < 2 * n; i += 2) {
			StringTokenizer inputData = new StringTokenizer(reader.readLine());
			a[i] = Integer.parseInt(inputData.nextToken());
			a[i + 1] = Integer.parseInt(inputData.nextToken());
		}
		int[] b = Arrays.copyOf(a, 2 * n);
		int[] imap = shrinkX(b);

		anc = new int[2 * n];
		full = new boolean[2 * n];
		rank = new int[2 * n];

		if (!ok(imap.length - 1, b)) {
			printer.println(-1);
			printer.close();
			return;
		}

		int low = -1, high = imap.length;
		while (high - low > 1) {
			int h = high + low >> 1;
			if (ok(h, b)) {
				high = h;
			} else {
				low = h;
			}
		}
		printer.println(imap[high]);
		printer.close();
	}

	boolean ok(int h, int[] b) {
		int n = b.length / 2;
		DJSet(2 * n);
		for (int i = 0; i < n; i++) {
			if (b[2 * i] > h)
				return false;
			if (b[2 * i + 1] > h) {
				if (!merge(b[2 * i], b[2 * i])) {
					return false;
				}
			} else {
				if (!merge(b[2 * i], b[2 * i + 1])) {
					return false;
				}
			}
		}
		return true;
	}

	int[] stack = new int[2_000_001];

	void DJSet(int n) {
		Arrays.fill(anc, -1);
		Arrays.fill(full, false);
		Arrays.fill(rank, 0);
	}

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

	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 (full[aRoot] && full[bRoot]) {
			return false;
		}

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

	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;
	}

	public static int[] shrinkX(int[] a) {
		int n = a.length;
		long[] b = new long[n];
		for (int i = 0; i < n; i++)
			b[i] = (long) a[i] << 32 | i;
		Arrays.sort(b);
		int[] ret = new int[n];
		int p = 0;
		ret[0] = (int) (b[0] >> 32);
		for (int i = 0; i < n; i++) {
			if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
				p++;
				ret[p] = (int) (b[i] >> 32);
			}
			a[(int) b[i]] = p;
		}
		return Arrays.copyOf(ret, p + 1);
	}

	public static void main(String[] args) throws Exception {
		new Composite().solve();
	}
}