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)

pub mod contest {
    pub mod input {
        use std::io;
        use std::str::FromStr;
        use std::fmt::Debug;

        pub fn read_line<F>() -> F
        where F: FromStr, <F as FromStr>::Err: Debug {
            let mut input = String::new();
            io::stdin().read_line(&mut input).unwrap();
            input.trim().parse().unwrap()
        }

        pub fn read_words<F>() -> Vec<F>
        where F: FromStr, <F as FromStr>::Err: Debug {
            let mut input = String::new();
            io::stdin().read_line(&mut input).unwrap();
            input
                .split_whitespace()
                .map(|i| i.parse().unwrap())
                .collect()
        }

        pub fn read_matrix<F>(num_lines: usize) -> Vec<Vec<F>>
        where F: FromStr, <F as FromStr>::Err: Debug {
            (0..num_lines).map(|_| read_words()).collect()
        }

        // 所有権的な問題で SplitWhitespace を直接持てないので妥協
        pub struct WordsReader {
            words: Vec<String>,
            cur: usize,
        }

        impl WordsReader {
            pub fn new() -> Self {
                let mut input = String::new();
                io::stdin().read_line(&mut input).unwrap();
                WordsReader {
                    words: input.split_whitespace().map(|i| i.to_owned()).collect(),
                    cur: 0,
                }
            }

            pub fn next<F>(&mut self) -> F
            where F: FromStr, <F as FromStr>::Err: Debug {
                let v: F = self.words[self.cur].parse().unwrap();
                self.cur += 1;
                v
            }
        }
    }
}

use std::cmp;

fn max_span(k: i32, a: &Vec<i32>) -> i32 {
    let mut min_a = k + 9999; // a[i] としてはありえない値をてきとうに
    let mut max_a = -9999;
    let mut max_span = -9999;
    for i in 0..(a.len() - 1) {
        min_a = cmp::min(min_a, a[i]);
        max_a = cmp::max(max_a, a[i]);
        max_span = cmp::max(max_span, a[i + 1] - a[i]);
    }
    let last_span = (k - a.last().unwrap()) + a[0];
    cmp::max(max_span, last_span)
}

fn solve(k: i32, a: &mut Vec<i32>) -> i32 {
    a.sort();
    k - max_span(k, a)
}

#[test]
fn test_solve() {
    assert_eq!(solve(20, &mut vec![5, 10, 15]), 10);
    assert_eq!(solve(20, &mut vec![0, 5, 15]), 10);
}

fn main() {
    let mut wr = contest::input::WordsReader::new();
    let k: i32 = wr.next();
    let _: usize = wr.next();
    let mut a: Vec<i32> = contest::input::read_words();
    println!("{}", solve(k, &mut a));
}