object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val c = sc.nextLong()
  val xvorig = Array.fill(n,2)(sc.nextLong())
  def abs(n:Long) = {
    if (n>=0){n}else{-n}
  }
  def distance(x:Long,y:Long) = {
    if (x-y>=0){
      x-y
    } else {
      c+x-y
    }
  }
  def eat(xv:Array[Array[Long]],pos:Long):Long = {
    if (xv.isEmpty){
      0
    } else {
      val sorted1 = xv.sortBy(xvi=>distance(xvi.apply(0),pos))
      val sorted2 = xv.sortBy(xvi=>distance(pos,xvi.apply(0)))
      val target1 = sorted1.head
      val target2 = sorted2.head
      val cal1 = target1(1) - distance(target1(0),pos)
      val cal2 = target2(1) - distance(pos,target2(0))
      val cal1rec = eat(sorted1.tail,target1(0))
      val cal2rec = eat(sorted2.tail,target2(0))
      val cal1total = cal1rec + cal1
      val cal2total = cal2rec + cal2
      Array(cal1total,cal2total,0).max
    }
  }
  println(eat(xvorig,0))
}