#include <bits/stdc++.h>
using namespace std;
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
int main() {
  int a, b, c, d, f, s, e;
  int ans = 0;
  cin >> a >> b >> c >> d;
  f = min(min(a, b), c);
  e = max(max(a, b), c);
  s = a + b + c - e - f;
  if (d - (s - f) > 0) ans += (d - (s - f));
  if (d - (e - s) > 0) ans += (d - (e - s));
  cout << ans;
}
