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>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
struct segtree{
private:
  ll siz;
  vector<ll> node;
public:
  segtree(ll n){
    siz=1;
    while(siz<=n) siz*=2;
    node.resize(siz*2+1,0);
  }
  void upd(int p,int x){
    p+=siz-1;
    node[p]=x;
    while(p>0){
      p=(p-1)/2;
      node[p]=max(node[2*p+1],node[2*p+2]);
    }
  }
  ll max_v(ll a,ll b,ll p,ll l,ll r){
    if(a<=l&&r<=b) return node[p];
    if(r<=a||b<=l) return 0;
    ll mid=(l+r)/2;
    return max(max_v(a,b,2*p+1,l,mid),max_v(a,b,2*p+2,mid,r));
  }
  ll max_q(ll a){
    return max_v(a,siz,0,0,siz);
  }
};
int main(){
  vector<ll> vec;
  vector<P> points;
  ll h,w,n;
  cin>>h>>w>>n;
  for(int i=0;i<n;i++){
    ll x,y;
    cin>>x>>y;
    points.push_back(P(x,y));
    vec.push_back(x);
  }
  vec.push_back(0);
  points.push_back(P(0,0));
  sort(vec.begin(),vec.end());
  sort(points.begin(),points.end());
// cout<<0<<" "<<points[0].first<<" "<<points[0].second<<endl;
  segtree seg=segtree(n+5);
  for(int i=1;i<=n;i++){
    seg.upd(i,points[i].second);
  //  c//out<<i<<" "<<points[i].first<<" "<<points[i].second<<endl;
  }
  ll ans=17441744174417;
  for(int i=0;i<vec.size();i++){
    ll kans=vec[i];
    ll p=lower_bound(points.begin(),points.end(),P(kans+1,0))-points.begin();
  //  cout<<i<<" "<<kans<<" "<<p<<" "<<seg.max_q(p)<<endl;
    kans+=seg.max_q(p);
    ans=min(ans,kans);
  }
//  ans=min(ans,vec[])
  cout<<ans<<endl;
}