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 Python (3.4.3) (judge verdict: Wrong Answer)

'''

2 2 4 4 4
11 12 13 14
21 22 23 24
1 2 3 4

'''

'''o=input().rstrip().split(' ')
x=int(o[0])
y=int(o[1])
a=int(o[2])
b=int(o[3])
c=int(o[4])
H=0;
R=input().rstrip().split(' ')
G=input().rstrip().split(' ')
ANY=input().rstrip().split(' ')
R.sort(key=int,reverse=True)
G.sort(key=int,reverse=True)
ANY.sort(key=int,reverse=True)
while(x>0):
    if int(R[0]) > int(ANY[0]):
        H+=int(R[0])
        del(R[0])
        x-=1;
    else:
        break;
while(y>0):
    if int(G[0]) > int(ANY[0]):
        H+=int(G[0])
        del(G[0])
        y-=1;
    else:
        break;
if(x>0 && y==0):
    for i in range(0,x):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(R[0])
            del(R[0])
elif(y>0 && x==0):
    for i in range(0,y):
        if i<len(ANY):
            H+=ANY[i];
        else:
            H+=int(G[0])
            del(G[0])
else:
    FF=x+y;
    if FF<=len(ANY):
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
    else:
        for i in range(0,len(ANY)):
            H+=int(ANY[i])
        XX=0;
        YY=0;
        for i in range(0,x):
            '''
            
            
'''

20 3
0 5 15

'''


o=input().rstrip().split(' ')
p=input().rstrip().split(' ')
l=[]
for i in range(1,len(p)):
    l.append(int(p[i]) - int(p[i-1]))
l.append(int(o[0])-int(p[len(p)-1]))
q=[]
C=list(p)
A=list(C[0])
B=C[1:len(C)];
B.reverse();
C=A+B;
#print(C)
for i in range(1,len(C)):
    q.append(int(o[0]) - int(C[i]))
q.append(int(o[0]) - int(C[0]))
#print(l,q)
S=0
D=0
for i in range(0,len(l)):
    S+=int(l[i]);
    D+=int(q[i])
W=[]
minu = 10000000000;
for i in range(0,len(l)):
    if i==0:
        F=S-int(l[len(l)-1])
        minu = min(minu,F)
    else:
        F=S-int(l[i-1])
        minu = min(minu,F);
for i in range(0,len(q)):
    if i==0:
        F=D-int(q[len(l)-1])
        minu = min(minu,F)
    else:
        F=D-int(q[i-1])
        minu = min(minu,F);
print(minu)