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 Ruby (2.3.3) (judge verdict: Runtime Error)

ALP = ('a'..'z').to_a
INF = 0xffffffffffffffff
# mod = 998244353
# mod = 10 ** 9 + 9
mod = 10 ** 9 + 7
PI = Math::PI
dx = [ 1,  0, -1,  0,  1,  1, -1, -1]
dy = [ 0,  1,  0, -1,  1, -1,  1, -1]
eight = [0, 1, 1, 0, -1, -1, 1, -1, 0]
def max(a,b);       a > b ? a : b                              end
def min(a,b);       a < b ?  a : b                             end
def swap(a,b);      a, b = b, a                                end
def gi;             gets.to_i                                  end
def gf;             gets.to_f                                  end
def gs;             gets.chomp                                 end
def gai(n=nil); n.nil? ? gets.split.map(&:to_i) : n.times.map{ gets.to_i } end
def gaf(n=nil); n.nil? ? gets.split.map(&:to_f) : n.times.map{ gets.to_f } end
def gas(n=nil); n.nil? ? gets.split : n.times.map{ gets.chomp } end
def ga2i(h);        h.times.map{ gets.split.map(&:to_i) }      end
def yn(f); puts f ? "Yes" : "No" end
def YN(f); puts f ? "YES" : "NO" end
def yes(f=nil); f.nil? ? (puts "Yes"; exit) : (puts f ? "Yes" : "No") end
def no(f=nil); f.nil? ? (puts "No"; exit) : (puts f ? "Yes" : "No") end
def yay(f=nil); f.nil? ? (puts "Yay!"; exit) : (puts f ? "Yay!" : ":(") end

class Array
  def lcm; inject(:lcm) end
  def gcd; inject(:gcd) end
  def prod; inject(:*) end
  def abs; map{|k| k.abs } end
  def mod(m); map{|e|e%m} end
  def cumsum; s = 0; map{|k| s += k } end
  # frequency dist.
  def fd(n=nil); n = self.max if n.nil?; res = Array.new(n+1,0);self.map{|e| res[e]+=1 };res;end
  # cumlative frequency dist.
  def cfd(n=nil); fd.cumsum; end
  def diff; s = self[0]; self[1...(self.size)].map{|k|d=k-s; s=k; d} end
  # def sum; inject(:+) end
  # def append(k); push(k) end
  # def prepend(k); unshift(k) end
end

def an(n,d=0) Array.new(n,d)end
def an2(n,m,d=0) Array.new(n){an(m,d)}end
def an3(n,m,l,d=0) Array.new(n){an2(m,l,d)}end
def zeros(n); an2(n,n) end
def eye(n); an2(n,n).each_with_index{|e,i|e[i]=1} end # 単位行列 identity matrix

k,n = gai
a = gai

d = a.diff.append(k-a[-1] + a[0]).max

ans = k - d

puts ans