import java.io.*;
import java.util.*;
public class Main
{
	public static void main(String[] args)throws IOException {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	}
} import java.io.*; io.* java import java.util.*; util.* java public class Main
{
	public static void main(String[] args)throws IOException {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	}
} Main public static void main(String[] args)throws IOException {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	} main {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	} InputStreamReader read=new InputStreamReader(System.in); read new InputStreamReader(System.in) BufferedReader in=new BufferedReader(read); in new BufferedReader(read) String str=in.readLine(); str in.readLine() in.readLine in String line[]=str.trim().split(" "); line str.trim().split(" ") str.trim().split str.trim() str.trim str " " int k=Integer.parseInt(line[0]); k Integer.parseInt(line[0]) Integer.parseInt Integer line[0] line 0 int n=Integer.parseInt(line[1]); n Integer.parseInt(line[1]) Integer.parseInt Integer line[1] line 1 String st=in.readLine(); st in.readLine() in.readLine in int a[]=new int[n]; a new int[n] n int diff[]=new int[n]; diff new int[n] n String l[]=st.trim().split(" "); l st.trim().split(" ") st.trim().split st.trim() st.trim st " " int i; i for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		} i=0; i=0 i 0 i<n i n i++ i++ i {
		    a[i]=Integer.parseInt(l[i]);
		} {
		    a[i]=Integer.parseInt(l[i]);
		} a[i]=Integer.parseInt(l[i]) a[i] a i Integer.parseInt(l[i]) Integer.parseInt Integer l[i] l i diff[0]=a[0] diff[0] diff 0 a[0] a 0 for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		} i=1; i=1 i 1 i<n i n i++ i++ i {
		    diff[i]=a[i]-a[i-1];
		} {
		    diff[i]=a[i]-a[i-1];
		} diff[i]=a[i]-a[i-1] diff[i] diff i a[i]-a[i-1] a[i] a i a[i-1] a i-1 i 1 Arrays.sort(diff) Arrays.sort Arrays diff int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]); ans Math.max((k-a[n-1]+a[0]),diff[n-1]) Math.max Math (k-a[n-1]+a[0]) k-a[n-1] k a[n-1] a n-1 n 1 a[0] a 0 diff[n-1] diff n-1 n 1 System.out.println((k-ans)) System.out.println System.out System System.out (k-ans) k ans String[] args args public class Main
{
	public static void main(String[] args)throws IOException {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	}
} public class Main
{
	public static void main(String[] args)throws IOException {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	}
} Main
import java.io.*;
import java.util.*;
public class Main
{
	public static void main(String[] args)throws IOException {
		InputStreamReader read=new InputStreamReader(System.in);
		BufferedReader in=new BufferedReader(read);
		String str=in.readLine();
		String line[]=str.trim().split(" ");
		int k=Integer.parseInt(line[0]);
		int n=Integer.parseInt(line[1]);
		String st=in.readLine();
		int a[]=new int[n];
		int diff[]=new int[n];
		String l[]=st.trim().split(" ");
		int i;
		for(i=0;i<n;i++)
		{
		    a[i]=Integer.parseInt(l[i]);
		}
		diff[0]=a[0];
		for(i=1;i<n;i++)
		{
		    diff[i]=a[i]-a[i-1];
		}
		Arrays.sort(diff);
		int ans=Math.max((k-a[n-1]+a[0]),diff[n-1]);
		System.out.println((k-ans));
	}
}
