#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a[3], d, ans = 0;
  for (int i = 0; i < 3; i++) cin >> a[i];
  cin >> d;
  sort(a, a + 3);
  long long diff1 = a[1] - a[0], diff2 = a[2] - a[1];
  if (diff1 < d) ans += (d - diff1);
  if (diff2 < d) ans += (d - diff2);
  cout << ans << endl;
  return 0;
}
