#include <bits/stdc++.h>
const long long INF = 1e18;
const long long MOD = 1e9 + 7;
const long double PI = asin(1) * 2;
inline int sbt(int x) { return __builtin_popcount(x); }
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long SIZE = 0;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  vector<long long> vec(3);
  for (long long i = 0; i < 3; i++) cin >> vec[i];
  sort((vec).begin(), (vec).end());
  long long d;
  cin >> d;
  long long ans = 0;
  if (vec[1] - vec[0] < d) ans += (d - (vec[1] - vec[0]));
  if (vec[2] - vec[1] < d) ans += (d - (vec[2] - vec[1]));
  cout << ans;
  return 0;
}
