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 C (GCC 5.4.1) (judge verdict: Accepted)

/*
とりあえずソースコード見る前に推し見ていってください。
                                                                                   ` ....J&wZTUUWc
                                                                             `...JZY"!~-._<~~~~_d'
                                                                       ` ..-Z"=~`..._~___::~~~_J\
       ........                                                    ` ..dY=````..~~__::::::::~_J\
  .KY"=<<<<<<<?77"YTXa-..                                      `  .JY=`````.-<~--::<<~~~~<:<_d^
   W,~~~_______<~-..`.. ?7TG+...`         ` ` `  `   ....   `  .v"~`````.-<~-JT1__........_-d'
    T,_~:::;:<z+-~~~~<-..``.`._?TSJ.. ..JVYT7TTTTTY"=<<<?7"TQJ=~`.``...?~~__(<~_......... .#`
     (h_~:<::((+v___._.~~?<.`````.`_?TG-__...___.__....` ..?~```.` .J!~~_..-~<7_......._ .P
      .W,_~~~_?71-.````` _~~?i..````````.``.```..```````._```.. .J>_~_`   ?<<__.......  (%
        ?p _..._._-z.,  ``` _~_?<- .`.````````````.``.````` _<1>_~~~~._ --(i-......._ .V!
         .W, ......(>~<~+-....~~~(t<!`.`.`.`.``.```.``.``` `   .<-~~((?1~?=_......_ .J=
           7e .........!_.C~-(>(?`  ` ````````````````````  `     ?(~_`` ``....  `.-Y
            .W, .......```` ~(<`   ` `   ` ``````````      `  `     ?-_.    ``` .J"
             `?h.     ` ` .~J! ` `     `  `   ` ` `   `  `   `  `  `  1__    ..J=
                7a,     .~_J`     ` ` `  ` ``   `  `  `   `    `  `  ` 1-_ (d=`
                  ?TGgJ<<_J`` ` `  ` - `  ```` ``` `  `(   `  `     ` .wWc<W_
                      .5g-$`   `     >  `````  ``` ` ` (-  `    `  `   _7HHXb.
                       .@H=`     ` `.l` ````  `  ```` `(<-`  _ `  `   `  j ?NHm,`
                    `.dHMF ` ` ` . `.$```` `    `` ````(~(.(<J- `  ` `   (  W7HHm,
                   `.@Ht,P` `   ._  (1_. `  ` `   `` .(T!_<-`?W+  `  ````(  d, (UHh.`
                `  .M#!  b``  ` ,Sv7TOo.  `  `   ` _~. <`` <. ({`  `` ` .<   h   .UH&`
                 `.MD`   O-`````vl  v.1__ ``` ``````.;.:``` 1 J1 ` ````.i! ` J;    ?Mh.
                 .WF     .n...``.?- > ->.-`````````..I+.`` ..e:(- . ```.V-  `.b      UN. `
                `(F      (C<~(<..l(1c` ?z1---....._jjA.JJadHYW@.> < ``.c(} `  d.      ?N.
               `.@`     .@  <((-_vM#HmQmq8?1&-~~~(J!`  WHMHMb `.>.> ._J~(!` ``(]       ,b   `
                ,>      d!`  ` v+(_`.HHMMb. `????````  XxdHMH~` >(iJO!_(> ```_ K        ,`
                .      .F` ` ``.c1s..HdMMH~````````````(BXVY=``.O>`.c_(>_````(-W
                      `J{;``` ``(+> `-77"= ```.````````````   ````..+>~~_````.}d
                       H(:```````.3 ..````````.````````````` `.` .>~~~({__ ``.Pd
                     ` Dd_```.....I ``````````_````````````````.v~~:~:(P_~_ ((bK
                       WM{ ...~~~~(o.``````````````` ..````` .?`(<:~(-+#_~~_1dH%
                       dvr-l..~~_s_w7+.``.<(-. ?!``~~..-<1.?!` J<+~:(iWH~::(kWF
                         X_w~~~(jM:Wc_O?42``` ?&+-;+<~``.(:` .4~(D~(Id!H~_(V!!
                         .hdm_~(d?hdNz-_(>``.._(_.(>_..``(:`` (1H%(uV! HV"!
                           7:?4uD.K~.._?z>`.._(JSjk-__`.`(C<<!_7H#"!
                                .dI:__-.I?&Jv=_+?>?l_?4v?> -< ...?h.
                    `        .JHz?1<:::J<(l ` J?1J<1_ `(:..?_...._Jb
                        `..v"!``(>(1+:(>~~(1.<(1<iJ><..C<_--1(:::+CJN.
                        J=````.` (a+g0Ws:::_:_::(<___<(~___(C~:<z<1>-Ta,`
                       `?e````.z@!     X<:::::::<+:::::?1+jdYTYAaZ!``` ?4a.`    `
                         (n.````Oe    .D_~~~:::::::::::::(W!     ?U, ````.To.      `
                          .4,````?Hka.f_....__~~::~:~:~~~_d;       (W,`` `` 7a.
                            ?N,  `-_4D..............~.~..._W,        ?NJ. ` ` ?4dYBh, `
                             JD?-   +_......................W.         _TS&,   `  ` TmJN`
                             .UR-...<z+-_-............-__-((dL             _Wn. `  .! (t   `
                            ` J8!_.2____<?<<<+(-(--(+<<1<<(_<jL             .N..`.J!  ?W&
                              d&J?` ~?<+-_(-~(-:(_: <.<(-(<7!_?o             ?B9Tn.` (4kdb
                        ` `.dY!   ` `./` ` _(????!!~!` ``(_``..?L                .4,m,iW.
                            .U,    `.!   `  !  `` `. `` ` < `...?r                 Wd?BY`
                              (N&.`.!` `   <  ```` _ ` ```(_`..(JV`   `  `
                             `(8-.?=<?1+-..+... ```(.......f<~?r          ` ` ......
                             .K`.?<-.   __(K<D__<?TC~~~~__```.-S.  ....-&ZY""777?77""9GJ,`   `
                            `J}````` _??<d= J{~~~~~(i.....-<<~`(Y77<~_``.```.```````.`.(XHa.    `
                        `   .K`````````.d^  X~~.~.~~_1. ````.`` I_....```.````.``.`````_dWqHm,
                           `(%``.`.```.Y    R~~~.~.~~~<-`````.``(-~~.....`.`````.``.``.(dWqkHh
                           .K_```.``.J^     S_~~~~~.~~~?:``.``.`.l~~~~...``.`.``````.``_zWkqkHb`
                        ` .d'`..```.Y`      q{~.~.~~.~.~(i ````.`(/.~~.....```.``.```.`.dWqqqqH[
                         .d!`~~_-.J^        .b_~.~~.~~~~~_1.  ..``?+~~~~....````````.` (WkHHM#H#
                         d!` __?(f`          ?n~~~.~~.~~~~~?i_.````?/~~.~..`.``.``.``._jkY"7=
                     `  .D``` `.%             ?h,~~~~.~..~~~~1, ``` j_~~....``.``.``.-V=`
                       `(}`   (\                Tm,~~~~~.~.~~~?- `` .>~~....``.``..JY`
               `   `   .g`   ('                   ?W&-~~~~~.~.~(>  ` 1_~~....`..JT^
                      d=<  .,Nx                      THaJ-_~~~~~(l   (o.(((JZ"^`
                `    .@   `?`.H.                        _"YBHmggk<!`` > W>
                      4eJ>:1.(t                                 W+.<(. .(D           `
                      ({?<!~db                                   W$<<jY4F`
                      ,h. _`.W`                                 (5..J=.d`
                    `   7TUY"`                                  4,...(Y!
                                                                  ~~`                         `

	でかいですね。
	更にかわいいときた。
	うん。好き。
*/

