Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.

According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.

Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

Input

The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.

Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

Output

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

Examples

Input

3
5 2
3 1
4 2


Output

2


Input

3
6 1
5 2
4 3


Output

6

Note

In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.

In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.
import java.io.*;
import java.util.*;

public class practiceClass {

	static class Reader {

		boolean consume = false;
		final private int BUFFER_SIZE = 1 << 12;
		private byte[] buffer;
		private int bufferPointer, bytesRead;
		private boolean reachedEnd = false;

		public Reader() {
			buffer = new byte[BUFFER_SIZE];
			bufferPointer = 0;
			bytesRead = 0;
		}

		public boolean hasNext() {
			return !reachedEnd;
		}

		private void fillBuffer() throws IOException {
			bytesRead = System.in.read(buffer, bufferPointer = 0, BUFFER_SIZE);
			if (bytesRead == -1) {
				buffer[0] = -1;
				reachedEnd = true;
			}
		}

		private void consumeSpaces() throws IOException {
			while (read() <= ' ' && reachedEnd == false)
				;
			bufferPointer--;
		}

		private byte read() throws IOException {
			if (bufferPointer == bytesRead) {
				fillBuffer();
			}
			return buffer[bufferPointer++];
		}

		public String next() throws IOException {
			StringBuilder sb = new StringBuilder();
			consumeSpaces();
			byte c = read();
			do {
				sb.append((char) c);
			} while ((c = read()) > ' ');
			if (consume) {
				consumeSpaces();
			}
			;
			if (sb.length() == 0) {
				return null;
			}
			return sb.toString();
		}

		public String nextLine() throws IOException {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			String str = br.readLine();
			return str;
		}

		public int nextInt() throws IOException {
			consumeSpaces();
			int ret = 0;
			byte c = read();
			boolean neg = (c == '-');
			if (neg) {
				c = read();
			}
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (consume) {
				consumeSpaces();
			}
			if (neg) {
				return -ret;
			}
			return ret;
		}

		public long nextLong() throws IOException {
			consumeSpaces();
			long ret = 0;
			byte c = read();
			boolean neg = (c == '-');
			if (neg) {
				c = read();
			}
			do {
				ret = ret * 10L + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (consume) {
				consumeSpaces();
			}
			if (neg) {
				return -ret;
			}
			return ret;
		}

		public double nextDouble() throws IOException {
			consumeSpaces();
			double ret = 0;
			double div = 1;
			byte c = read();
			boolean neg = (c == '-');
			if (neg) {
				c = read();
			}
			do {
				ret = ret * 10 + c - '0';
			} while ((c = read()) >= '0' && c <= '9');
			if (c == '.') {
				while ((c = read()) >= '0' && c <= '9') {
					ret += (c - '0') / (div *= 10);
				}
			}
			if (consume) {
				consumeSpaces();
			}
			if (neg) {
				return -ret;
			}
			return ret;
		}

		public int[] nextIntArray(int n) throws IOException {
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = nextInt();
			}
			return a;
		}

		public int[][] nextIntMatrix(int n, int m) throws IOException {
			int[][] grid = new int[n][m];
			for (int i = 0; i < n; i++) {
				grid[i] = nextIntArray(m);
			}
			return grid;
		}

		public char[][] nextCharacterMatrix(int n) throws IOException {
			char[][] a = new char[n][];
			for (int i = 0; i < n; i++) {
				a[i] = next().toCharArray();
			}
			return a;
		}

		public void close() throws IOException {
			if (System.in == null) {
				return;
			} else {
				System.in.close();
			}
		}
	}

	static Reader r = new Reader();

	static boolean biPartiteGraphCheck(int V, ArrayList<Integer> graph[]) {
		boolean vis[] = new boolean[V + 1];
		int color[] = new int[V + 1];
		for (int i = 1; i <= V; i++) {
			if (!vis[i]) {
				boolean flag = biPartiteGraphHelper(i, vis, color, graph);
				if (!flag)
					return false;
			}
		}
		return true;
	}

