#include <bits/stdc++.h>
using namespace std;
long long t, n, m, k, ccnt, pos = 0, sum = 0, minn2 = INT_MAX, sum2, cnt2 = 0,
                            cnt3 = 0, cnt4 = 0, cnt5 = 0, cnt6 = 0, cnt7 = 0,
                            vis[100005], x1, y7, x2, y2, x3, y3, aaa, bbb;
long long a[10000005], b[10000005], c[1000005], c3[1000005], c4[1000005],
    kk[1000005], cnt = 0, l, r, x, y, z, d = 0, minn = LONG_LONG_MAX,
                 maxx = -LONG_LONG_MAX;
long long pos1 = 0, pos2 = 0, tem, tem2, ans2, mod = 1000000007, tw[105],
          th[105], dd, ee, out[1000005], ans = 0, gggcd, dp[7][100005];
string s1, s2, s;
char cc[1005][1005], ch;
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> x >> y >> z >> n;
  a[0] = x;
  a[1] = y;
  a[2] = z;
  sort(a, a + 3);
  x = a[0];
  y = a[1];
  z = a[2];
  m = y;
  cnt = 0;
  if (y <= x + n) {
    cnt = cnt + (x + n) - y;
    y = x + n;
  }
  if (z <= y + n) {
    cnt = cnt + (y + n) - z;
  }
  y = m;
  cnt2 = 0;
  if (x >= y - n) {
    cnt2 = cnt2 + x - (y - n);
  }
  if (z <= y + n) {
    cnt2 = cnt2 + (y + n) - z;
  }
  cnt3 = 0;
  if (y >= z - n) {
    cnt3 = cnt3 + y - z + n;
    y = z - n;
  }
  if (x >= y - n) {
    cnt3 = cnt3 + x - y + n;
  }
  cout << min(cnt, min(cnt2, cnt3));
}
