#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long int x, b, c, d;
  cin >> x >> b >> c >> d;
  long long int a[3];
  a[0] = x;
  a[1] = b;
  a[2] = c;
  sort(a, a + 3);
  long long int cc = 0;
  long long int df = abs(a[0] - a[1]);
  if (df < d) {
    cc += (d - df);
  }
  long long int dff = abs(a[1] - a[2]);
  if (dff < d) {
    cc += (d - dff);
  }
  cout << cc << endl;
  return 0;
}
