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 <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#define INF 2000000000
using namespace std;
int dx[4]={0,1,0,-1},dy[4]={-1,0,1,0};
#define sym cout<<"---------"<<endl;
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll

#define MAX 100005

int w,h,n;

// first = yoko / second = tate
P p[100005];

int yoko[MAX];
int tate[MAX];
int max_yoko[MAX];
int max_tate[MAX];

signed main(){
  cin>>w>>h>>n;
  for(int i=0; i<n; i++){
    cin>>p[i].first>>p[i].second;
    yoko[p[i].second]=max(yoko[p[i].second],p[i].first);
    tate[p[i].first]=max(tate[p[i].first],p[i].second);
  }
  
  for(int i=h-1; i>=0; i--){
    max_yoko[i]=max(max_yoko[i+1],yoko[i]);
  }
  
  for(int i=w-1; i>=0; i--){
    max_tate[i]=max(max_tate[i+1],tate[i]);
  }
  
  int ans=INF;
  bool is0_tate=true;
  for(int i=1; i<w; i++){
    ans=min(ans,i+max_tate[i+1]);
    if(max_tate[i+1]) is0_tate=false;
  }
  
  bool is0_yoko=true;
  for(int i=1; i<h; i++){
    ans=min(ans,i+max_yoko[i+1]);
    if(max_yoko[i+1]) is0_yoko=false;
  }
  
  if(is0_yoko&&is0_tate){
    cout<<0<<en;
    RE;
  }
  cout<<ans<<en;
}