#include <bits/stdc++.h>
using namespace std;
const int mn = 1e5 + 5;
int a[4], d;
void input() {
  cin >> a[1] >> a[2] >> a[3] >> d;
  sort(a + 1, a + 4);
}
void work() {
  cout << max(0, d - (a[2] - a[1])) + max(0, d - (a[3] - a[2])) << endl;
}
signed main() {
  iostream ::sync_with_stdio(false);
  input();
  work();
  return 0;
}