	static boolean biPartiteGraphHelper(int u, boolean vis[], int color[], ArrayList<Integer> graph[]) {
		vis[u] = true;
		for (Integer v : graph[u]) {
			if (!vis[v]) {
				color[v] = 1 - color[u];
				if (!biPartiteGraphHelper(v, vis, color, graph)) {
					return false;
				}
			} else if (color[v] == color[u])
				return false;
		}
		return true;
	}

	private static void solve1() throws IOException {

		int t = r.nextInt();
		StringBuilder res = new StringBuilder();
		int j = 1;

		while (t-- > 0) {

			int n = r.nextInt();
			int m = r.nextInt();

			ArrayList<Integer> graph[] = new ArrayList[n + 1];
			for (int i = 0; i <= n; i++) {
				graph[i] = new ArrayList<Integer>();
			}

			for (int i = 1; i <= m; i++) {
				int u = r.nextInt();
				int v = r.nextInt();
				graph[u].add(v);
				graph[v].add(u);
			}

			res.append("Scenario #" + j++ + ":").append("\n");
			res.append(biPartiteGraphCheck(n, graph) == true ? "No suspicious bugs found!" : "Suspicious bugs found!")
					.append("\n");
		}

		System.out.print(res);

	}

	static int dist[];
	static boolean vis[];
	static ArrayList<Integer> graph[];

	private static void BFS(int start) {
		Queue<Integer> qu = new LinkedList<Integer>();
		qu.add(start);
		vis[start] = true;
		while (!qu.isEmpty()) {
			int u = qu.poll();
			for (int v : graph[u])
				if (!vis[v]) {
					qu.add(v);
					vis[v] = true;
					dist[v] = dist[u] + 1;
				}
		}
	}

	// monk and the islands
	private static void solve2() throws IOException {

		int t = r.nextInt();
		StringBuilder res = new StringBuilder();

		while (t-- > 0) {

			int n = r.nextInt();
			int m = r.nextInt();

			graph = new ArrayList[n + 1];
			dist = new int[n + 1];
			vis = new boolean[n + 1];

			for (int i = 0; i <= n; i++) {
				graph[i] = new ArrayList<Integer>();
			}

			for (int i = 1; i <= m; i++) {
				int u = r.nextInt();
				int v = r.nextInt();
				graph[u].add(v);
				graph[v].add(u);
			}

			BFS(1);

			res.append(dist[n]).append("\n");
		}

		System.out.print(res);

	}

	static class Subset {
		private int parent;
		private int rank;

		public Subset(int v, int u) {
			parent = v;
			rank = u;
		}
	}

	static int find(Subset subsets[], int i) {
		if (subsets[i].parent != i)
			subsets[i].parent = find(subsets, subsets[i].parent);
		return subsets[i].parent;
	}

	static void Union(Subset subsets[], int x, int y) {
		int xroot = find(subsets, x);
		int yroot = find(subsets, y);

		if (subsets[xroot].rank < subsets[yroot].rank)
			subsets[xroot].parent = yroot;
		else if (subsets[xroot].rank > subsets[yroot].rank)
			subsets[yroot].parent = xroot;
		else {
			subsets[yroot].parent = xroot;
			subsets[xroot].rank++;
		}
	}