/*  include  */
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <locale.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <setjmp.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>

/*  typedef  */
typedef long long ll;
typedef char string[100];
typedef int array[100];
/*  define  */

/*  define funcs  */
#define sort_d(array,end){int size=end-array;qsort(array,size,sizeof(int),intcmp);}
#define sort_c(array,end){int size=end-array;qsort(array,size,sizeof(char),charcmp);}
#define rand_range(min,max) (min)+(rand())%((max)-(min)+(1))
#define rep(i,j) for(int i=0;i<j;i++)
#define loop(i,j,z) for(int i=j;i<z;i++)
#define swap(a,b) ((a!=b)&&(a+=b,b=a-b,a-= b))
#define getarray_d(array,size){array=(int *)malloc(sizeof(int)*size);}
#define getarray_c(array,size){array=(char *)malloc(sizeof(char)*size);}
#define fill(array,end,val){int size=end-array;rep(i,size)array[i]=val;}
#define min(a,b) (a>b)?b:a
/*  otter  (Otter)  ｶﾜｳｿｯ！ */
#define intptr int *
#define random srand((unsigned)time(NULL))
#define ON 1
#define OFF 0
#define TREE_SIZE 1000

/*  prototype Declaration */
int intcmp(const void* a, const void* b);
int charcmp(const void* a, const void* b);
bool is_sorted(const int* start, const int* end);
int count(const int* start, const int* end, int target);
void shuffle(int* start, int* end);
void tree_init(void);
int tree_root(int x);
void unite(int x, int y);
bool same(int x, int y);

