program abc17
 implicit none
 integer::K,N,i,ia,s
 integer,allocatable::a(:),b(:)
 read(*,*)K,N
 allocate(a(N),b(N))
 read(*,*)(a(i),i=1,N)
 do ia=1,N-1
  b(ia)=a(ia+1)-a(ia)
 end do
 b(N)=K-a(N)+a(1)
 s=sum(b)-maxval(b)
 print'(i0)',s
end program abc17