Problem:
Dungeon

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.

Sample Input 1

5 4 4
0 3
1 1
2 2
2 3

Sample Output 1

2

Sample Input 2

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

Sample Output 2

4

Sample Input 3

8 8 4
6 0
7 0
0 6
0 7

Sample Output 3

0

Solution in Haskell (judge verdict: Memory Limit Exceeded)

import Data.Array
import Data.List
import Data.List.Split


type Width = Int
type Height = Int
type Enemy = (Int,Int)
type Player = (Int,Int)
type Cost = Int
type Situation = (Width, Height, Player, [Enemy], Cost)

main = do
  input <- getSplitedLines
  let (w:h:_) = head input
  let enemys = map (\(a:b:_) -> (a, b)) $ drop 1 input
  let player = (0,0)
  let cost = 0
  let minimumCost = calcMinimumCost (w, h, player, enemys, cost)
  putStrLn $ show minimumCost

calcMinimumCost :: Situation -> Cost
calcMinimumCost situation = calcMinimumCost' [explosion situation]
  where
    calcMinimumCost' :: [Situation] -> Cost
    calcMinimumCost' situations = case getEnd situations of
      Nothing -> calcMinimumCost' nextSituations
      Just (_, _, _, _, c)  -> c
      where
        nextSituations = situations >>= getNextSituation

getNextSituation :: Situation -> [Situation]
getNextSituation situation = map explosion $ getNextMove situation

getNextMove :: Situation -> [Situation]
getNextMove (w, h, (px,py), es, c)
  | px < w-1 && py < h-1 = [(w, h, (px + 1,py), es, nextCost), (w, h, (px,py + 1), es, nextCost)]
  | px < w-1             = [(w, h, (px + 1,py), es, nextCost)]
  | py < h-1             = [(w, h, (px,py + 1), es, nextCost)]
  | otherwise			 = []
  where 
    nextCost = c + 1

explosion :: Situation -> Situation
explosion (w, h, (px,py), es, c) = (w, h, (px,py), filter (not.isHit) es, c)
  where
    isHit (ex,ey) = px == ex || py == ey

getEnd :: [Situation] -> Maybe Situation
getEnd [] = Nothing
getEnd ss = safeHead $ filter isEnd ss

isEnd :: Situation -> Bool
isEnd (_, _, _, [], _) = True
isEnd (_, _, _, _, _) = False
    
safeHead [] = Nothing
safeHead (a:_) = Just a

getSplitedLines =  do
  s <- getContents
  return $ map (map read) $ map (splitOn " ") $ lines s