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 eat1(xv:Array[Array[Long]],pos:Long,turned:Boolean):Long = {
    if (xv.isEmpty){
      0
    } else {
      if (!turned) {
        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 = eat1(sorted1.tail, target1(0), false)
        val cal2rec = eat2(sorted2.tail, target2(0), true)
        val cal1total = cal1rec + cal1
        val cal2total = cal2rec + cal2
        Array(cal1total, cal2total, 0).max
      } else {
        val sorted1 = xv.sortBy(xvi => distance(xvi.apply(0), pos))
        val target1 = sorted1.head
        val cal1 = target1(1) - distance(target1(0), pos)
        val cal1rec = eat1(sorted1.tail, target1(0), true)
        val cal1total = cal1rec + cal1
        Array(cal1total, 0).max
      }
    }
  }
  def eat2(xv:Array[Array[Long]],pos:Long,turned:Boolean):Long = {
    if (xv.isEmpty){
      0
    } else {
      if (!turned) {
        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 = eat1(sorted1.tail, target1(0), true)
        val cal2rec = eat2(sorted2.tail, target2(0), false)
        val cal1total = cal1rec + cal1
        val cal2total = cal2rec + cal2
        Array(cal1total, cal2total, 0).max
      }else {
        val sorted1 = xv.sortBy(xvi => distance(xvi.apply(0), pos))
        val target1 = sorted1.head
        val cal1 = target1(1) - distance(target1(0), pos)
        val cal1rec = eat1(sorted1.tail, target1(0), true)
        val cal1total = cal1rec + cal1
        Array(cal1total,  0).max
      }
    }
  }

  println(Array(eat1(xvorig,0,false),eat2(xvorig,0,false)).max)
}