import java.util.*; 
import java.io.*;

public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main static StringTokenizer st; st static BufferedReader br; br static PrintWriter out; out public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    } main {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) int K = nextInt(); K nextInt() nextInt int N = nextInt(); N nextInt() nextInt int[] input = new int[N]; input new int[N] N for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        } int x=0; int x=0; x 0 x<N x N x++ x++ x { 
            input[x]  = nextInt();
        } { 
            input[x]  = nextInt();
        } input[x]  = nextInt() input[x] input x nextInt() nextInt Arrays.sort(input) Arrays.sort Arrays input int max = Integer.MIN_VALUE; max Integer.MIN_VALUE Integer Integer.MIN_VALUE for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        } int x=1; int x=1; x 1 x<N x N x++ x++ x { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        } { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        } int tmp = input[x] - input[x-1]; tmp input[x] - input[x-1] input[x] input x input[x-1] input x-1 x 1 max = Math.max(tmp, max) max Math.max(tmp, max) Math.max Math tmp max max = Math.max(K-input[N-1]+input[0], max) max Math.max(K-input[N-1]+input[0], max) Math.max Math K-input[N-1]+input[0] K-input[N-1] K input[N-1] input N-1 N 1 input[0] input 0 max System.out.println(K-max) System.out.println System.out System System.out K-max K max String[] args args public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    } nextInt {
        return Integer.parseInt(next());
    } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next public static long nextLong() throws IOException {
        return Long.parseLong(next());
    } nextLong {
        return Long.parseLong(next());
    } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    } nextDouble {
        return Double.parseDouble(next());
    } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } next {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    } while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim()); st == null || !st.hasMoreTokens() st == null st null !st.hasMoreTokens() st.hasMoreTokens() st.hasMoreTokens st st = new StringTokenizer(br.readLine().trim()) st new StringTokenizer(br.readLine().trim()) return st.nextToken(); st.nextToken() st.nextToken st public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
} Main
import java.util.*; 
import java.io.*;

public class Main{
    static StringTokenizer st;
    static BufferedReader br;  
    static PrintWriter out;
 
    public static void main(String[] args) throws IOException {
        br = new BufferedReader(new InputStreamReader(System.in));
 
        int K = nextInt();
        int N = nextInt();

        int[] input = new int[N];
        for (int x=0; x<N; x++) { 
            input[x]  = nextInt();
        }

        Arrays.sort(input);

        int max = Integer.MIN_VALUE;
        
        for (int x=1; x<N; x++) { 
            int tmp = input[x] - input[x-1]; 
            max = Math.max(tmp, max);
        }

        max = Math.max(K-input[N-1]+input[0], max);

        System.out.println(K-max);
    }
    
      
    public static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }
    public static long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    static String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine().trim());
        return st.nextToken();
    }
}