import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    }
} Main public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    } main {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) StringTokenizer st = new StringTokenizer(s.nextLine()); st new StringTokenizer(s.nextLine()) int K = Integer.parseInt(st.nextToken()); K Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st int N = Integer.parseInt(st.nextToken()); N Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st st = new StringTokenizer(s.nextLine()) st new StringTokenizer(s.nextLine()) int[] houses = new int[N+1]; houses new int[N+1] N+1 N 1 for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            houses[i] = Integer.parseInt(st.nextToken());
        } {
            houses[i] = Integer.parseInt(st.nextToken());
        } houses[i] = Integer.parseInt(st.nextToken()) houses[i] houses i Integer.parseInt(st.nextToken()) Integer.parseInt Integer st.nextToken() st.nextToken st houses[N] = K + houses[0] houses[N] houses N K + houses[0] K houses[0] houses 0 int distance = Integer.MAX_VALUE; distance Integer.MAX_VALUE Integer Integer.MAX_VALUE for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        } {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        } int start = houses[i]; start houses[i] houses i int end = houses[i+1]; end houses[i+1] houses i+1 i 1 int d = K - (Math.abs(start - end)); d K - (Math.abs(start - end)) K (Math.abs(start - end)) Math.abs Math start - end start end distance = Math.min(distance, d) distance Math.min(distance, d) Math.min Math distance d System.out.println(distance) System.out.println System.out System System.out distance String[] args args public class Main {
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    }
} public class Main {
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    }
} Main
import java.util.*;

public class Main {
    public static void main(String[] args)
    {
        Scanner s = new Scanner(System.in);
        StringTokenizer st = new StringTokenizer(s.nextLine());
        int K = Integer.parseInt(st.nextToken());
        int N = Integer.parseInt(st.nextToken());
        st = new StringTokenizer(s.nextLine());
        int[] houses = new int[N+1];
        for(int i=0;i<N;i++)
        {
            houses[i] = Integer.parseInt(st.nextToken());
        }
        houses[N] = K + houses[0];
        int distance = Integer.MAX_VALUE;
        for(int i=0;i<N;i++)
        {
            int start = houses[i];
            int end = houses[i+1];
            int d = K - (Math.abs(start - end));
            distance = Math.min(distance, d);
        }
        System.out.println(distance);
    }
}
