#include <bits/stdc++.h>
using namespace std;
int d;
long long a[10], b[10];
long long ans, ans1;
int main() {
  cin >> a[1] >> a[2] >> a[3] >> d;
  sort(a + 1, a + 4);
  if (a[2] - a[1] < d) ans += d - (a[2] - a[1]);
  if (a[3] - a[2] < d) ans += d - (a[3] - a[2]);
  memcpy(b, a, sizeof(a));
  ans1 = 0;
  if (b[2] - b[1] < d) ans1 += d - (b[2] - b[1]), b[2] = b[1] + d;
  if (b[3] - b[2] < d) ans1 += d - (b[3] - b[2]);
  ans = min(ans, ans1);
  memcpy(b, a, sizeof(a));
  ans1 = 0;
  if (b[3] - b[2] < d) ans1 += d - (b[3] - b[2]), b[2] = b[3] - d;
  if (b[2] - b[1] < d) ans1 += d - (b[2] - b[1]);
  ans = min(ans, ans1);
  cout << ans;
}
