#include <bits/stdc++.h>
using namespace std;
int a[3], d;
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> a[0] >> a[1] >> a[2] >> d;
  sort(a, a + 3);
  cout << max(0, d - a[1] + a[0]) + max(0, d - a[2] + a[1]);
}
