import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner scanner = new Scanner(System.in);
    
    int K = scanner.nextInt();
    int N = scanner.nextInt();
    int locations[] = new int[N];
    
    for(int i = 0;i < locations.length;i++){
      locations[i] = scanner.nextInt();
    }
    
    int sumDistance = 0;
    int maxDistance = locations[1] - locations[0];
    sumDistance += maxDistance;
    
    for(int i = 2;i < locations.length;i++){
        
      
      int distance = Math.abs(locations[i - 1] - locations[i]);
      
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      if(i == locations.length - 1){
        distance = Math.abs(K - locations[i]) + locations[0];
        sumDistance += distance;
        if(distance > maxDistance){
          maxDistance = distance;
        }
      }
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  }
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz