This page is still under construction.

Parts of this page are still being built. What you see may change.

Space Station Promenade

Time limit1sMemory limit1024 MB

Summary
A circular station has m windows on given modules that must be visited in order from module 1 and back; minimize total travel subject to equal clockwise and counterclockwise distances.
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum, Greedy, Implementation
Solved
No attempts yet

Problem

Astronaut Gustav works on a space station made of nn modules joined in a circle, so that module 11 is joined to module 22, module 22 to module 33, and so on, with module nn joined to module 11. The distance between two neighboring modules is 11. To create artificial gravity, the station rotates at a constant speed around the center of the circle.

The station has been in space for a long time, and it is time to clean the outside of the windows. The lot has fallen on Gustav to do this. There are mm windows numbered from 11 to mm, where window ii is on module aia_i. For some reason, the windows must be cleaned in exactly this order. The only entrance and exit to the station is at module 11.

To move between modules there is a rocket-powered window elevator that travels along the outside of the station. The elevator can only move between neighboring modules, so it cannot take any shortcuts. Gustav wants to choose a route from module 11, around to all the windows, and back to module 11. Unfortunately there are two problems: first, the elevator has limited fuel, so Gustav must choose a route that minimizes the distance he travels. Second, the elevator's movements affect the station's rotation, so it must travel the same distance clockwise as counterclockwise.

Find the smallest distance Gustav can travel so that he starts at module 11, visits all the windows in the correct order, returns to module 11, and travels the same distance counterclockwise as clockwise.

Input

The first line contains two integers nn and mm: the number of modules and the number of windows (3≤n≤1053 \leq n \leq 10^5 , 1≤m≤1051 \leq m \leq 10^5). The second line contains mm integers, the indices of the modules the windows are on (1≤ai≤n1 \leq a_i \leq n).

Output

One integer, the smallest distance.

Examples3

  1. Example 1

    Input
    8 4
    2 4 3 6
    
    Expected output
    12
    
  2. Example 2

    Input
    5 4
    1 2 2 2
    
    Expected output
    2
    
  3. Example 3

    Input
    8 5
    4 6 8 2 7
    
    Expected output
    16