#include <bits/stdc++.h>
using namespace std;
int main() {
  int d, sum = 0;
  int tab[3];
  cin >> tab[0] >> tab[1] >> tab[2] >> d;
  sort(tab, tab + 3);
  if (tab[1] - tab[0] < d) sum += d - (tab[1] - tab[0]);
  if (tab[2] - tab[1] < d) sum += d - (tab[2] - tab[1]);
  cout << sum << "\n";
  return 0;
}