/*  scan  */
#define sd(a) scanf("%d",&a)
#define sc(a) scanf("%c",&a), getchar()
#define sf(a) scanf("%f",&a)
#define ss(a) scanf("%s",a)
#define sld(a) scanf("%lld",&a)
/*  print  */
#define odn(a) printf("%d\n",a)
#define oldn(a) printf("%lld\n",a)
#define ocn(a) printf("%c\n",a)
#define ofn(a) printf("%f\n",a)
#define osn(a) printf("%s\n",a)
#define od(a) printf("%d ",a)
#define old(a) printf("%lld\n",a)
#define oc(a) printf("%c ",a)
#define of(a) printf("%f ",a)
#define os(a) printf("%s ",a)

/*  struct  */
typedef struct {
	int first;
	int second;
}pair;
/*  local val  */
int tree[TREE_SIZE];
int cnts=0;
/*  funcs （local）*/

/*  main program  */
int main(void){
	int K,N;
	int A[200005];
	sd(K),sd(N);
	rep(i,N) sd(A[i]);
	int m_len=INT_MAX;
	rep(i,N){
		if(i!=N-1)
			m_len=min(m_len,K-(A[i+1]-A[i]));
		else
			m_len=min(m_len,A[N-1]-A[0]);
	}
	od(m_len);
}

/*  funcs  (global)*/
void sort_first(pair* b, pair* e)
{
	int* dec;
	int s2 = e - b;
	int size = e - b;
	getarray_d(dec, size);
	rep(i, size) dec[i] = b[i].first;
	sort_d(dec, dec + s2);
	rep(i, size) b[i].first = dec[i];
}
void sort_second(pair* b, pair* e)
{
	int* dec;
	int s2 = e - b;
	int size = e - b;
	getarray_d(dec, size);
	rep(i, size) dec[i] = b[i].second;
	sort_d(dec, dec + s2);
	rep(i, size) b[i].second = dec[i];
}


int intcmp(const void* a, const void* b)
{
	return *(int*)a - *(int*)b;
}

int charcmp(const void* a, const void* b)
{
	return *(char*)a - *(char*)b;
}

bool is_sorted(const int* start, const int* end)
{
	while (start != end) {
		if (*start > * (start + 1)) return false;
		start++;
	}
	return true;
}

int count(const int* start, const int* end, int target)
{
	int cnt = 0;
	int* array;
	int* p;

	array = (int*)malloc((end - start) * sizeof(int));

	if (array == NULL) {
		printf("Err0r");
		exit(-1);
	}

	memcpy(array, start, sizeof(int) * (end - start));
	sort_d(array, array + (end - start));
	p = (int*)bsearch(&target, array, end - start, sizeof(int), intcmp);

	if (p == NULL) {
		free(array);
		return 0;
	}
	while (*p == target)
		p++;
	p--;
	while (*p == target) {
		cnt++;
		p--;
	}

	free(array);

	return cnt;
}

void shuffle(int* start, int* end)
{
	random;

	int a, b;
	int size = end - start;

	rep(i, 500) {
		a = rand_range(0, size - 1);
		b = rand_range(0, size - 1);
		swap(start[a], start[b]);
	}
}
/*  tree  */

void tree_init(void)
{
	for (int i = 0; i < TREE_SIZE; i++) tree[i] = i;
}

int tree_root(int x)
{
	if (tree[x] == x) return x;
	return tree[x] = tree_root(tree[x]);
}

void unite(int x, int y)
{
	int rx = tree_root(x);
	int ry = tree_root(y);
	if (rx == ry) return;
	tree[rx] = ry;
}

bool same(int x, int y)
{
	int rx = tree_root(x);
	int ry = tree_root(y);
	return rx == ry;
}

ll pow_mod(ll n, ll k, ll m)
{
	if (k == 0) return 1;
	else if (k % 2) return pow_mod(n, k - 1, m) * n % m;
	else {
		int t = pow_mod(n, k / 2, m);
		return t * t % m;
	}
}