object Main extends App {
  import scala.io.StdIn._
  val Array(n, l) = readLine.trim.split(' ').map(_.toInt)
  val pawns = Array.fill(n){
    readLine.trim.split(' ').map(_.toInt) match {
      case Array(pos, 0) ⇒ Left(pos)
      case Array(pos, 1) ⇒ Right(pos)
    }
  }.sortBy(_.position)
  var currentScore = 0L
  val forward = Array.fill(n){0L}
  for (i ← pawns.indices) {
    pawns(i) match {
      case Left(pos) ⇒
        currentScore += pos - i - 1
      case Right(pos) ⇒
        currentScore += i - pos + 1
    }
    forward(i) = currentScore
  }
  currentScore = 0L
  val backward = Array.fill(n){0L}
  for (i ← pawns.indices.reverse) {
    pawns(i) match {
      case Left(pos) ⇒
        currentScore += pos - (l - (n - i - 1))
      case Right(pos) ⇒
        currentScore += (l - (n - i - 1)) - pos
    }
    backward(i) = currentScore
  }
  var min = math.max(forward.last, backward.head)
  for (i ← pawns.indices.tail) {
    min = math.max(min, forward(i - 1) + backward(i))
  }
  println(min)
  sealed class Pawn(val position: Int)
  case class Left(override val position: Int) extends Pawn(position)
  case class Right(override val position: Int) extends Pawn(position)
}

