Going to Meet Sina

Interview

Time limit1sMemory limit128 MB

Summary
Given up to 10^4 blocked cells on a bounded grid, find the shortest 4-directional path from (0,0) to (X,Y) avoiding all puddles.
Level

Medium4 of 10

Topics
BFS, Graph, Shortest path, Hash map
Solved
No attempts yet

Problem

Kipa set out early in the morning to meet Sina. Heavy rain had fallen overnight, so Kipa put on brand-new rain boots and left home at (0,0)(0, 0), only to find that NN puddles had appeared. The ii-th puddle is located at (Ai,Bi)(A_i, B_i), and Kipa knows the position of every puddle.

Kipa wants to reach Sina's house at (X,Y)(X, Y) as quickly as possible, but because the boots are new, Kipa does not want to step on any puddle. Kipa can move only one cell at a time in the four directions up, down, left, and right. Find the minimum travel distance (the number of cells moved) needed to reach Sina's house without stepping on a puddle. Assume it is never necessary to step on a puddle in order to reach Sina's house.

Constraints: 1≤N≤1041 \le N \le 10^4, ∣Ai∣≤500|A_i| \le 500, ∣Bi∣≤500|B_i| \le 500.

Input

The first line contains XX, YY, and NN, separated by spaces.

Each of the next NN lines contains the coordinates AiA_i and BiB_i of the ii-th puddle, separated by a space.

Output

Print, on the first line, the minimum travel distance to reach Sina's house without stepping on a puddle.

Hint

Sina's house is at (1,2)(1, 2). The figure below shows a situation with 7 puddles. M marks a puddle, B marks Sina's house, and * marks Kipa's starting point (0,0)(0, 0).

   4 . . . . . . . .
   3 . M . . . . . .
Y  2 . . M B M . M .
   1 . M . M . M . .
   0 . . * . . . . .
  -1 . . . . . . . .
    -2-1 0 1 2 3 4 5
           X

The shortest route is shown by the cells marked * below, and its length is 1111.

   4 ******* . . . .
   3 * M . * . . . .
Y  2 * . M B M . M .
   1 * M . M . M . .
   0 ***** . . . . .
  -1 . . . . . . . .
    -2-1 0 1 2 3 4 5

           X

Examples1

  1. Example 1

    Input
    1 2 7
    0 2
    -1 3
    3 1
    1 1
    4 2
    -1 1
    2 2
    
    Expected output
    11