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 <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#define ll long long
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define ROF(i,a,b) for(int i=b-1;i>=a;i--)
using namespace std;
//
const int inf=1e9+7;
const int mod=1e9+7;
//
struct poi{
	int X;int Y;int Z;
	bool operator<(const poi&R)const{
		return X==R.X ? Y==R.Y ? Z<R.Z : Y<R.Y : X<R.X;
	}
};
//
ll GCD(ll a,ll b){
	return (b==0)?(a):(GCD(b,a%b));
}
ll LCM(ll a,ll b){
	return a/GCD(a,b)*b;
}
//
//
int main(){
	int W,H,N;
	cin>>W>>H>>N;
	vector<pair<int,int> > V(N);
	pair<int,int> cnt[100000];
	fill(cnt,cnt+W,make_pair(-1,-1));
	FOR(i,0,N){
		int X,Y;
		cin>>X>>Y;
		if(cnt[X].first<Y){
			cnt[X]=make_pair(Y,i);
		}
		V[i]=make_pair(X,Y);
	}
	vector<pair<int,int> > vv;
	FOR(i,0,W){
		if(cnt[i].first!=-1){
			vv.push_back(V[cnt[i].second]);
		}
	}
	sort(vv.rbegin(),vv.rend());
	int now=-1;
	vector<pair<int,int> > v;
	FOR(i,0,vv.size()){
		if(now<vv[i].second){
			v.push_back(vv[i]);
			now=vv[i].second;
		}
	}
	reverse(v.begin(),v.end());
	if((int)v.size()==1){
		cout<<min(v[0].first,v[0].second)<<endl;
	}else{
		int ans=min(v[0].second,v[v.size()-1].first);
		FOR(i,0,v.size()-1){
			ans=min(ans,v[i].first+v[i+1].second);
		}
		cout<<ans<<endl;
	}
	return 0;
}