#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long arr[3];
  long long d;
  cin >> arr[0] >> arr[1] >> arr[2];
  sort(arr, arr + 3);
  cin >> d;
  long long mn = 0;
  for (long long i = 0; i < 2; i++) {
    if (arr[i + 1] - arr[i] < d) {
      mn += (d - (arr[i + 1] - arr[i]));
    }
  }
  cout << mn << '\n';
  return 0;
}
