#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
void test_case() {
  int d;
  int a[3];
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a, a + 3);
  int ans = 0;
  ans += max(0, d - (a[1] - a[0]));
  ans += max(0, d - (a[2] - a[1]));
  cout << ans << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int t = 1;
  while (t--) test_case();
  return 0;
}
