var n,c:int64;
    i:longint;
    sum,ans,ans2:int64;
    firstl,firstr:longint;
    x,v,num:array[0..100500]of int64;
begin
  read(n,c);
  for i:=1 to n do
  begin
    read(x[i],v[i]);
    num[i]:=num[i-1]+v[i];
    inc(sum,v[i]);
    if sum-x[i]>ans then
    begin
      ans:=sum-x[i];
      firstl:=i;
    end;
  end;
  sum:=0;
  for i:=n downto 1 do
  begin
    inc(sum,v[i]);
    if sum-(c-x[i])>ans2 then
    begin
      ans2:=sum-(c-x[i]);
      //writeln(i);
    end;
  end;
  if ans2>ans then ans:=ans2;
  for i:=1 to n do
  if x[i]>=c div 2 then
  begin
    firstr:=i;
    firstl:=i-1;
    break;
  end;
  for i:=1 to n do
  if x[i]<c div 2 then
  begin
    while (x[firstr+1]-num[firstr]<x[firstr]-num[firstr-1])and(firstr<n)do inc(firstr);
    if num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c>ans then
    ans:=num[i]+num[n]-num[firstr-1]-2*x[i]+x[firstr]-c;
  end;
  if firstl>0 then
  begin
  for i:=n downto 1 do
  if x[i]>=c div 2 then
  begin
    while (num[firstl-1]-x[firstl-1]>num[firstl]-x[firstl])and(firstl>1)do dec(firstl);
    if num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl]>ans then
    ans:=num[n]-num[i-1]+num[firstl]-2*(c-x[i])-x[firstl];
  end;
  end;
  writeln(ans);
end.

