Under construction

Still being built. Things may change

AtCoder Study

Time limit1sMemory limit512 MB

Summary
Seunghyeong starts at building N in a row of 2N-1 buildings and must collect M scattered Gunsu using steps of 1 or N, with free returns to visited buildings. The task asks for the minimum time.
Level

Medium7 of 10

Topics
Graph, Shortest path, Math
Solved
No attempts yet

Problem

Seogang Village is a straight village with a total of 2N−12N-1 buildings in a row, numbered from 1 to 2N−12N-1 from left to right. MM of Gunsu live in this village, along with Seunghyeong. All of the Gunsu are in different buildings, and Seunghyeong is in building NN, where K512 is located. The Gunsu and Seunghyeong hold an AtCoder study every Monday. Five minutes before the study starts, not a single Gunsu has arrived! Angry, Seunghyeong plans to use a secret special movement method to bring back the Gunsu scattered around the village to K512.

Seunghyeong can use the following three movement methods.

  1. He can move to a neighboring building in 1 second. That is, he can move from building ii to building i+1i+1 or building i−1i-1. If he is in building 1 or building 2N−12N-1, he can only move to building 2 or building 2N−22N-2, respectively.
  2. He can skip exactly NN buildings in 1 second. That is, for i>Ni > N, he can move from building ii to building i−Ni-N, and for i<Ni < N, he can move from building ii to building i+Ni+N.
  3. He can instantly move to any building he has visited before, without spending any time. However, buildings skipped while using method 2 do not count as visited.

When Seunghyeong reaches a building with a Gunsu, he picks up the Gunsu right away, so collecting them takes no extra time. Let us calculate the minimum time needed to bring back all the Gunsu.

Input

The first line contains the integers NN and MM. (2≤N≤1062 \le N \le 10^6; 1≤M≤2N−21 \le M \le 2N-2)

The second line contains the positions a1a_1 through aMa_M of the Gunsu, separated by spaces, where aia_i is the position of the ii-th Gunsu. All positions are distinct, and ai=Na_i = N never happens. (1≤ai≤2N−11 \le a_i \le 2N-1)

Output

Print the minimum time Seunghyeong needs to bring back all the Gunsu.

Examples2

  1. Example 1

    Input
    4 2
    1 7
    
    Expected output
    4
    
  2. Example 2

    Input
    8 8
    1 2 5 11 13 9 3 12
    
    Expected output
    8