#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0;
  int zf = 1;
  char ch = ' ';
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') zf = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * zf;
}
int a[3];
int main() {
  a[0] = read(), a[1] = read(), a[2] = read();
  int d = read();
  sort(a, a + 3);
  int ans = 0;
  if (a[1] - a[0] < d) ans += d - a[1] + a[0];
  if (a[2] - a[1] < d) ans += d - a[2] + a[1];
  printf("%d", ans);
  return 0;
}
