
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;


public class Main {
    
         
    static FastReader in;
    static StringBuilder out;

    public  static void main(String[] args) {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
 
     
    
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.lang.reflect.Array; Array java.lang.reflect import java.util.*; util.* java public class Main {
    
         
    static FastReader in;
    static StringBuilder out;

    public  static void main(String[] args) {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
 
     
    
} Main static FastReader in; in static StringBuilder out; out public  static void main(String[] args) {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    } main {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    } in = new FastReader() in new FastReader() out=new StringBuilder() out new StringBuilder() int k=in.nextInt() k in.nextInt() in.nextInt in n=in.nextInt(); n in.nextInt() in.nextInt in int dis[]=new int[n]; dis new int[n] n for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            dis[i]=in.nextInt();
        } {
            dis[i]=in.nextInt();
        } dis[i]=in.nextInt() dis[i] dis i in.nextInt() in.nextInt in Arrays.sort(dis) Arrays.sort Arrays dis int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0])); ans Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0])) Math.min Math Math.abs(dis[dis.length-1]-dis[0]) Math.abs Math dis[dis.length-1]-dis[0] dis[dis.length-1] dis dis.length-1 dis.length dis dis.length 1 dis[0] dis 0 k-Math.abs(dis[1]-dis[0]) k Math.abs(dis[1]-dis[0]) Math.abs Math dis[1]-dis[0] dis[1] dis 1 dis[0] dis 0 for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        } int i=1; int i=1; i 1 i<n i n i++ i++ i {
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        } {
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        } int clk=0 clk 0 aclk=0; aclk 0 clk=k-Math.abs(dis[i]-dis[i-1]) clk k-Math.abs(dis[i]-dis[i-1]) k Math.abs(dis[i]-dis[i-1]) Math.abs Math dis[i]-dis[i-1] dis[i] dis i dis[i-1] dis i-1 i 1 if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            } i==n-1 i n-1 n 1 {
                aclk=Math.abs(dis[n-1]-dis[0]);
            } aclk=Math.abs(dis[n-1]-dis[0]) aclk Math.abs(dis[n-1]-dis[0]) Math.abs Math dis[n-1]-dis[0] dis[n-1] dis n-1 n 1 dis[0] dis 0 {
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            } aclk=k-Math.abs(dis[i+1]-dis[i]) aclk k-Math.abs(dis[i+1]-dis[i]) k Math.abs(dis[i+1]-dis[i]) Math.abs Math dis[i+1]-dis[i] dis[i+1] dis i+1 i 1 dis[i] dis i ans=Math.min(Math.min(clk, aclk),ans) ans Math.min(Math.min(clk, aclk),ans) Math.min Math Math.min(clk, aclk) Math.min Math clk aclk ans System.out.println(ans) System.out.println System.out System System.out ans String[] args args static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    } FastReader BufferedReader br; br StringTokenizer st; st public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        } FastReader {
            br = new BufferedReader(new InputStreamReader(System.in));
        } br = new BufferedReader(new InputStreamReader(System.in)) br new BufferedReader(new InputStreamReader(System.in)) String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } next {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        } while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } st == null || !st.hasMoreElements() st == null st null !st.hasMoreElements() st.hasMoreElements() st.hasMoreElements st {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } IOException e {
                    e.printStackTrace();
                } e.printStackTrace() e.printStackTrace e {
                    st = new StringTokenizer(br.readLine());
                } st = new StringTokenizer(br.readLine()) st new StringTokenizer(br.readLine()) return st.nextToken(); st.nextToken() st.nextToken st int nextInt() {
            return Integer.parseInt(next());
        } nextInt {
            return Integer.parseInt(next());
        } return Integer.parseInt(next()); Integer.parseInt(next()) Integer.parseInt Integer next() next long nextLong() {
            return Long.parseLong(next());
        } nextLong {
            return Long.parseLong(next());
        } return Long.parseLong(next()); Long.parseLong(next()) Long.parseLong Long next() next double nextDouble() {
            return Double.parseDouble(next());
        } nextDouble {
            return Double.parseDouble(next());
        } return Double.parseDouble(next()); Double.parseDouble(next()) Double.parseDouble Double next() next String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        } nextLine {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        } String str = ""; str "" try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } IOException e {
                e.printStackTrace();
            } e.printStackTrace() e.printStackTrace e {
                str = br.readLine();
            } str = br.readLine() str br.readLine() br.readLine br return str; str public class Main {
    
         
    static FastReader in;
    static StringBuilder out;

    public  static void main(String[] args) {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
 
     
    
} public class Main {
    
         
    static FastReader in;
    static StringBuilder out;

    public  static void main(String[] args) {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
 
     
    
} Main

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.*;


public class Main {
    
         
    static FastReader in;
    static StringBuilder out;

    public  static void main(String[] args) {
        
        in = new FastReader();
        out=new StringBuilder();
        int k=in.nextInt(),n=in.nextInt();
        int dis[]=new int[n];
        for(int i=0;i<n;i++){
            dis[i]=in.nextInt();
        }
        Arrays.sort(dis);
        int ans=Math.min(Math.abs(dis[dis.length-1]-dis[0]),k-Math.abs(dis[1]-dis[0]));
        for(int i=1;i<n;i++){
            int clk=0,aclk=0;
            clk=k-Math.abs(dis[i]-dis[i-1]);
            if(i==n-1){
                aclk=Math.abs(dis[n-1]-dis[0]);
            }else{
                aclk=k-Math.abs(dis[i+1]-dis[i]);
            }
            ans=Math.min(Math.min(clk, aclk),ans);
        }
        System.out.println(ans);
    }
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
 
        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
 
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
 
        int nextInt() {
            return Integer.parseInt(next());
        }
 
        long nextLong() {
            return Long.parseLong(next());
        }
 
        double nextDouble() {
            return Double.parseDouble(next());
        }
 
        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
 
     
    
}