program main
   implicit none
   integer N, i, j
   integer(8) C, countN, cyR, cyL
   integer, allocatable :: v(:)
   integer(8), allocatable :: x(:), fR(:),fL(:), va(:, :)
   read(*, *) N, C
   allocate(x(0: N))
   allocate(v(0: N))
   allocate(fR(0: N))
   allocate(fL(0: N))
   allocate(va(0:N, 0:N))
   do i = 1, N
       read(*, *) x(i), v(i)
   end do

   x(0) = 0
   v(0) = 0
   fR(0) = 0
   fL(0) = 0
   do i = 1, N
      fR(i) = v(i) + fR(i-1)
      fL(i) = v(N+1-i) + fL(i-1)	  
   end do
   cyR = fR(N) - x(N)
   cyL = fL(N) - (C-x(1))
   va(:, :) = 0
   do j = 0, N
       do i = 0, N - j
	   if ( x(j) /= 0 ) then
		   va(i, j) = fR(i) + fL(j)  - 2 * min(x(i), C-x(N+1-j)) - max(x(i), C-x(N+1-j))
	   else
		   va(i, j) = fR(i) + fL(j)  - x(i)	       
	   end if
	   end do
   end do
   write(*, *) max(maxval(va(:, :)), cyR, cyL)
end program main