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)

use std::io::{self, BufReader, Read};

// fn read_line() -> String {
//     let mut s = String::new();
//     std::io::stdin().read_line(&mut s).unwrap();
//     s
// }

// fn split_whitespace(line: String) -> Vec<u32> {
//     line.split_whitespace()
//         .map(|s| s.parse().unwrap())
//         .collect()
// }

// fn reader() -> Vec<usize> {
//     let mut buffer = String::new();
//     io::stdin().read_to_string(&mut buffer);

//     let st: Vec<&str> = buffer.lines().collect();

//     let s: Vec<&str> = st.get(1).unwrap().clone().split_terminator(" ").collect();

//     s.iter()
//         .map(|v| v.trim().parse::<usize>().unwrap())
//         .collect()
// }

// fn solver(mut hs: Vec<usize>) -> usize {
//     let mut v: Vec<usize> = vec![0; hs.len()];;

//     for i in 1..hs.len() {
//         if hs[i - 1] >= hs[i] {
//             v[i] = v[i - 1] + 1;
//         }
//     }

//     let max = v.iter().max().unwrap().clone();
//     println!("{}", max);
//     max
// }

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}

fn eval_ds(ds: Vec<usize>) -> Vec<usize> {
    let mut v = Vec::new();
    for sl in ds.windows(2) {
        v.push(sl[1] - sl[0]);
    }
    v
}

fn main() {
    let mut sc = IO::new(std::io::stdin(), std::io::stdout());
    let p: usize = sc.read();
    let h_n: usize = sc.read();
    let mut ds: Vec<usize> = sc.vec(h_n);
    ds.sort();
    let last = ds.get(0).unwrap() + p;
    ds.push(last);

    let evld = eval_ds(ds);
    let max = evld.iter().max().unwrap();

    sc.write(p - max);
    // sc.write()
    // let hs = reader();

    // solver(hs);
}

#[test]
fn evls() {
    let v = vec![0, 5, 15, 20];

    let ds = eval_ds(v);
    println!("{:?}", ds);
    println!("foo");

    assert_eq!(1, 2);
}

// #[cfg(test)]
// mod tests {
//     use super::*;

//     #[test]
//     fn fst() {
//         assert_eq!(solver(vec![4, 4, 5, 6, 6, 5, 5]), 3);
//     }

//     #[test]
//     fn snd() {
//         assert_eq!(solver(vec![10, 4, 8, 7, 3]), 2);
//     }

//     #[test]
//     fn thrd() {
//         assert_eq!(solver(vec![1, 2, 3, 4]), 0);
//         assert_eq!(solver(vec![1, 2]), 0);
//     }
// }