#include <bits/stdc++.h>
using namespace std;
int main() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  long long l = ((a < b) ? a : b);
  l = ((l < c) ? l : c);
  long long m = ((a > b) ? a : b);
  m = ((m > c) ? m : c);
  long long middle;
  if (a != l && a != m)
    middle = a;
  else if (b != l && b != m)
    middle = b;
  else
    middle = c;
  long long ans = 0;
  if (d - (m - middle) > 0) ans += d - (m - middle);
  if (d - (middle - l) > 0) ans += d - (middle - l);
  cout << ans << endl;
  return 0;
}
