#include <bits/stdc++.h>
using namespace std;
int main() {
  int arr[4];
  for (int i = 0; i < 4; i++) cin >> arr[i];
  int d = arr[3];
  sort(arr, arr + 3);
  int c1 = d - (arr[1] - arr[0]);
  int c2 = d - (arr[2] - arr[1]);
  int sum = 0;
  if (c1 > 0) sum += c1;
  if (c2 > 0) sum += c2;
  cout << sum;
  return 0;
}
