//package solution;

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
import java.awt.Point;

public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    //long mod = 1000000007L;
    void run() throws Exception {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    String ns() throws Exception {
        return br.readLine();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }

    void p(Object o) {
        System.out.print(o);
    }

    void pl(Object o) {
        System.out.println(o);
    }
} import java.io.*; io.* java import java.util.*; util.* java import java.math.*; math.* java import java.text.*; text.* java import java.awt.Point; Point java.awt public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    //long mod = 1000000007L;
    void run() throws Exception {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    String ns() throws Exception {
        return br.readLine();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }

    void p(Object o) {
        System.out.print(o);
    }

    void pl(Object o) {
        System.out.println(o);
    }
} Main BufferedReader br; br StringTokenizer stk; stk public static void main(String[] args) throws Exception {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    } main {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    } new Main().run() new Main().run new Main() String[] args args //long mod = 1000000007L;
    void run() throws Exception {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    } run {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    } int k = ni() k ni() ni n = ni(); n ni() ni int[] a = new int[n]; a new int[n] n for(int i=0; i<n; i++) a[i] = ni(); int i=0; int i=0; i 0 i<n i n i++ i++ i a[i] = ni(); a[i] = ni() a[i] a i ni() ni int max = -1; max -1 1 for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            max = Math.max(max, a[i] - a[i-1]);
        } {
            max = Math.max(max, a[i] - a[i-1]);
        } max = Math.max(max, a[i] - a[i-1]) max Math.max(max, a[i] - a[i-1]) Math.max Math max a[i] - a[i-1] a[i] a i a[i-1] a i-1 i 1 max = Math.max(max, (k - a[n-1]) + a[0]) max Math.max(max, (k - a[n-1]) + a[0]) Math.max Math max (k - a[n-1]) + a[0] (k - a[n-1]) k a[n-1] a n-1 n 1 a[0] a 0 pl(k - max) pl k - max k max //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    } nextToken {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    } if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        } stk == null || !stk.hasMoreTokens() stk == null stk null !stk.hasMoreTokens() stk.hasMoreTokens() stk.hasMoreTokens stk {
            stk = new StringTokenizer(br.readLine(), " ");
        } stk = new StringTokenizer(br.readLine(), " ") stk new StringTokenizer(br.readLine(), " ") return stk.nextToken(); stk.nextToken() stk.nextToken stk String nt() throws Exception {
        return nextToken();
    } nt {
        return nextToken();
    } return nextToken(); nextToken() nextToken String ns() throws Exception {
        return br.readLine();
    } ns {
        return br.readLine();
    } return br.readLine(); br.readLine() br.readLine br int ni() throws Exception {
        return Integer.parseInt(nextToken());
    } ni {
        return Integer.parseInt(nextToken());
    } return Integer.parseInt(nextToken()); Integer.parseInt(nextToken()) Integer.parseInt Integer nextToken() nextToken long nl() throws Exception {
        return Long.parseLong(nextToken());
    } nl {
        return Long.parseLong(nextToken());
    } return Long.parseLong(nextToken()); Long.parseLong(nextToken()) Long.parseLong Long nextToken() nextToken double nd() throws Exception {
        return Double.parseDouble(nextToken());
    } nd {
        return Double.parseDouble(nextToken());
    } return Double.parseDouble(nextToken()); Double.parseDouble(nextToken()) Double.parseDouble Double nextToken() nextToken void p(Object o) {
        System.out.print(o);
    } p {
        System.out.print(o);
    } System.out.print(o) System.out.print System.out System System.out o Object o o void pl(Object o) {
        System.out.println(o);
    } pl {
        System.out.println(o);
    } System.out.println(o) System.out.println System.out System System.out o Object o o public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    //long mod = 1000000007L;
    void run() throws Exception {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    String ns() throws Exception {
        return br.readLine();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }

    void p(Object o) {
        System.out.print(o);
    }

    void pl(Object o) {
        System.out.println(o);
    }
} public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    //long mod = 1000000007L;
    void run() throws Exception {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    String ns() throws Exception {
        return br.readLine();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }

    void p(Object o) {
        System.out.print(o);
    }

    void pl(Object o) {
        System.out.println(o);
    }
} Main
//package solution;

import java.io.*;
import java.util.*;
import java.math.*;
import java.text.*;
import java.awt.Point;

public final class Main {
    BufferedReader br;
    StringTokenizer stk;

    public static void main(String[] args) throws Exception {
        //new Thread(null, new Runnable() {
            //@Override
            //public void run() {
                //try {
                    new Main().run();
                //} catch(Exception ex) {ex.printStackTrace();}
            //}
        //}, "solution", 1<<26).start();
    }
    
    {
        stk = null;
        br = new BufferedReader(new InputStreamReader(System.in));
    }
    
    //long mod = 1000000007L;
    void run() throws Exception {
        int k = ni(), n = ni();
        int[] a = new int[n];
        for(int i=0; i<n; i++) a[i] = ni();
        
        int max = -1;
        for(int i=1; i<n; i++) {
            max = Math.max(max, a[i] - a[i-1]);
        }
        
        max = Math.max(max, (k - a[n-1]) + a[0]);
        
        pl(k - max);
    }
    
    //Reader & Writer
    String nextToken() throws Exception {
        if (stk == null || !stk.hasMoreTokens()) {
            stk = new StringTokenizer(br.readLine(), " ");
        }
        return stk.nextToken();
    }

    String nt() throws Exception {
        return nextToken();
    }

    String ns() throws Exception {
        return br.readLine();
    }

    int ni() throws Exception {
        return Integer.parseInt(nextToken());
    }

    long nl() throws Exception {
        return Long.parseLong(nextToken());
    }

    double nd() throws Exception {
        return Double.parseDouble(nextToken());
    }

    void p(Object o) {
        System.out.print(o);
    }

    void pl(Object o) {
        System.out.println(o);
    }
}