#include <bits/stdc++.h>
using namespace std;
void solve() {
  vector<long long> x(3);
  long long d;
  cin >> x[0] >> x[1] >> x[2] >> d;
  sort(x.begin(), x.end());
  cout << max(0LL, d - x[1] + x[0]) + max(0LL, d - x[2] + x[1]);
}
signed main() {
  solve();
  return 0;
}
