let () =
	let k, n = Scanf.scanf "%d %d\n" @@ fun a b -> a, b in
	let arr = Array.init n @@ fun _ -> Scanf.scanf "%d " @@ fun d -> d in
	let brr = Array.init n @@ fun i -> 
    	if i = 0 then k - arr.(n-1) + arr.(i) else arr.(i) - arr.(i-1) in
    print_int ((Array.fold_left (+) 0 brr) - (Array.fold_left max 0 brr))