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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  }
} import java.util.Scanner; Scanner java.util 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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  }
} 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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  } main {
    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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  } 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 int locations[] = new int[N]; locations new int[N] N for(int i = 0;i < locations.length;i++){
      locations[i] = scanner.nextInt();
    } int i = 0; int i = 0; i 0 i < locations.length i locations.length locations locations.length i++ i++ i {
      locations[i] = scanner.nextInt();
    } {
      locations[i] = scanner.nextInt();
    } locations[i] = scanner.nextInt() locations[i] locations i scanner.nextInt() scanner.nextInt scanner int sumDistance = 0; sumDistance 0 int maxDistance = locations[1] - locations[0]; maxDistance locations[1] - locations[0] locations[1] locations 1 locations[0] locations 0 sumDistance += maxDistance sumDistance maxDistance for(int i = 2;i <= locations.length;i++){
        
      int distance;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    } int i = 2; int i = 2; i 2 i <= locations.length i locations.length locations locations.length i++ i++ i {
        
      int distance;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    } {
        
      int distance;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    } int distance; distance if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      } i == locations.length i locations.length locations locations.length {
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      } distance = Math.abs(K - locations[i - 1]) + locations[0] distance Math.abs(K - locations[i - 1]) + locations[0] Math.abs(K - locations[i - 1]) Math.abs Math K - locations[i - 1] K locations[i - 1] locations i - 1 i 1 locations[0] locations 0 {
      	distance = Math.abs(locations[i - 1] - locations[i]);
      } distance = Math.abs(locations[i - 1] - locations[i]) distance Math.abs(locations[i - 1] - locations[i]) Math.abs Math locations[i - 1] - locations[i] locations[i - 1] locations i - 1 i 1 locations[i] locations i sumDistance += distance sumDistance distance if(distance > maxDistance){
        maxDistance = distance;
      } distance > maxDistance distance maxDistance {
        maxDistance = distance;
      } maxDistance = distance maxDistance distance sumDistance -= maxDistance sumDistance maxDistance System.out.println(sumDistance) System.out.println System.out System System.out sumDistance String[] args args 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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  }
} 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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  }
} Main
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;
      if(i == locations.length){
        distance = Math.abs(K - locations[i - 1]) + locations[0];
      }else{
      	distance = Math.abs(locations[i - 1] - locations[i]);
      }
      sumDistance += distance;
      if(distance > maxDistance){
        maxDistance = distance;
      }
      
      
    }
    
    sumDistance -= maxDistance;
    System.out.println(sumDistance);
  }
}