/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	}
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	}
} Main public static void main (String[] args) throws java.lang.Exception
	{try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	} main {try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	} try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){} catch(Exception e){} Exception e {} {BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String str[] = br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long k=Long.parseLong(str[0]); k Long.parseLong(str[0]) Long.parseLong Long str[0] str 0 int n= Integer.parseInt(str[1]); n Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 String str1[]= br.readLine().split(" "); str1 br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " long max =-1; max -1 1 int i; i for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	} i=0; i=0 i 0 i<n-1 i n-1 n 1 i++ i++ i {
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	} {
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	} if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]); (Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max (Long.parseLong(str1[i+1])-Long.parseLong(str1[i])) Long.parseLong(str1[i+1]) Long.parseLong Long str1[i+1] str1 i+1 i 1 Long.parseLong(str1[i]) Long.parseLong Long str1[i] str1 i max max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]) max Long.parseLong(str1[i+1])-Long.parseLong(str1[i]) Long.parseLong(str1[i+1]) Long.parseLong Long str1[i+1] str1 i+1 i 1 Long.parseLong(str1[i]) Long.parseLong Long str1[i] str1 i long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]); t k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]) k+Long.parseLong(str1[0]) k Long.parseLong(str1[0]) Long.parseLong Long str1[0] str1 0 Long.parseLong(str1[n-1]) Long.parseLong Long str1[n-1] str1 n-1 n 1 if(t>max)max =t; t>max t max max =t max t System.out.print(k-max) System.out.print System.out System System.out k-max k max String[] args args public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	}
} public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	}
} Main
/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{try{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	String str[] = br.readLine().split(" ");
	long k=Long.parseLong(str[0]);
	int n= Integer.parseInt(str[1]);
	String str1[]= br.readLine().split(" ");
	long max =-1;
	int i;
	for(i=0;i<n-1;i++){
		if((Long.parseLong(str1[i+1])-Long.parseLong(str1[i]))>max) max = Long.parseLong(str1[i+1])-Long.parseLong(str1[i]);
	}
	long t = k+Long.parseLong(str1[0])-Long.parseLong(str1[n-1]);
	if(t>max)max =t;
	System.out.print(k-max);
		
	}catch(Exception e){}
		
	}
}