#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
const long long SZ = 107;
const long long N = 2e5 + 7;
const long long inf = 1e17;
long long pwr(long long x, long long y) {
  long long r = 1LL;
  while (y) {
    if (y & 1) r = (r * x) % MOD;
    y >>= 1;
    x = (x * x) % MOD;
  }
  return r % MOD;
}
long long inv(long long x) { return pow(x, MOD - 2); }
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  vector<long long> v;
  v.push_back(a);
  v.push_back(b);
  v.push_back(c);
  sort(v.begin(), v.end());
  long long ans = 0;
  long long d1 = v[1] - v[0];
  if (d1 < d) ans += d - d1;
  long long d2 = v[2] - v[1];
  if (d2 < d) ans += d - d2;
  cout << ans;
}
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
