#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b, c, d, cnt = 0;
  int ara[3];
  cin >> ara[0] >> ara[1] >> ara[2] >> d;
  sort(ara, ara + 3);
  int biogfol = ara[2] - ara[1];
  if (biogfol < d) {
    biogfol = d - biogfol;
    ara[2] = ara[2] + biogfol;
    cnt = cnt + biogfol;
  }
  int biogfol2 = ara[2] - ara[0];
  if (biogfol2 < d) {
    biogfol2 = d - biogfol2;
    ara[2] = ara[2] + biogfol2;
    cnt = cnt + biogfol2;
  }
  int biogfol3 = ara[1] - ara[0];
  if (biogfol3 < d) {
    biogfol3 = d - biogfol3;
    ara[1] = ara[1] + biogfol3;
    cnt = cnt + biogfol3;
  }
  cout << cnt << endl;
  return 0;
}
