#include <bits/stdc++.h>
using namespace std;
long long a[4], d, t = 0;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> a[1] >> a[2] >> a[3];
  cin >> d;
  sort(a + 1, a + 4);
  if (a[3] - a[2] < d) t += d - (a[3] - a[2]);
  if (a[2] - a[1] < d) t += d - (a[2] - a[1]);
  cout << t;
}
