#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  vector<long long> arr(3);
  cin >> arr[0] >> arr[1] >> arr[2];
  long long d;
  cin >> d;
  sort(arr.begin(), arr.end());
  long long res = 0;
  if (d - (arr[1] - arr[0]) > 0) {
    res += d - (arr[1] - arr[0]);
  }
  if (d - (arr[2] - arr[1]) > 0) {
    res += d - (arr[2] - arr[1]);
  }
  cout << res;
}
