프로그램 명: coci_dostava
제한시간: 3 초

Little Ivica recently got a job delivering pizzas for the most popular pizzeria in town. At the start of his work day, he receives a list with the locations to which he needs to deliver pizzas, in order in which the locations are given.

The city is divided into R×C cells. The rows are numbered 1 through R, columns 1 through C. From every cell, it is possible to move to neighbouring cells to the left and right. Moving up or down is only allowed in the first and last columns (columns 1 and C).

The pizzeria is in the top left corner (1, 1) and this is the location Ivica starts from. Ivica takes with him all the pizzas he will deliver that day so he does not have to return to the pizzeria between deliveries or after the last delivery.

For each location in the city, Ivica knows how much time he will spend every time he is in it (trying to get through the intersection, for example).

Write a program that calculates the smallest amount of time for Ivica to deliver all the pizzas.

입력

Each of the following D lines contains two integers A and B (1 ≤ A ≤ R, 1 ≤ B ≤ C), the location to which a pizza must be delivered. The pizzas are given in the order in which they must be delivered. No location will be given twice in a row.

출력

Output the smallest amount of time for Ivica to deliver all the pizzas.

입출력 예


input 

3 3 
1 8 2 
2 3 2 
1 0 1 
3 
1 3 
3 3 
2 2 

output 

17 

input 

2 5 
0 0 0 0 0 
1 4 2 3 2 
4 
1 5 
2 2 
2 5 
2 1 

output 

9 

sample

In the first example, the shortest path goes through the following locations: (1, 1), (2, 1), (3, 1), (3, 2), (3, 3), (2, 3), (1, 3), (2, 3), (3, 3), (2, 3) and (2, 2). The locations in bold show where Mirko made deliveries. The total time for the deliveries is 1+2+1+0+1+2+2+2+1+2+3=17.
출처: coci 2008/2009 contest6 4/6

[질/답] [제출 현황] [푼 후(0)]
[ 채 점 ] [홈으로]  [뒤 로]