#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <string>
#include <cstring>
#include <set>
#include <map>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <utility>
#include <numeric>
#include <bitset>
#include <cstdio>
#include <istream>
#include <ostream>
#include <fstream>
#include <random>
#include <memory.h>
#include <complex>
#include <ctime>
#include <cassert>

//#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef int64_t ll;
typedef uint64_t ull;
typedef long double ld;

template < typename T >
istream& operator >> (istream &in, vector < T > &arr) {
    for (auto &x: arr)
        in >> x;
    return in;
}

template < typename T >
ostream& operator << (ostream &out, const vector < T > &arr) {
    for (auto &x: arr)
        out << x << ' ';
    return out;
}

template < typename T >
ostream& operator << (ostream &out, const set < T > &arr) {
    for (auto &x: arr)
        out << x << ' ';
    return out;
}

template < typename T >
ostream& operator << (ostream &out, const multiset < T > &arr) {
    for (auto &x: arr)
        out << x << ' ';
    return out;
}

template < typename T, typename D >
istream& operator >> (istream &in, pair < T, D > &p) {
    in >> p.first >> p.second;
    return in;
}

template < typename T, typename D >
ostream& operator << (ostream &out, const pair < T, D > &p) {
    out << p.first << ' ' << p.second;
    return out;
}

template < typename T, typename D >
istream& operator >> (istream &in, vector < pair < T, D > > &arr) {
    for (auto &p: arr)
        in >> p.first >> p.second;
    return in;
}

template < typename T, typename D >
ostream& operator << (ostream &out, const vector < pair < T, D > > &arr) {
    for (auto &p: arr)
        out << p.first << ' ' << p.second << '\n';
    return out;
}

//#define int ll
#define buyolitsez_fanclub ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define forn(i, x, y) for (int i = x; i <= y; ++i)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(_x) _x.begin(), _x.end()
#define rall(_x) _x.rbegin(), _x.rend()
#define sz(_x) (int) _x.size()
#define mp(_x, _y) make_pair(_x, _y)
#define eb(_x, ...) _x.emplace_back(__VA_ARGS__)
#define ins(_x, _y) _x.insert(_y)
#define pi pair < int, int >
#define pll pair < ll, ll >
#define pld pair < ld, ld >
#define vb vector < bool >
#define vvb vector < vb >
#define vi vector < int >
#define vpi vector < pi >
#define vvi vector < vi >
#define vvpi vector < vpi >
#define vc vector < char >
#define vll vector < ll >
#define vvll vector < vll >
#define vpll vector < pll >
#define vvpll vector < vpll >
#define si set < int >
#define spi set < pi >
#define sll set < ll >
#define spll set < pll >
#define tiii tuple < int, int, int >
#define tiiii tuple < int, int, int, int >
#define vsi vector < si >
#define vspi vector < spi >
#define mi multiset < int >
#define mpi multiset < pi >
#define um unordered_map
#define vsll vector < sll >
#define vspll vector < spll >
#define us unordered_set
#define di deque < int >
#define dll deque < ll >
#define dpi deque < pi >
#define dpll deque < pll >
#define vtiii vector < tiii >
#define vtiiii vector < tiiii >

void IO () {
    #if (DEBUG_LEVEL)
        freopen("../input.txt", "r", stdin);
    #else
//        freopen("a.in", "r", stdin);
//        freopen("a.out", "w", stdout);
        buyolitsez_fanclub
    #endif
}

const ll INF = 1e18;
const int int_INF = 1e9 + 13;
const ld PI = acos(-1);
const ld EPS = 1e-9;
const ll MOD = 1e9 + 7;
const ll MAXN = 2e5 + 13;

void solve () {
    int k, n;
    cin >> k >> n;
    vi a(n);
    cin >> a;
    int ans = a.back() - a.front();
    rep(i, n - 1)
        ans = min(ans, k - (a[i + 1] - a[i]));
    cout << ans << '\n';
}

void run () {
    #if (DEBUG_LEVEL)
        clock_t clk = clock();
        solve();
        cerr << clock() - clk << '\n';
    #else
        solve();
    #endif
}

signed main () {
    IO();
    run();

    return 0;
}


Measured memory_bytes: 1024