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<algorithm>
#include<vector>
template<typename T, T INF>
class SegmentTree {
 public:
  SegmentTree(int n) : size_(1) {
    while(size_ < n) size_ <<= 1;
    node_.assign(size_ << 1, 0);
    lazy_.assign(size_ << 1, 0);
  }
  T find(int left, int right) {return find(left, right + 1, 1, 0, size_);}
  void add(int left, int right, T value) {add(left, right + 1, value, 1, 0, size_);}
 private:
  inline int left_child(int node) const {return node << 1;}
  inline int right_child(int node) const {return (node << 1) + 1;}
  inline T merge(T vl, T vr) const {return std::max(vl, vr);}
  inline void propagate(int k) {
    if(k < size_) {
      lazy_.at(left_child(k)) += lazy_.at(k);
      lazy_.at(right_child(k)) += lazy_.at(k);
    }
    node_.at(k) += lazy_.at(k);
    lazy_.at(k) = 0;
  }
  T find(int a, int b, int k, int l, int r) {
    propagate(k);
    if(b <= l || r <= a) return -INF;
    if(a <= l && r <= b) return node_.at(k) + lazy_.at(k);
    auto vl = find(a, b, left_child(k), l, (l + r) / 2);
    auto vr = find(a, b, right_child(k), (l + r) / 2, r);
    return merge(vl, vr);
  }
  T add(int a, int b, T value, int k, int l, int r) {
    propagate(k);
    if(b <= l || r <= a) return node_.at(k);
    if(a <= l && r <= b) return node_.at(k) + (lazy_.at(k) += value);
    auto vl = add(a, b, value, left_child(k), l, (l + r) / 2);
    auto vr = add(a, b, value, right_child(k), (l + r) / 2, r);
    return node_.at(k) = merge(vl, vr);
  }
  int size_;
  std::vector<T> node_;
  std::vector<T> lazy_;
};

#include<bits/stdc++.h>
using namespace std;

int main() {
  const int INF = 2e5;

  int W, H, N;
  cin >> W >> H >> N;

  SegmentTree<int, INF> T(INF);
  for(auto i = 0; i < N; ++i) {
    int x, y;
    cin >> x >> y;
    if(T.find(x, x) < y) T.add(x, x, y - T.find(x, x));
  }

  int res = INF;

  for(auto x = 0; x < INF; ++x) {
    res = min(res, x + T.find(x + 1, INF));
  }

  cout << res << endl;
}