Problem: Bob is playing a popular game called "Dungeon". The game is played on a rectangular board consisting of W × H squares. Each square is identified with its column and row number, thus the square located in the x-th column and the y-th row is represented as (x, y). The left-most square in the top row is (0, 0) and the right-most square in the bottom row is (W-1, H-1).

Bob moves a character "BomBom" to clear the game. BomBom is initially located at (0, 0). The game is won if Bob successfully destroys all the enemy characters on the board by manipulating BomBom cleverly. The enemy characters are fixed on specific squares, and Bob can manipulate BomBom using the following two operations any number of times.

* One-square movement in the up, down, left, or right direction within the board
* Using a bomb, eliminate all the enemy characters that are located in the same column and row as that of BomBom



BomBom consumes a Cost when it moves from one square to another. BomBom can use a bomb any number of times without consuming a Cost. Use of a bomb has no effect on BomBom’s behavior and it can move even to a square where an enemy character is located.

Given the board size and enemy information, make a program to evaluate the minimum Cost BomBom consumes before it destroys all enemy characters.



Input

The input is given in the following format.


W H N
x_1 y_1
x_2 y_2
:
x_N y_N


The first line provides the number of squares in the horizontal direction W (1 ≤ W ≤ 105), in the vertical direction H (1 ≤ H ≤ 105), and the number of enemy characters N (1 ≤ N ≤ 105). Each of the subsequent N lines provides location information of the i-th enemy, column x_i (0 ≤ x_i ≤ W-1) and row y_i (0 ≤ y_i ≤ H-1). The number of enemy characters in a specific square can be either one or zero.

Output

Output the minimum Cost in a line.

Examples

Input

5 4 4
0 3
1 1
2 2
2 3


Output

2


Input

6 6 5
2 1
5 2
3 3
1 4
1 5


Output

4


Input

8 8 4
6 0
7 0
0 6
0 7


Output

0

Solution (in CPP):

Solution:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

struct point
{
	LL x, y;
	point(LL xx, LL yy) :x(xx), y(yy)
	{}
};
LL W, H, N;
vector<point>xsort;//x??????????????????????????????
vector<point>ysort;//y??????????????????????????????

				   //?????????P??????s
bool cond(LL P)
{
	point now(P, 0);
	LL seive = 0;
	for (point teki : xsort)
	{
		if (teki.x <= now.x || teki.y <= now.y)
		{
			++seive;
		}
	}
	if (seive == N)
	{
		return true;
	}
	for (int k = 0; k < P; ++k)
	{
		now.x--;
		now.y++;
		auto plussleft = lower_bound(ysort.begin(), ysort.end(), point(1145148101919, now.y), [](const point&p1, const point&p2)
		{
			if (p1.y == p2.y)return p1.x < p2.x; return p1.y < p2.y;
		});
		auto plussright = lower_bound(ysort.begin(), ysort.end(), point(now.x + 2, now.y), [](const point&p1, const point&p2)
		{
			if (p1.y == p2.y)return p1.x < p2.x; return p1.y < p2.y;
		});
		int pluss = plussleft - plussright;
		int minus = lower_bound(xsort.begin(), xsort.end(), point(now.x + 1, 1145148101919), [](const point&p1, const point&p2)
		{
			if (p1.x == p2.x)return p1.y < p2.y; return p1.x < p2.x;
		})
			- lower_bound(xsort.begin(), xsort.end(), point(now.x + 1, now.y + 1), [](const point&p1, const point&p2)
		{
			if (p1.x == p2.x)return p1.y < p2.y; return p1.x < p2.x;
		});
		seive += pluss;
		seive -= minus;
		if (seive == N)
		{
			return true;
		}
	}
	return false;
}

int main(void)
{
	cin >> W >> H >> N;
	for (int i = 0; i < N; ++i)
	{
		int x, y;
		cin >> x >> y;
		xsort.push_back({ x,y });
		ysort.push_back({ x,y });
	}
	sort(xsort.begin(), xsort.end(), [](const point&p1, const point&p2)
	{
		if (p1.x == p2.x)return p1.y < p2.y; return p1.x < p2.x;
	});
	sort(ysort.begin(), ysort.end(), [](const point&p1, const point&p2)
	{
		if (p1.y == p2.y)return p1.x < p2.x; return p1.y < p2.y;
	});
	int lo = 0, hi = W + H + 1;
	if (cond(0))
	{
		cout << 0 << endl;
		return 0;
	}
	while (lo + 1 < hi)
	{
		int mid = (lo + hi) / 2;
		if (cond(mid))
		{
			hi = mid;
		}
		else
		{
			lo = mid;
		}
	}
	cout << hi << endl;
	return 0;
}