#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1e6 + 7;
long long MOD;
void f_io() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
}
int32_t main() {
  f_io();
  long long a, b, c, d, ans = 0;
  cin >> a >> b >> c >> d;
  long long M = max(a, max(b, c));
  long long m = min(a, min(b, c));
  long long k = a + b + c - M - m;
  if (k - m < d) ans += d - (k - m);
  if (M - k < d) ans += d - (M - k);
  cout << ans;
}