	private static void solve3() throws IOException {

		int t = r.nextInt();
		StringBuilder res = new StringBuilder();

		while (t-- > 0) {

			int n = r.nextInt();
			int k = r.nextInt();

			Subset equal[] = new Subset[n + 1];
			for (int i = 0; i <= n; i++) {
				equal[i] = new Subset(i, 0);
			}

			Subset unequal[] = new Subset[n + 1];
			for (int i = 0; i <= n; i++) {
				unequal[i] = new Subset(i, 0);
			}

			boolean flag = true;
			while (k-- > 0) {

				int num1 = r.nextInt();
				String op = r.next();
				int num2 = r.nextInt();

				if (flag) {
					if (op.equals("=")) {

						int u = find(unequal, num1);
						int v = find(unequal, num2);

						if (u == v) {
							flag = false;
						} else {
							Union(equal, u, v);
						}

					} else {

						int u = find(equal, num1);
						int v = find(equal, num2);

						if (u == v) {
							flag = false;
						} else {
							Union(unequal, u, v);
						}

					}
				}

			}

			res.append(flag ? "YES" : "NO").append("\n");
		}

		System.out.print(res);

	}

	static int mod = (int) 1e9 + 7;

	static long MOD(long a) {
		long r = a % mod;
		return r < 0 ? r + mod : r;
	}

	private static void solve4() throws IOException {

		long x = r.nextLong();
		long y = r.nextLong();
		long n = r.nextLong();

		long a = n / 3;
		long idx = n % 3;

		if (idx == 0) {
			if ((a & 1) != 0) {
				System.out.println(MOD(y - x));
			} else {
				System.out.println(MOD(x - y));
			}
		} else {
			if ((a & 1) != 0) {
				if (idx == 1)
					System.out.println(MOD(-x));
				else if (idx == 2)
					System.out.println(MOD(-y));
			} else {
				if (idx == 1)
					System.out.println(MOD(x));
				else if (idx == 2)
					System.out.println(MOD(y));
			}
		}

	}

//	static class Pair<T, V> {
//		T fi;
//		V se;
//
//		public Pair(T a, V b) {
//			fi = a;
//			se = b;
//		}
//
//		public String toString() {
//			return ("{" + this.fi + "," + this.se + "}");
//		}
//	}

	static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> {

		public U fi;
		public V se;

		public Pair(U fi, V se) {
			this.fi = fi;
			this.se = se;
		}

		public int compareTo(Pair<U, V> that) {
			int value = this.fi.compareTo(that.fi);
			if (value != 0)
				return value;
			return this.se.compareTo(that.se);
		}

		@Override
		public boolean equals(Object other) {
			if (!(other instanceof Pair)) {
				return false;
			}
			Pair that = (Pair<U, V>) other;
			return this.fi.equals(that.fi) && this.se.equals(that.se);
		}

		@Override
		public int hashCode() {
			return 31 * (527 + fi.hashCode()) + se.hashCode();
		}

		@Override
		public String toString() {
			return "{" + fi + ", " + se + "}";
		}
	}

	private static void solve5() throws IOException {

		int n = r.nextInt();

		Pair<Long, Long> arr[] = new Pair[n];

		for (int i = 0; i < n; i++) {
			arr[i] = new Pair<>(r.nextLong(), r.nextLong());
		}

		Arrays.sort(arr);

		long curr = 0;
		for (int i = 0; i < n; i++) {

			long min = Math.min(arr[i].se, arr[i].fi);
			long max = Math.max(arr[i].se, arr[i].fi);

			if (min >= curr) {
				curr = min;
			} else {
				curr = max;
			}

		}

		System.out.println(curr);

	}

	private static void solve6() throws IOException {

		int t = r.nextInt();
		StringBuilder res = new StringBuilder();

		while (t-- > 0) {

//			int n = r.nextInt();
			String str = r.nextLine();
			res.append(str).append("\n");

		}

		System.out.print(res);

	}

	private static void solve7() throws IOException {

		int t = r.nextInt();
		StringBuilder res = new StringBuilder();

		while (t-- > 0) {

			int n = r.nextInt();

			res.append(false).append("\n");
		}

		System.out.print(res);

	}

	public static void main(String[] args) throws IOException {

//		Bug's Life(SPOJ)
//		solve1();

//		monk and the islands
//		solve2();

//		Feasible Relations
//		solve3();

//		juhu and sequences
//		solve4();

//		Exams
		solve5();

//		solve6();

		// solve7();

	}

}