#include <iostream>
using namespace std; typedef long long ll;

int main() {
  ll k,n; cin>>k>>n;
  ll a[n]; for(int i=0;i<n;i++) {cin>>a[i];}
  ll an=a[n-1]-a[0];
  for(int i=0;i<n-1;i++) {
    an = min(an, k + a[i] - a[i+1]);
  }
  cout<<an<<endl;
}