#include <cstdio>
#include <cstdlib>
#define MAXN            1000000
typedef long long Integer;

Integer coins[MAXN];

int main()
{
  FILE * in = fopen("zetoni.in", "r"),
       * out = fopen("zetoni.out", "w");
       
  int n, k;
  fscanf(in, "%d %d", &n, &k);
  for (int i = 0; i < n; ++i) {
    fscanf(in, "%lld", &coins[i]);
  }
  
  Integer moves = 0;
  for (int i = 1; i < n; ++i) {
    if (coins[i] <= coins[i - 1] && i != n - 1 && coins[i] <= coins[i + 1]) {
      Integer diffA = abs(coins[i - 1] - coins[i]), diffB = abs(coins[i + 1] - coins[i]);
      if (diffA <= diffB)
        coins[i] = coins[i - 1], moves += abs(diffA);
      else coins[i] = coins[i + 1], moves += abs(diffB);
    } else if (coins[i] <= coins[i - 1]) {
      moves += abs(coins[i - 1] - coins[i]);
      coins[i] = coins[i - 1];
    } else if (i != n - 1 && coins[i] <= coins[i + 1]) {
      moves += abs(coins[i + 1] - coins[i]);
      coins[i] = coins[i + 1];
    } else {
      moves += abs(coins[i] - coins[i - 1]);
      coins[i - 1] = coins[i];
    }
  }
  int median = coins[n-2];
  moves += k - median;
    
  fprintf(out, "%lld", moves);
  fclose(in), fclose(out);
  
  return 0;
}
