#include <bits/stdc++.h>
using namespace std;
const unsigned long long mod = 1e9 + 7;
template <class S, class T>
ostream& operator<<(ostream& os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const vector<vector<T>> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<long long> a(3);
  long long d;
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a.begin(), a.end());
  long long res = 0;
  if (a[1] - a[0] < d) res += (d - a[1] + a[0]);
  if (a[2] - a[1] < d) res += (d - a[2] + a[1]);
  cout << res << endl;
  return 0;
}
