Problem:
Score : 300 points

Problem Statement

There is a circular pond with a perimeter of K meters, and N houses around them.

The i-th house is built at a distance of A_i meters from the northmost point of the pond, measured clockwise around the pond.

When traveling between these houses, you can only go around the pond.

Find the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Constraints

2 \leq K \leq 10^6

2 \leq N \leq 2 \times 10^5

0 \leq A_1 < ... < A_N < K

All values in input are integers.

Input

Input is given from Standard Input in the following format:

K N
A_1 A_2 ... A_N

Output

Print the minimum distance that needs to be traveled when you start at one of the houses and visit all the N houses.

Sample Input 1

20 3
5 10 15

Sample Output 1

10

If you start at the 1-st house and go to the 2-nd and 3-rd houses in this order, the total distance traveled will be 10.

Sample Input 2

20 3
0 5 15

Sample Output 2

10

If you start at the 2-nd house and go to the 1-st and 3-rd houses in this order, the total distance traveled will be 10.

Solution in Rust (1.15.1) (judge verdict: Accepted)

#![allow(non_snake_case)]
#![allow(unused_imports)]
macro_rules!input{(source=$s:expr,$($r:tt)*)=>{let mut iter=$s.split_whitespace();let mut next=||{iter.next().unwrap()};input_inner!{next,$($r)*}};($($r:tt)*)=>{let stdin=std::io::stdin();let mut bytes=std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next=move||->String{bytes.by_ref().map(|r|r.unwrap()as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next,$($r)*}};}macro_rules!input_inner{($next:expr)=>{};($next:expr,)=>{};($next:expr,$var:ident:$t:tt$($r:tt)*)=>{let $var=read_value!($next,$t);input_inner!{$next$($r)*}};}macro_rules!read_value{($next:expr,($($t:tt),*))=>{($(read_value!($next,$t)),*)};($next:expr,[$t:tt;$len:expr])=>{(0..$len).map(|_|read_value!($next,$t)).collect::<Vec<_>>()};($next:expr,chars)=>{read_value!($next,String).chars().collect::<Vec<char>>()};($next:expr,usize1)=>{read_value!($next,usize)-1};($next:expr,$t:ty)=>{$next().parse::<$t>().expect("Parseerror")};}

use std::cmp::{min, max};
use std::collections::{HashMap, HashSet, VecDeque};
use std::mem::swap;


fn main() {
    input!{
        N: u32,
        L: usize,
        A: [u32; L],
    }
    let ans = solve((N,L,A,));
    println!("{}", ans);
}

fn solve((L,N, A,): (u32,usize, Vec<u32>)) -> u32 {
    let a = A[0];
    let mut A = A;
    A.push(a + L);
    let A = A;
    let diff: Vec<_> = A.iter().zip(A[1..].iter()).map(|(a,b)| b-a).collect();
    L-diff.iter().max().unwrap()
}


#[test]
fn test_solve() {
    //let cases = [("CBABA", 29), ("HEART", 55), ("EARTH", 28), ("IOI", 2)];
    //for (i, (S, a)) in cases.iter().enumerate() {
    //    assert_eq!(solve((S.as_bytes().to_vec(),)), *a, "#{}", i);
    //}
}


/*
    Copyright for the `input!` macro at line #3 is held by Hideyuki Tanaka, 2019.  The
    original macro is licensed under BSD 3-clause license.
 
    Redistribution and use in source and binary forms, with or without
    modification, are permitted provided that the following conditions are met:
 
    1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
 
    2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
    3. Neither the name of the copyright holder nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.
 
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/