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 Fortran (gfortran v4.8.4) (judge verdict: Accepted)

      MODULE qSortMod
      implicit none
      interface qSort
        module procedure :: i16qSort
      end interface
      contains
      
      pure recursive function i16qSort( a ) result( r )
          !ref
          !https://t.doujin-constant.net/
          !post/143374686394/fortranをより使いこなしたquick-sort
          !
          !left is smaller!
        implicit none
        integer(16),intent(in) :: a(:)
        integer(16) :: r( size(a) )
        !integer(16),allocatable :: r(:) !is OK
        integer(16) :: sample(3),len,i,j
        
        len = size(a)
        if( len>1 )then
          sample(1) = a(1)
          sample(2) = a(len/2)
          sample(3) = a(len)
          !select median
          do i = 1,2
            do j = 2,3
              if( sample(i)>sample(j) )then
                len = sample(i) !len is buffer
                sample(i) = sample(j)
                sample(j) = len
              end if
            end do
          end do
          
          r = [ qSort( pack( a(:),a(:)< sample(2) ) ), &
                       pack( a(:),a(:)==sample(2) )  , &
                qSort( pack( a(:),a(:)> sample(2) ) ) ]
        else
          r = a
        end if
      end function i16qSort
      
      END MODULE qSortMod
      
      
      PROGRAM piyo
      use qSortMod
      IMPLICIT NONE
      integer(16) :: n,k
      integer(16),allocatable :: a(:)
      integer(16) :: ans,ant,post,i
      
      read*,k,n
      allocate( a(n) )
      read*,a
      
      a = qSort( a )
      
      if( n==2 )then
        print*,abs( a(2)-a(1) );stop
      end if
      
      
      ans = k
      do i = 1,n-1
        ans = min( ans,k-(a(i+1)-a(i)) )
      end do
      !i=n
      ans = min( ans,a(n)-a(1) )
      
      print*,ans
      
      !debugg
    !  print*,k,n
    !  print*,a
      
      
      END PROGRAM