import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;

class Main {
    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            list.add(scanner.nextInt());
        }
        scanner.close();
        Collections.sort(list);
        List<Integer> resultList = new ArrayList<>();
        for(int i=0; i<n; i++){
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        }
        Collections.sort(resultList);
        int result = k - resultList.get(n-1);
        System.out.println(result);
    }
} import java.util.Scanner; Scanner java.util import java.util.List; List java.util import java.util.ArrayList; ArrayList java.util import java.util.Collections; Collections java.util import java.lang.Math; Math java.lang class Main {
    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            list.add(scanner.nextInt());
        }
        scanner.close();
        Collections.sort(list);
        List<Integer> resultList = new ArrayList<>();
        for(int i=0; i<n; i++){
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        }
        Collections.sort(resultList);
        int result = k - resultList.get(n-1);
        System.out.println(result);
    }
} Main public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            list.add(scanner.nextInt());
        }
        scanner.close();
        Collections.sort(list);
        List<Integer> resultList = new ArrayList<>();
        for(int i=0; i<n; i++){
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        }
        Collections.sort(resultList);
        int result = k - resultList.get(n-1);
        System.out.println(result);
    } main {
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            list.add(scanner.nextInt());
        }
        scanner.close();
        Collections.sort(list);
        List<Integer> resultList = new ArrayList<>();
        for(int i=0; i<n; i++){
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        }
        Collections.sort(resultList);
        int result = k - resultList.get(n-1);
        System.out.println(result);
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner int n = scanner.nextInt(); n scanner.nextInt() scanner.nextInt scanner List<Integer> list = new ArrayList<>(); list new ArrayList<>() for(int i=0; i<n; i++){
            list.add(scanner.nextInt());
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            list.add(scanner.nextInt());
        } {
            list.add(scanner.nextInt());
        } list.add(scanner.nextInt()) list.add list scanner.nextInt() scanner.nextInt scanner scanner.close() scanner.close scanner Collections.sort(list) Collections.sort Collections list List<Integer> resultList = new ArrayList<>(); resultList new ArrayList<>() for(int i=0; i<n; i++){
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        } int i=0; int i=0; i 0 i<n i n i++ i++ i {
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        } {
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        } if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            } i == n-1 i n-1 n 1 {
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            } resultList.add(Math.abs(list.get(0)+k-list.get(n-1))) resultList.add resultList Math.abs(list.get(0)+k-list.get(n-1)) Math.abs Math list.get(0)+k-list.get(n-1) list.get(0)+k list.get(0) list.get list 0 k list.get(n-1) list.get list n-1 n 1 {
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            } resultList.add(Math.abs(list.get(i+1)-list.get(i))) resultList.add resultList Math.abs(list.get(i+1)-list.get(i)) Math.abs Math list.get(i+1)-list.get(i) list.get(i+1) list.get list i+1 i 1 list.get(i) list.get list i Collections.sort(resultList) Collections.sort Collections resultList int result = k - resultList.get(n-1); result k - resultList.get(n-1) k resultList.get(n-1) resultList.get resultList n-1 n 1 System.out.println(result) System.out.println System.out System System.out result String[] args args
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;

class Main {
    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        int k = scanner.nextInt();
        int n = scanner.nextInt();
        List<Integer> list = new ArrayList<>();
        for(int i=0; i<n; i++){
            list.add(scanner.nextInt());
        }
        scanner.close();
        Collections.sort(list);
        List<Integer> resultList = new ArrayList<>();
        for(int i=0; i<n; i++){
            if(i == n-1){
                resultList.add(Math.abs(list.get(0)+k-list.get(n-1)));
            }else{
                resultList.add(Math.abs(list.get(i+1)-list.get(i)));
            }
        }
        Collections.sort(resultList);
        int result = k - resultList.get(n-1);
        System.out.println(result);
    }
}