cho.sh
NotesCho Mini
Loading...

Security Guard

Time limit

1s

Memory limit

128 MB

Problem

Donggeun is a security guard for an unmanned security company. Whenever a call comes in, he must drive his patrol car to the requested store. His patrol area is a rectangular block, and there is no road through the inside of the block. Therefore, he can move only along the boundary of the block. Stores that requested unmanned security service are located on this boundary.

Suppose, for instance, that a block has width 10 and height 5, and that three stores are located on its boundary. In the figure below, the stores are marked 1, 2, and 3, and Donggeun's position is marked X.

If store 1 calls, Donggeun travels distance 12 by moving clockwise around the block, and distance 18 by moving counterclockwise. Thus the shortest distance from his position to store 1 is 12. In the same way, the shortest distances from Donggeun's position to stores 2 and 3 are 6 and 5.

Given the size of the block, the number and positions of the stores, and Donggeun's position, compute the sum of the shortest distances from Donggeun to every store.

Input

The first line contains the width W and height H of the block, in that order. The second line contains the number of stores N. W, H, and N are natural numbers no greater than 100.

Each of the next N lines contains the position of one store. A position is written as two natural numbers d and x.

  • d = 1 means the store is on the north side, and x is the distance from the left boundary.
  • d = 2 means the store is on the south side, and x is the distance from the left boundary.
  • d = 3 means the store is on the west side, and x is the distance from the top boundary.
  • d = 4 means the store is on the east side, and x is the distance from the top boundary.

The last line contains Donggeun's position in the same format. No store position and not Donggeun's position is a corner of the block.

Output

Print one integer: the sum of the shortest distances from Donggeun's position to each store.