Bribing the Prisoners

Release Q prisoners from a row of P cells in the order that minimizes bribes paid to neighbors reached by the news. Find that minimum total cost.

Medium7Dynamic programmingIntervalsDivide and conquerInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

A prison has PP cells in one row, numbered 1,2,,P1, 2, \dots, P from the left. Every cell is a solitary cell and holds exactly one prisoner. Neighboring cells share a window, so a prisoner can talk to the prisoner next door.

When you release the prisoner of some cell, the prisoner in each cell right next to it learns about it and starts a riot. Releasing one prisoner therefore costs one gold coin for the prisoner in each of the two neighboring cells. The news keeps traveling sideways from window to window, so you have to pay every prisoner the news reaches. An empty cell has no prisoner to pass the news along, so the news stops there.

Today you release the QQ prisoners held in cells A1,A2,,AQA_1, A_2, \dots, A_Q. The number of coins depends on the order of the releases. Find the order that spends the fewest coins and report how many coins that order needs.

Input

The first line contains two integers PP and QQ separated by a space. (1P100001 \le P \le 10\,000, 1Q1001 \le Q \le 100, QPQ \le P)

The second line contains QQ integers A1,A2,,AQA_1, A_2, \dots, A_Q separated by spaces. Each value is the cell number of a prisoner to release, and no value is given twice. (1AiP1 \le A_i \le P)

Output

Print on one line the minimum number of gold coins needed to release all QQ prisoners.