Polycarp decided to relax on his weekend and visited to the performance of famous ropewalkers: Agafon, Boniface and Konrad.The rope is straight and infinite in both directions. At the beginning of the performance, Agafon, Boniface and Konrad are located in positions $$$a$$$, $$$b$$$ and $$$c$$$ respectively. At the end of the performance, the distance between each pair of ropewalkers was at least $$$d$$$.Ropewalkers can walk on the rope. In one second, only one ropewalker can change his position. Every ropewalker can change his position exactly by $$$1$$$ (i. e. shift by $$$1$$$ to the left or right direction on the rope). Agafon, Boniface and Konrad can not move at the same time (Only one of them can move at each moment). Ropewalkers can be at the same positions at the same time and can "walk past each other".You should find the minimum duration (in seconds) of the performance. In other words, find the minimum number of seconds needed so that the distance between each pair of ropewalkers can be greater or equal to $$$d$$$.Ropewalkers can walk to negative coordinates, due to the rope is infinite to both sides.

The only line of the input contains four integers $$$a$$$, $$$b$$$, $$$c$$$, $$$d$$$ ($$$1 \le a, b, c, d \le 10^9$$$). It is possible that any two (or all three) ropewalkers are in the same position at the beginning of the performance.

Output one integer — the minimum duration (in seconds) of the performance.

NoteIn the first example: in the first two seconds Konrad moves for 2 positions to the right (to the position $$$8$$$), while Agafon and Boniface stay at their positions. Thus, the distance between Agafon and Boniface will be $$$|5 - 2| = 3$$$, the distance between Boniface and Konrad will be $$$|2 - 8| = 6$$$ and the distance between Agafon and Konrad will be $$$|5 - 8| = 3$$$. Therefore, all three pairwise distances will be at least $$$d=3$$$, so the performance could be finished within 2 seconds.

#include <bits/stdc++.h>
using namespace std;

int main() {
  int a, b, c, d;
  scanf("%d %d %d %d", &a, &b, &c, &d);
  int t[3];
	t[0] = a, t[1] = b, t[2] = c;
  sort(t, t + 3);
  a = t[0], b = t[1], c = t[2];
  if (c - a >= d + d) {
    printf("%d", max(0, d - min(c - b, b - a)));
  } else {
    if (b - a >= d) {
      printf("%d", max(0, d - (c - b)));
    } else if (c - b >= d) {
      printf("%d", max(0, d - (b - a)));
    } else {
      printf("%d", (d - (b - a)) + (d - (c - b)));
    }
  }
  return 0;
}

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int pos[5];
int main()
{
	int d;
	scanf("%d%d%d%d",&pos[0],&pos[1],&pos[2],&d);
	sort(pos,pos+3);
	int ans=max(0,d-(pos[1]-pos[0]));
	ans+=max(0,d-(pos[2]-pos[1]));
	printf("%d\n",ans);

}

#include<iostream>
#include<cstdio>
#include<algorithm>

using namespace std;

int main()
{
	int a[3],d;
	for(int i=0;i<3;i++)
	cin>>a[i];
	cin>>d;
	sort(a,a+3);
	int ans[3];
	ans[0]=a[0];
	ans[1]=ans[0]+d;
	ans[2]=ans[1]+d;

	int x1=(ans[1]-a[1]);
	int x2=(ans[2]-a[2]);
	if(x1<0)x1=0;
	if(x2<0)x2=0;
	int y=x1>x2?x1:x2;
	cout<<y<<endl;

	return 0;
}

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[4], d, ans;
int main() {
	scanf("%d %d %d %d", &a[1], &a[2], &a[3], &d);
	sort(a + 1, a + 4);
	ans = max(d - a[2] + a[1], 0) + max(d- a[3] + a[2], 0);
	printf("%d\n", ans);
	return 0;
}

#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second

using namespace std;
typedef long long INT;
typedef pair <int, int> pii;
typedef vector <int> VI;

int main (){
#ifndef ONLINE_JUDGE
	freopen ("in.in", "r", stdin);
	freopen ("out.out", "w", stdout);
#endif

	int a[4];
	for(int i=0; i<4; i++) cin >> a[i];

	sort(a, a+3);
	cout << max(0, a[3]-a[1]+a[0])+max(0, a[3]-a[2]+a[1]) << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[109],d1,d,d2,f1,f2;
    for(int i=0;i<3;i++)
    {
        cin>>a[i];
    }
    cin>>d;
    sort(a,a+3);
    d1=max(0,d-(a[1]-a[0]));
    d2=max(0,d-(a[2]-a[1]));
    cout<<d1+d2<<endl;

}

#include <iostream>
using namespace std;
int a,b,c,d,g,s,x,n,m;
bool y;
int main() {
cin>>a>>b>>c>>d;
g=0;s=0;
x=a;
if (x<b){ x=b;}
if (x<c){ x=c;}
n=a;
if (n>b){ n=b;}
if (n>c){ n=c;}
m=a+b+c-x-n;
while(x-m<d){
x=x+1;
s=s+1;
}
while(m-n<d){
n=n-1;
g=g+1;
}
cout<<s+g;
}

#include<bits/stdc++.h>
using namespace std;

struct Rope{
    long int a[3];
};

Rope Q ;   long int d;

Rope SORT ( Rope a ){
    for(int i=0 ; i<2 ; i++){
        for(int j=0 ; j<2 ; j++){
            if(a.a[j]>a.a[j+1]){
                swap(a.a[j],a.a[j+1]);
            }
        }
    }
    return a;
}

int main()
{
    long int count1 = 0;
    for(int i=0 ; i<3 ; i++){
        cin>>Q.a[i];
    }
    Q=SORT(Q);
    cin>>d;
    while(Q.a[1]-Q.a[0]<d || Q.a[2]-Q.a[1]<d ){
        if(Q.a[2]-Q.a[1] <= Q.a[1]-Q.a[0] ){
            count1+=(d-(Q.a[2]-Q.a[1]));
            Q.a[2]=Q.a[1]+d;
        }else {
            count1+=(d-(Q.a[1]-Q.a[0]));
            Q.a[0]=Q.a[1]-d;
        }
    }
    cout<<count1;
    return 0;
}

#include <iostream>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#define ll long long
using namespace std;

int main()
{
	ll a,b,c,d,k=0;
	cin>>a>>b>>c>>d;
	ll x,y,z;
	z=max(a,max(b,c));
	x=min(a,min(b,c));
	if(x==a && z==c)
	{
		y=b;
	}
	if(x==a && z==b)
	{
		y=c;
	}
	if(x==b && z==c)
	{
		y=a;
	}
	if(x==b && z==a)
	{
		y=c;
	}
	if(x==c && z==a)
	{
		y=b;
	}
	if(x==c && z==b)
	{
		y=a;
	}
	ll p,q;
	//cout<<x<<" "<<y<<" "<<z<<endl;
	p=abs(y-x);
	q=abs(z-y);
	if(p<d)
	{
		x=x+p-d;
		k=k+abs(p-d);
		//cout<<"p"<<p<<endl;
	}
	if(q<d)
	{
		z=z+d-q;
		k=k+abs(d-q);
		//cout<<"q"<<q<<endl;
	}
	cout<<k;
	return 0;
}

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
  int a[3], d;
  cin >> a[0] >> a[1] >> a[2] >> d;

  sort(a, a+3);

  int ans = 0;
  for (int i=0; i<2; i++)
  {
    int diff = -a[i]+ a[i+1];
    if ( diff < d)
    {
      ans += (d-diff);
    }
  }

  int tmp = 0;
  for (int i=2; i>0; i--)
  {
    int diff = a[i] -a [i-1];
    if ( diff < d )
    {
      tmp += (d-diff);
    }
  }
  ans = min(ans, tmp);

  cout << ans<<"\n";

}

#include <iostream>

using namespace std;

int a[3], d;

int main(void) {
    for(int i = 0; i < 3; i++)
        cin >> a[i];
    cin >> d;
    for(int i = 0; i < 2; i++)
        for(int j = i + 1; j < 3; j++)
            if (a[i] > a[j])
                swap(a[i], a[j]);
    cout << max(0, d - (a[1] - a[0])) + max(0, d - (a[2] - a[1])) << endl;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	unsigned long long int a,b,c,d,l,m,r,ar[3],def1,def2,res=0;
	cin>>a>>b>>c>>d;
	ar[0]=a;
	ar[1]=b;
	ar[2]=c;
	sort(ar,ar+3);
	l=ar[0];
	m=ar[1];
	r=ar[2];
	def1=m-l;
	def2=r-m;
	if(def1 < d)
	{
		res+=(l-(m-d));
	}
	if(def2 <d )
	{
		res+=((m+d)-r);
	}
	cout<<res<<endl;
	return 0;
}

#include <bits/stdc++.h>
#define read(x) int x; cin>>x
#define pi pair<int, int>
#define pl pair<ll, ll>
#define newa(x, type, n) type* x = new type[n + 1]

typedef long long ll;

using namespace std;

int test()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int a[3], d;
	for (int i = 0; i < 3; i++)
		cin >> a[i];
	cin >> d;
	sort(a, a + 3);
	int cnt = 0;
	if (a[1] - a[0] < d)
	{
		cnt += d - (a[1] - a[0]);
	}
	if (a[2] - a[1] < d)
	{
		cnt += d - (a[2] - a[1]);
	}
	cout << cnt;
	return 0;
}

/*public*/ static int solve(int tmp[], int a, int b, int n)
{
	int cnt = 0;
	int div = tmp[b] - tmp[a];
	int now = 2;
	for (int i = b + 1; i < n; i++)
	{
		if (tmp[i] == tmp[a] + div * now)
		{
			cnt++;
			now++;
		}
	}
	return cnt;
}

/*public*/ static int search(int num, int b[], int n)
{
	for (int i = 0; i < n; i++)
	{
		if (b[i] == num)
		{
			/*System.out.println(i+1);*/
			i + 1;
			return 0;
		}
	}
	return 1;
}

int donot(){return 0;}

int hack(){return 1;}

string me(){
	const char* s = "do not hack me";
	return s;
}

class havenoidea{
	int ameaninglessnum = 49935;
	bool iamanaduit = 1;
public:
	int iwillsolvefiveproblems(int x)
	{
	return x;}};

int main(){if(hack())
	    donot();
int tmp[6] = {1,1,4,5,1,4};
solve(tmp, search(114514, tmp, 6), 1, 6);

	havenoidea fuck;fuck.iwillsolvefiveproblems(hack());
	test();if (me() == "do not fack me")
		if (me() == "do not hack me")hack() == donot();return 0;
}

#include<iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define ll long long

int main() {
	int t = 1;
	// cin >> t;
	while (t--)
	{
		int c[3], d;
		for (int i = 0; i < 3; i++) cin >> c[i];
		cin >> d;
		sort(c, c + 3);
		cout << max(0, d - (c[1] - c[0])) + max(0, d - (c[2] - c[1])) << endl;
	}
}

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll i,j,k,n,ans=0,q,t,a,b,c,d;
    cin>>a>>b>>c>>d;

    vector<ll> v;
    v.pb(a);
    v.pb(b);
    v.pb(c);

    if(a == b && a == c)
    {
        ans = 2 * abs(d);
        cout<<ans;
        return 0;
    }

    sort(v.begin(),v.end());

    if(v[0] == v[1])
    {
        ans=d;
        t=v[2]-v[1];
        if(t<d)
            ans+=abs(d-t);
        cout<<ans;
        return 0;
    }
    else if(v[1] == v[2])
    {
        ans=d;
        t=v[1]-v[0];
        if(t<d)
            ans+=abs(d-t);
         cout<<ans;
        return 0;

    }
    else
    {
        t=v[2]-v[1];
        if(t<d)
            ans+=abs(d-t);

        t=v[1]-v[0];
        if(t<d)
            ans+=abs(d-t);

        cout<<ans;
        return 0;
    }

    return 0;
}

//@alok__10
#include <bits/stdc++.h>
using namespace std;
#define fastio      ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(0);
#define MOD         1000000007
#define pie         3.141592653
#define ll          long long
#define lli         long long int
#define ulli        unsigned long long int
#define ull         unsigned long long
#define vi          vector<int>
#define vvi         vector<vi>
#define vlli        vector<lli>
#define vll         vector<ll>
#define pb          push_back
#define uset        unordered_set
#define useti       unordered_set<int>
#define umap        unordered_map
#define loop(i,n)   for(int i=0;i<n;i++)
#define clr(a,x)    memset(a,x,sizeof(a))
#define all(a)      a.begin(),a.end()
#define mp          make_pair
#define Pi          pair<int, int>
#define ppi         pair<ll,ll>
#define u_b         upper_bound
#define l_b         lower_bound
#define b_s         binary_search
// -----------------------------------------------------------------------------------
// is prime or not
bool prime(ll a) {
    if (a <= 1) return 0;
    if (a == 2 || a == 3) return 1;
    if (a % 2 == 0 || a % 3 == 0) return 0;
    for (int i = 5; i <= sqrt(a); i += 6)
        if (a % i == 0 || a % (i + 2) == 0)return 0;
    return 1;
}
// comparator to sort vector of pairs in decending order of the first element
// if 2 elements are equal than the smaller second element gets the priority
bool cmp(pair<ll, ll>& p1, pair<ll, ll>& p2) {
    if (p1.first < p2.first)return 0;
    if (p1.first == p2.first)
        if (p1.second > p2.second)return 0;
    return 1;
}
// BINARY EXPONENTIATION(WITH MOD=1000000007)
long long binpow(long long a, long long b) {
    a %= MOD;
    long long res = 1;
    while (b) {
        if (b & 1) res = res * a % MOD;
        a = (a * a) % MOD;
        b >>= 1;
    }
    return res;
}
// is sorted or not (for array)
bool sorted(vll v) {
    loop(i, v.size() - 1) if (v[i] > v[i + 1]) return 0;
    return 1;
}
//solution goes below
// -----------------------------------------------------------------------------------------------------------------------------------
ll a, b, c, d, n, t, res = 0;
const ll size = 10000000;
ll arr1[size], arr2[size], arr3[size];

int main() {
    fastio
#ifndef ONLINE_JUDGE
    freopen("tin.txt", "r", stdin);
    freopen("tout.txt", "w", stdout);
#endif

    vll v(3);
    loop(i, 3) cin >> v[i];
    cin >> d;
    sort(all(v));
    ll ans = 0;
    if (abs(v[1] - v[0]) < d) ans += (d - abs(v[1] - v[0]));
    v[0] -= (d - abs(v[1] - v[0]));
    sort(all(v));
    if (abs(v[2] - v[1]) < d) ans += (d - abs(v[2] - v[1]));
    cout << ans << endl;

#ifndef ONLINE_JUDGE
    cout << "\n\nTime lap: " << 1.0 * clock() / CLOCKS_PER_SEC << " sec\n";
#endif
    return 0;
}
//-----------------------------------------------------------------------------------------------------------------------------

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <bitset>
#include <vector>
#include <string>
#include <set>
#include <queue>
#include <stack>
#include <assert.h>
#include <algorithm>
#include <string>

using namespace std;
#define ll long long

int main()
{
	int a[3], d;
	cin >> a[0] >> a[1] >> a[2] >> d;
	sort(a, a + 3);
	int result = 0;
	result += max(0, d - (a[2] - a[1])) + max(0, d - (a[1] - a[0]));
	cout << result << endl;
	return 0;
}

#include <iostream>
#include<algorithm>
using namespace std;
long long f(long long x)
{
    long long ret;
    if(x>0)ret=x;
    else ret=-x;
    return ret;
}
int main()
{
    long long a[10],d,ans,x1,x2,mi,ma;
    cin>>a[0]>>a[1]>>a[2]>>d;
    sort(a,a+3);
   // cout<<a[0]<<' '<<a[1]<<' '<<a[2];
    x1=f(a[0]-a[1]);x2=f(a[1]-a[2]);
    mi=min(x1,x2);
    ma=max(x1,x2);
    //cout<<ma<<' '<<mi;
    if(mi>=d)ans=0;
    else if(ma>=d)ans=d-mi;
    else if(ma<d)ans=2*d-mi-ma;
    cout<<ans;
}

#include <bits/stdc++.h>
#define ll long long int
#define P pair
#define F first
#define S second
#define m_p make_pair
#define ld long double
//#include <wadim>

using namespace std;

string alpha = "abcdefghijklmnopqrstuvwxyz";
string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string glas = "aeiou";
ll Mod = 998244353;
ll ctoi(char x) {
	ll xx = int(x) - 48;
	return xx;
}

ll one = 1;
ll null = 0;

int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
    vector<ll> arr(3);
    ll d;
    for(ll i=0;i<3;i++) cin >> arr[i];
    cin >> d;
    sort(arr.begin(),arr.end());
    cout << max(null,d-arr[1]+arr[0])+max(null,d-arr[2]+arr[1]);
}

#include<bits/stdc++.h>
using namespace std;
long long arr[4];
long long d,x,y,sum;
void do_king (){

    sum=0;
    cin>>arr[0]>>arr[1]>>arr[2];
    cin>>d;
    sort(arr,arr+3);
    x=d-arr[2]+arr[1];

    if(x>0) {
        sum+=x;

    }
    y=d-(arr[1]-arr[0]);
    if(y>0){
        sum+=y;

    }
    cout<<sum;
}
int main()
{
    ios_base::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	do_king();
}

#include<bits/stdc++.h>
using namespace std;

int main()
{
  int arr[3],a,b,c,d,k=0;
  cin>>arr[0]>>arr[1]>>arr[2]>>d;
  sort(arr,arr+3);
  a=arr[0];
  b=arr[1];
  c=arr[2];
  if(b-a>d&&c-b>d)
  k=0;
  else if(a==c)
  {
    k=2*d;
  }
  else
  {
    if(c-b>=d)
    {
      k=abs(a-b+d);
    }
    else if(b-a>=d)
    {
      k=abs(c-b-d);
    }
    else
    k=d+a-b+d-c+b;
  }
  cout<<k;
  return 0;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include <initializer_list>
#include<unordered_set>
#include<map>
#include <iterator>
#include<stack>
#include<queue>
#include <iomanip>
using namespace std;

int main(){
    /*int x,sum=0;
    string o,n;
    cin>>x;
    cin>>o>>n;
    for (int i=0;i<x;i++){
        if (abs(o[i]-n[i])<=5)
            sum=sum+abs(o[i]-n[i]);
        else
            sum=sum+(10-abs(o[i]-n[i]));
    }
    cout<<sum<<endl;*/
    /*int x,c=1,r;
    cin>>x;
    r=x;
    string s,q;
    while (x!=0){
    cin>>s;
    if (c==1){
    if (s[0]=='O' && s[1]=='O')
    {
        s[0]='+';
        s[1]='+';
            c--;

    }
    else if ( s[3]=='O'&&s[4]=='O')
    {
        s[3]='+';
        s[4]='+';

        c--;

    }
    }
    q=q+s;
    x--;
    }
    if (c==0){
    cout<<"YES\n";

    for (int i=0;i<q.length();i++){
        cout<<q[i];
    if ((i+1)%5==0)
        cout<<endl;
    }
    }
    else
        cout<<"NO\n";*/
    /*bool t=false;
    char c;
    int x,y;
    cin>>x>>y;
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            cin>>c;
            if (c!='W' && c!='B' && c!='G')
                t=true;
        }
    }
    if (t==true)
        cout<<"#Color\n";
    else
        cout<<"#Black&White\n";*/
    /*int x,y;
    cin>>x;
    int c=0;
    int *b=new int [x];
    for (int i=0;i<x;i++){
        cin>>b[i];
    }
    cin>>y;
    int *g=new int [y];
    for (int i=0;i<y;i++){
        cin>>g[i];
    }
    sort(b,b+x);
    sort(g,g+y);
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            if (g[j]!=102 && b[i]==g[j] || abs(b[i]-g[j])==1){
                g[j]=102;
                c++;
                break;
            }
        }
    }
    cout<<c<<endl;*/
    /*int x,c=1,sum=0,con=0;
    cin>>x;
    int *y=new int [x];
    for (int i=0;i<x;i++)
        cin>>y[i];
    int d,m;
    cin>>d>>m;
    if (x==1){
        if (y[0]==d)
        cout<<1<<endl;
    }
    else {
    for (int i=0;i<x;i++){
        sum=0;
        sum=sum+y[i];
        c=1;
		while (c<=m){
        for (int j=i+1;j<x;j++){

            c++;
            sum=sum+y[j];
            if (c==m){
                if (sum==d)
                    con++;

            }
			if (c>m)
				break;
        }
		break;
        }

    }
    cout<<con<<endl;
    }*/
/*int n;
    cin>>n;
    int *ar=new int [n];
int s[6]={0,0,0,0,0,0};
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
      s[ar[i]-1]++;

    }

    int fact=1;
   for (int i = 1; i <= n; i++) {
        fact = fact * i;
   }
    int fact1=1;
    for(int i=1;i<=6;i++)
    {
        fact1=1;
        for(int j=1;j<=s[i-1];j++)
        {
            fact1 = fact1 * j;

        }
        fact/=fact1;

    }
    cout<<fact;*/
/*int gcd,l,b,n;
cin>>n;
while (n!=0){
	cin>>l>>b;
	if (l>=b){
	 for(int i=1;i<l;i++)
    {
        if(l%i==0 && b%i==0)
        {
            gcd=i;
        }
    }
	}
	else{
		 for(int i=1;i<b;i++)
    {
        if(l%i==0 && b%i==0)
        {
            gcd=i;
        }
    }
	}
	cout<<(b*l)/(gcd*gcd)<<endl;
	n--;
}*/
/*int x;
cin>>x;
long q,n,y,z;
while (x!=0)
{
	z=(ceil(q/2.0)*y);
	cin>>q>>n>>y;
	if ((ceil(q/2.0)*y<(q*n)))
		cout<<z<<endl;
	else
		cout<<q*n<<endl;
	x--;

}*/
/*int n;
cin>>n;
int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
long sum=0,k=0;
for (int i=0;i<n;i++)
{
	sum=0;
	for (int j=0;j<n;j++){
		if (y[i]==y[j] || abs(y[i]-y[j])!=1)
			sum=sum+y[j];
	}
	if (sum>k)
		k=sum;

}
 cout<<k<<endl;*/
/*long a,b,c,m,n,h;
cin>>a>>b>>c;
m=max(a*b*c,a+b+c);
n=max(a*(b+c),(a+b)*c);
cout<<max(n,m)<<endl;*/
/*long long x;
cin>>x;
if (x%2==0)
	cout<<(long long)x/2<<endl;
else
	cout<<(long long)(ceil(x/2.0)*-1)<<endl;
    system("pause");*/
/*long x,t,c=0,k=0;
cin>>x;
long y[4]={0,0,0,0};
for (int i=0;i<x;i++){
	cin>>t;
	y[t-1]++;
}
c=y[3];
if (y[1]%2==0){
	if (y[2]==y[0]){
	c=c+y[0];
	c=c+y[1]/2;
	cout<<c<<endl;
	}
	else if (y[2]>y[0]){
	c=c+y[1]/2;
	c=c+y[0];
	c=c+y[2]-y[0];
	cout<<c<<endl;
	}
	else{
	c=c+y[1]/2;
	c=c+y[2];
	c=c+(y[0]-y[2])/4;
	if ((y[0]-y[2])%4!=0)
		c++;
	cout<<c<<endl;
	}
}
else{
	if (y[2]==y[0]){
	c=c+y[0];
	c=c+y[1]/2+1;
	cout<<c<<endl;
	}

	else if (y[2]>y[0]){
	c=c+y[0];
	c=c+y[1]/2+1;
	c=c+(y[2]-y[0]);
	cout<<c<<endl;
	}
	else {
	c=c+y[2];
	y[0]=y[0]-y[2];

	c=c+y[1]/2;

	y[1]=1;

	for (int i=y[0];i>=1;i--){

		if ((2*y[1]+i)%4==0){
			c=c+((2*y[1]+i)/4);

			y[0]=y[0]-i;
			break;
		}
	}

	if (y[0]%4==0)
	c=c+(y[0]/4);
	else
	c=c+(y[0]/4)+1;
	cout<<c<<endl;
		}

	}*/

	/*int x,q,y,c=0;
	cin>>x;
	while (x!=0){
		cin>>y>>q;
		if (y<=q-2)
			c++;
		x--;
	}

	cout<<c<<endl;*/
 /*int x,y;
 cin>>x>>y;
 if (x==1 && x==10)
	 cout<<-1<<endl;
 else{
	 if (y==10){
		 cout<<1;
		 for (int i=0;i<x-1;i++)
			 cout<<0;
	 }
	 else{
		 cout<<y;
		 for (int i=0;i<x-1;i++)
			 cout<<0;

	 }

 }
      cout<<endl; */
          /* if (s[0]>=97 && s[0]<=122)
           s[0]=s[0]-32;
           cout<<s<<endl;*/
/*int x,y;
cin>>x>>y;
if (x<y)
	cout<<-1<<endl;
else{
	if (y>=ceil(x/2.0))
		cout<<y<<endl;
	else{
		int sum=y;
		while (sum<=x/2){
			sum=sum+(y);
			if (sum==ceil(x/2.0))
				break;
		}
		cout<<sum<<endl;
	}
}*/

/*long long x,y;
cin>>x>>y;
if (y<=ceil(x/2.0))
	cout<<y*2-1<<endl;
else{
	y=y-ceil(x/2.0);
	cout<<y*2<<endl;
}*/
/*double sum=0;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
	sum=sum+((double)y[i]/100);
}

sum=(sum/x)*100;
printf("%3.12f\n",sum);*/
/*int t,a,b;
cin>>t;
while (t!=0){
	cin>>a>>b;
	cout<<(a>=b?"FunkyMonkeys\n":"WeWillEatYou\n");
	t--;
}*/
/*string t="AHIMOTUVWXY";
string s;
cin>>s;
int c=0;
for (int i=0;i<s.length();i++){
	for (int j=0;j<t.length();j++){
		if (s[i]==t[j]){
			c++;
			break;
		}
	}
}
if (c!=s.length())
	cout<<"no\n";
else{
	bool t=true;
	for (int i=s.length()-1;i>=0;i--){
		if (s[i]!=s[s.length()-i-1])
		{
			cout<<"no\n";
			t=false;
			break;
		}
	}
	if (t)
		cout<<"yes\n";
}*/
/*int x,sum=0,y=1;
cin>>x;
if (x==1)
	cout<<1<<endl;
else if (x==2)
	cout<<3<<endl;
else{
	sum=(x*(x+1))/2;
	for (int i=x-1;i>1;i--){
		sum=sum+(i-1)*y;
		y++;
	}
	cout<<sum<<endl;
}*/

/*long x,sum=0;
cin>>x;
long *y=new long [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);

for (int i=0;i<x;i++){
	sum=sum+(y[x-1]-y[i]);
}
cout<<sum<<endl;*/
/*int x;
cin>>x;
if (x%2==0){
for (int i=x;i>0;i--){
	if (i==1)
		cout<<"I love it ";
	else if (i%2==0)
		cout<<"I hate that ";
	else
		cout<<"I love that ";

}
}
else{
	for (int i=x;i>0;i--){
	if (i==1)
		cout<<"I hate it ";
	else if (i%2==0)
		cout<<"I love that ";
	else
		cout<<"I hate that ";

}
}*/
/*int x,y,t=240,u=5,sum=0,k=0;
cin>>x>>y;
t=t-y;

for (int i=1;i<=x;i++){

		sum=sum+i*u;
		if (sum<=t)
		k++;

}
cout<<k<<endl;*/
/*int x;
cin>>x;
cout<<(x==2?2:1)<<endl;*/
/*int x,y,sum=0,j,l=0;
cin>>x>>y;
j=x;
l=y;
sum=y;

if (l==x-1){
	cout<<l<<endl;
}
else if (l>=x)
	cout<<x-1<<endl;
else{
for (int i=2;i<=x;i++){
	l--;
	sum=sum+i;
	l++;
	if (x-i==l)
		break;
}
cout<<sum<<endl;
}*/
/*int u1=0,u2=0,u3=0,u4=0,u5=0,ch=0,ce=0,cl=0,cl2=0,co=0;
string s;
cin>>s;
for (int i=0;i<s.length();i++){
	if (s[i]=='h'){
		ch++;
		u1=i;
		break;
	}
}
for (int i=u1+1;i<s.length();i++){
	if (s[i]=='e'){
		u2=i;
		ce++;
		break;
	}
}
for (int i=u2+1;i<s.length();i++){
	if (s[i]=='l'){
		u3=i;
		cl++;
		break;
	}
}
for (int i=u3+1;i<s.length();i++){
	if (s[i]=='l'){
		u4=i;
		cl2++;
		break;
	}
}
for (int i=u4+1;i<s.length();i++){
	if (s[i]=='o'){

		co++;
		break;
	}
}
if (co==1 && ce==1 && cl==1 && cl2==1 && ch==1)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

/*int n,a,b;
cin>>n>>a>>b;
if (b>=0){

	if ((b+a)%n==0)
		cout<<n<<endl;
	else
		cout<<(a+b)%n<<endl;

}
else {

		if(-1*b < a)
		{
			cout<< a-b;
		}
		else
		{
			cout<< n-(((-1*b)-a)%n);
		}

}*/
/*long x,y;
cin>>x>>y;
cout<<y%((long)(pow(2.0,x)))<<endl;*/

/*long long x;
cin>>x;
if (x!=0&&x%2==0)
	cout<<x+1<<endl;
else
	cout<<x/2+1<<endl;*/

/*bool t=false;
long x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
for (int i=0;i<x;i++){
	if (t==true)
		break;
	for (int j=i+1;j<x-1;j++){
		if (y[i]+y[j]>y[j+1]){
			cout<<"YES\n";
			t=true;
			break;
		}
}
}
if (t==false)
	cout<<"NO\n";*/

/*long *y=new long[4];
for (long i=0;i<4;i++){
	cin>>y[i];
}
sort(y,y+4);
for (long i=0;i<3;i++){
	cout<<y[3]-y[i]<<" ";
}*/
/*int n,t,x,z;
cin>>n>>t;
int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
int *o;
while (t--){
	cin>>x>>z;
	o=new int [z];
	for (int i=x;i<z;i++){
		o[i]=y[i];
	}
	sort(o,o+z);
	cout<<o[0]<<endl;
}*/

/*vector <int>y;
int x,q,c=0;
cin>>x;
while (true){
    cin>>q;
    if (q==0)
        break;
	y.push_back(q);
    c++;
}

int n=0,r=c-1,b=0,o=0;

long int u=0;
while (r>n){
    if (u>abs(y[n]+y[r]-x)){
        u=abs(y[n]+y[r]-x);
        b=r;
        o=n;
    }
    if (abs(y[n]+y[r])>x)
        r--;
    else
        n++;
}

cout<<"The closest pair is "<<y[o]<<" and "<<y[b]<<endl;

 long long x,c=0,q;
    cin>>x;
    q=ceil(x/2.0);
    while (q!=0)
    {
        c++;
        q=q/2;
    }
    cout<<c<<endl;*/
/*int x,y,c=0,l;
cin>>x>>y;
l=x;

while (x<y&& y%x==0){
	x=x*l;
	c++;
}
if (x==y)
	cout<<"YES\n"<<c<<endl;
else
	cout<<"NO\n";*/
/*long x,sum=0;
cin>>x;
while (x!=0){
	if (x>=100){
		sum=sum+(x/100);
		x=x%100;
	}

	else if (x>=20){
		sum=sum+(x/20);
		x=x%20;
	}
	else if (x>=10){
		sum=sum+(x/10);
		x=x%10;
	}
	else if (x>=5){
		sum=sum+(x/5);
		x=x%5;
	}
	else{
	sum=sum+x;
	x=0;
	}
}
cout<<sum<<endl;*/
/*int x,c=1,j=0;
cin>>x;
int *y=new int [x];
int *q=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
for (int i=0;i<x;i++){
	q[i]=0;
}
for (int i=0;i<x-1;i++){
	if (y[i]<y[i+1])
		c++;
	else{
		q[j]=c;
		j++;
		c=1;
	}
}
sort(q,q+x);
if (c==x|| c>q[x-1])
	cout<<c<<endl;
else
cout<<q[x-1]<<endl;*/
/*int w,a,b,m=0,l=0,hh=0;

cin>>w>>a>>b;
string x;
cin>>x;
if (((x.length())>w*b) || ((x.length())<w*a) )
	cout<<"No solution\n";
else{
	int m = ((double)x.length() / w), d = x.length() % w;
	for (int i=1;i<=w;i++){
		if (i<w){
			for(int j=0; j<m; j++)
				cout<<x[hh++];
            cout<<endl;
		}
		else
		{
			while (hh<x.length()){
				cout<<x[hh++];
			}
            cout<<endl;

		}
      }

}
*/
/*int x;
cin>>x;
int *y=new int [x];
y[0]=1;
y[1]=1;
for (int i=2;i<x;i++){
	y[i]=y[i-1]+y[i-2];
}
cout<<y[x-1]<<endl;*/
/*int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
long q;
cin>>q;
bool t=false,l=false;
long *u=new long [q];
for (int i=0;i<q;i++){
	cin>>u[i];
}
sort(y,y+x);
long sum=0;
long beg=0,end=x-1;
for (int i=0;i<q;i++){
	cout<<upper_bound(y,y+x,u[i])-y<<endl;
}*/

/*int x,c=0,sum=0;
	vector<int>v;
	cin>>x;
	string s;
	cin>>s;
	for (int i=0;i<x;i++){
		if (s[i]=='x')
			c++;
		else{
			v.push_back(c);
			c=0;
		}
	}
	if (c!=0)
		v.push_back(c);
	for (int i=0;i<v.size();i++){
		if (v[i]>=3)
			sum=sum+(v[i]-2);
	}
	cout<<sum<<endl;*/
/*int x,y;
cin>>x>>y;
int c=1;
vector <int>b(100001);
int *u=new int [y];
int *v=new int [x];
int *p=new int [y];
for (int i=0;i<x;i++){
	cin>>v[i];
}
for (int i=0;i<y;i++){
	cin>>p[i];
}
for (int i=0;i<y;i++){
	c=1;
for (int j=p[i]-1;j<x;j++){
	if (b[v[j]]==0)
	{
		b[v[j]]=c;
		c++;
	}

}
u[i]=c-1;
	fill(b.begin(), b.end(), 0);
}
for (int i=0;i<y;i++){
	cout<<u[i]<<endl;
}
cout<<endl;*/
/*int x,y,z;
cin>>x>>y;
set<int>s1;
int *p=new int[x+1];
int *u=new int [x+1];
for (int i=1;i<=x;i++){
	cin>>u[i];
}

for (int i=x;i>=1;i--){
	s1.insert(u[i]);
	p[i]=s1.size();
}
for (int i=0;i<y;i++){
	cin>>z;
	cout<<p[z]<<endl;
}*/

/*bool o=false;
string x[5]={"S","M","L","XL","XXL"};
int y[5];

for (int i=0;i<5;i++){
	cin>>y[i];
}
int n,l;
cin>>n;
string s;
while (n--){
	o=false;
	cin>>s;

		for (int i=0;i<5;i++)
		{
			if (s==x[i]){
				l=i;
				if (y[l]>0){
					cout<<x[l]<<endl;
					y[l]--;
					o=true;
				}
				else if (l!=0&&l!=4 &&y[l]==0 &&y[l+1]==0&&y[l-1]>0){
					cout<<x[l-1]<<endl;
					y[l-1]--;
					o=true;
				}
				else{
				for (int j=l;j<5;j++)
				{
					if (y[j]>0){
						cout<<x[j]<<endl;
						y[j]--;
						o=true;
						break;
					}
				}
				}

		if (o==false){
			for (int j=l;j>=0;j--)
				{
					if (y[j]>0){
						y[j]--;
						cout<<x[j]<<endl;

						break;
					}
				}
		}
			}
		}

}*/
/*int c=0,sum=0;
long x;

scanf("%d",&x);
long *y=new long [x];
for (int i=0;i<x;i++){
	scanf("%d",&y[i]);
}
sort(y,y+x);
for (int i=0;i<x;i++){
	if (sum<=y[i])
		sum=sum+y[i];
	else
		c++;
}
printf("%d\n",x-c);*/
/*int x,y,max=1,c=1;
scanf("%d",&x);
scanf("%d",&y);
int *q=new int [x];
for (int i=0;i<x;i++){
	scanf("%d",&q[i]);
}
for (int i=0;i<x-1;i++){
	if (q[i]!=q[i+1])
		c++;
	else{
		if (max<c)
			max=c;
		c=1;
	}
}
if (c>max)
	cout<<c<<endl;
else
	cout<<max<<endl;*/

/*int x,max=0,j=0;
cin>>x;
int *y=new int [x];
int q[5]={0,0,0,0,0};
for (int i=0;i<x;i++){
	cin>>y[i];
	q[y[i]]++;
}
for (int i=0;i<5;i++){
	if (max<q[i]){
		max=q[i];
		j=i+1;
	}
}
cout<<j<<endl;*/
/*string  numbers[] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
        "twenty",
        "twenty one",
        "twenty two",
        "twenty three",
        "twenty four",
        "twenty five",
        "twenty six",
        "twenty seven",
        "twenty eight",
        "twenty nine"
    };
int h,m;
cin>>h>>m;
if (m==0)
	cout<<numbers[h]<<" o' clock\n";
else if (m==15)
	cout<<"quarter past "<<numbers[h]<<endl;
else if (m<30)
	cout<<numbers[m]<<(m==1?" minute past ":" minutes past ")<<numbers[h]<<endl;
else if (m==30)
	cout<<"It's half past "<<numbers[h]<<endl;
else if (m==45)
	cout<<"It's quarter to "<<numbers[h]<<endl;
else
	cout<<"It's "<<numbers[60-m]<<" to "<<numbers[h]<<endl;*/
/*bool cab=false,cba=false,k=false;
string s;
cin>>s;
for (int i=0;i<s.length()-1;i++){

	if (s[i]=='A' && s[i+1]=='B'){
		cab=true;
		if (i<s.length()-2)
		i=i+2;
		k=true;
	}

	if (k==true&&s[i]=='B' && s[i+1]=='A'){
		cba=true;
		i=i+2;
}

}
	if (cab ==true && cba ==true){
		cout<<"YES\n";

	}
	else{
		cba=false;
		cab=false;
		k=false;
		for (int i=0;i<s.length()-1;i++){

	if (s[i]=='B' && s[i+1]=='A'){
		cba=true;
		if (i<s.length()-2)
		i=i+2;
		k=true;
	}

	if (k==true&&s[i]=='A' && s[i+1]=='B'){
		cab=true;
		i=i+2;
}

}

	if (cab ==true && cba ==true)
		cout<<"YES\n";
	else{
		cout<<"NO\n";

	}
	}*/
/*int be=0,en=10;
int x,z;
bool t=false;

int y[11]={0,0,0,0,0,0,0,0,0,0,0};
for (int i=0;i<6;i++){
	cin>>x;
	y[x]++;
}

for (int i=0;i<11;i++){

if (y[i]>=4){
	z=i;
	t=true;
}
}

if (t==true)
{
	for (int i=0;i<11;i++){
		if (y[i]==2 ||y[i]==6){
		cout<<"Elephant\n";
		be=1;
		break;
		}

	}
	if (be==0)
		cout<<"Bear\n";
}
else
	cout<<"Alien\n";*/

/*long long x,y=2520,c=0;
cin>>x;
c=x/y;
cout<<c<<endl;*/
/*long sum=0;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
for (int i=x-1;i>=0;i--){
	sum=sum+(pow(2.0,x-1-i)*y[i]);
}
cout<<sum<<endl;*/
/*int sum=0;
int x,k;
cin>>x>>k;
vector<pair<int ,int >>v(x);
for (int i=0;i<x;i++){
	cin>>v[i].first;
	cin>>v[i].second;
}
sort(v.begin(),v.end());
/*for (int i=0;i<x;i++){
	cout<<v[i].first<<"        "<<v[i].second<<endl;
}*/

/*for (int i=x-1;i>=0;i--){
	if (v[i].second==0)
		sum=sum+v[i].first;
	else{
		if (k>0){
		k--;
		sum=sum+v[i].first;
		}
		else{
			sum=sum-v[i].first;
		}

	}
}
cout<<sum<<endl;*/

/*int x;
cin>>x;
string *y=new string [x];

set <string> s;
for (int i=0;i<x;i++)
	cin>>y[i];
cout<<endl;
for (int i=x-1;i>=0;i--){
	if (s.count(y[i])==0)
	cout<<y[i]<<endl;
	s.insert(y[i]);
}*/

/*bool b=false;
int x;
vector <int>v;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}

for (int i=0;i<x-1;i++){
	for (int j=i+1;j<x;j++){
		if (y[i]==y[j]){
			b=true;
			v.push_back(j-i);
		}
	}
}
if (b==true){
sort(v.begin(),v.end());
cout<<v.at(0)<<endl;
}
else
	cout<<-1<<endl;*/

/*int x,k,c=0;
cin>>x;
int *y=new int [x];
vector <int >v;
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
while (y[x-1]!=0){
	c=0;
	for (int i=0;i<x;i++){
		if (y[i]>0){
			k=y[i];
			break;
		}
	}
	for (int i=0;i<x;i++){
		y[i]=y[i]-k;
	}
	for (int i=0;i<x;i++){
		if (y[i]>0)
			c++;
	}
	v.push_back(c);
}
cout<<x<<endl;
for (auto i=v.begin();i!=v.end()-1;i++){
	cout<<*i<<endl;
}*/

/*
int x;
cin>>x;

int** a = new int*[x];
for(int i = 0; i < x; ++i)
    a[i] = new int[x];

for (int i=0;i<x;i++){
	for (int j=0;j<x;j++){
		scanf("%1d", &a[i][j]);
	}
}
for (int i=0;i<x;i++){
	for (int j=0;j<x;j++){
		if ((i>=1)&&(i<x-1) && (j>=1)&&(j<x-1)){
		if ( a[i][j] > a[i-1][j] &&
             a[i][j] > a[i+1][j] &&
             a[i][j] > a[i][j-1] &&
             a[i][j] > a[i][j+1])
              printf("X");
         else
            printf("%d",a[i][j]);
		}
		else
			 printf("%d",a[i][j]);
	}

	printf("\n");
}
*/

/*int n=2;
int x,y;
cin>>x>>y;
if (x<y && x!=y && y%2!=0)
	cout<<"No\n";
else if ((x==y && y%2!=0))
	cout<<"Yes\n";
else if (x>y && x%y==0)
	cout<<"Yes\n";
else{
	for (int i=1;;i++)
	{
		n=pow(2.0,i);
		if (n==y){
			cout<<"Yes\n";
			break;
		}
		else if (n>y){
			if ( y==x || y==(x*2) ||(x==3 && y%x==0)){
				cout<<"Yes\n";
				break;
			}
			else{
				cout<<"No\n";
				break;
			}
		}
	}
}*/

/*int x,y,z;
cin>>x>>y>>z;
if (x>y+z)
	cout<<"+"<<endl;
else if (y>x+z)
	cout<<"-"<<endl;
else if (y==x+z && x==y+z )
	cout<<"0"<<endl;
else
	cout<<"?"<<endl;*/

/*bool v=false;
string suit="DCSH";
string rank="23456789TJQKA";
string t,h;
cin>>t;
for (int i=0;i<5;i++){
	cin>>h;
	if (h[0]==t[0] ||h[1]==t[1])
		v=true;
}
if (v)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

/*string s,s1,s2;
cin>>s;
for (int i=0;i<s.length();i++){
	s1=s1+s[i];
	if (s[i]!='a')
		s2=s2+s[i];
	if (s1.length()+s2.length()==s.length())
		break;
}
if (s1+s2==s)
	cout<<s1<<endl;
else
	cout<<":(\n";*/

/*long x,k;
cin>>x>>k;
while (k--){
	if (x%10>0)
		x--;
	else
		x/=10;
}
cout<<x<<endl;*/

/*bool t=false;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
	if (y[i]==1)
		t=true;
}
if (t)
	cout<<"HARD\n";
else
	cout<<"EASY\n";*/

/*int sum=0;
int x;
scanf("%d",&x);
string y;
while (x--){
	cin>>y;
	if (y=="Icosahedron")
		sum=sum+20;
	else if (y=="Cube")
		sum=sum+6;
	else if (y=="Tetrahedron")
		sum=sum+4;
	else if (y=="Dodecahedron")
		sum=sum+12;
	else
		sum=sum+8;
}
printf("%d\n",sum);*/

/*int c=0;
int x;
cin>>x;
for (int i=1;i<x;i++){
	if (x%i==0)
		c++;
}
cout<<c<<endl;*/

/*
long long n,x,y,sum=0;
cin>>n;

while (n--){
cin>>x;
cin>>y;
	sum=0;
while (true){
	if (x==0)
		break;
	if (x%y==0){
		sum++;
		x=x/y;
	}
	else{
		sum=sum+x%y;
		x=x-x%y;
	}
}
cout<<sum<<endl;
}
*/

/*int x;
cin>>x;
if (x%2==0)
	cout<<"Mahmoud\n";
else
	cout<<"Ehab\n";*/

/*int san=0,sea=0;
int x;
cin>>x;
string n;
cin>>n;
for (int i=0;i<n.length()-1;i++){
	if (n[i]=='S' && n[i+1]=='F')
		san++;
	else if (n[i]=='F' && n[i+1]=='S')
		sea++;
}
if (san!=0&&san>sea)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

   /*long double x,y,z,c=0;
    cin>>x>>y;
    z=(int)ceil(y/x);
    cout<<fixed<<setprecision(0)<<z<<endl;*/

/*bool t=true;
  long long n,k,sum=0,c=1,h=0;
  cin>>n>>k;
  sum=k;
  if (n==1)
      cout<<0<<endl;
  else if ((2+k)*(k-1)/2-(k-2)<n)

        cout<<"-1"<<endl;
  else
  {
	  k--;
	  n--;
     long long r=k;
	 long long l=0;
	long  long mid=(r+l)/2;
	  while (l<r){
		  mid=(r+l)/2;
		  sum=((((k*(k+1))/2))-(((mid*(mid+1))/2)));
		  if (n<sum)
			  l=mid+1;
		  else if (n>sum)
			  r=mid;
		  else
		  {
			  t=false;
			  cout<<k-mid<<endl;
			  break;
		  }
	  }
	  if (t&&sum>n){
		  cout<<k-mid<<endl;

	  }
	  else if (t && sum<n){
		  cout<<k-mid+1<<endl;

	  }

  }
  */

/*int x;
cin>>x;
int** a = new int*[x];
for(int i = 0; i < x; i++)
    a[i] = new int[x];
for(int i = 0; i< x;i++){
	for(int j = 0; j< x;j++){
		if (i==0 ||j==0)
			a[i][j]=1;
		else
			a[i][j]=a[i-1][j]+a[i][j-1];
	}
}
cout<<a[x-1][x-1]<<endl;*/

/*int x,t,z,cm=0,cc=0;
cin>>x;
while (x--){
	cin>>t>>z;
	if (t>z)
		cm++;
	else if (t<z)
		cc++;
}
if (cm>cc)
	cout<<"Mishka\n";
else if (cc>cm)
	cout<<"Chris\n";
else
	cout<<"Friendship is magic!^^\n";*/

/*
int x[4],sum=0;
for (int i=0;i<4;i++){
	cin>>x[i];
}
string s;
cin>>s;
for (int i=0;i<s.length();i++){
	sum=sum+x[s[i]-'1'];
}
cout<<sum<<endl;*/

/*long long a,b,k;
int x;
cin>>x;
while (x--){
cin>>a>>b>>k;

	cout<<fixed<<setprecision(0)<<(a*(k/2+k%2))-(b*(k/2))<<endl;
}*/
/*
int x;
cin>>x;
long long y,sum=0;
while (x--){
	sum=0;
	cin>>y;
	while (y>1){
		if (y%2==0){
			y=y/2;
			sum++;
		}
		else if (y%3==0){
			y=y/3*2;
			sum++;
		}
		else if (y%5==0)
		{
			y=y/5*4;
			sum++;
		}
		else
			break;
	}

	if (y==1)
		cout<<sum<<endl;
	else
		cout<<-1<<endl;
}*/

/*
int sum1=0,sum2=0;
int s,v1,v2,t1,t2;
cin>>s>>v1>>v2>>t1>>t2;
sum1=2*t1+(v1*s);
sum2=2*t2+(v2*s);
if (sum1<sum2)
	cout<<"First\n";
else if (sum1>sum2)
	cout<<"Second\n";
else
	cout<<"Friendship\n";*/

/*bool t=false;
int x;
cin>>x;
int*y=new int [x];
int q[10];
for (int i=0;i<x;i++){
	cin>>y[i];
}
for (int i=0;i<10;i++){

	cin>>q[i];
}
sort(y,y+x);

for (int i=0;i<10;i++){
	if (y[0]>=q[i])
		t=true;
}
if (t)
	cout<<10<<endl;
else
	cout<<y[0]<<endl*/

/*
int x,sum=0,l=0;
cin>>x;
for (int i=1;;i++){
	sum=sum+(i*(i+1))/2;
	if (sum==x){

		cout<<i<<endl;
		break;
	}
	else if (sum>x){

		cout<<i-1<<endl;
		break;
	}
}*/

/*
long long x,y,z,c=0;
cin>>x>>y>>z;
if (x+y<z)
	cout<<0<<" "<<0<<endl;
else if (x%z==0 || y%z==0||(x%z+y%z)<z)
	cout<<((x+y)/z)<<" "<<0<<endl;
else  {
	cout<<((x+y)/z)<<" "<<min(z-(y%z),z-(x%z))<<endl;
}*/

/*
int x,t1,t2,p,sum=0;
cin>>x>>t1>>t2;
t2=2*t2;
for (int i=0;i<x;i++){
	cin>>p;
	if (p==2)
	{
		if (t2%2==0)
		t2=t2-p;
		else
			sum=sum+p;
	}
	if (p==1){
		if (t1>0)
			t1--;
		else if (t2>0)
			t2--;
		else
			sum++;
	}
}
cout<<sum<<endl;*/

/*
bool p2=true;
bool p=true;
int k=2;
int x,i,l=0;
cin>>x;
if ((x*1+1)%2==0&& x!=1)
	cout<<1<<endl;
else{
while (true){
	p2=true;
	p=true;
for ( i=2;i<k;i++){
	if (k%i==0)
		p=false;
}
if (i==k)
	p=true;
if (p){
	l=x*k+1;
	for (int i=2;i<l;i++){
		if (l%i==0){
			cout<<k<<endl;
			p2=false;
			break;
		}
	}
	}
if (!p2)
	break;
	else
		k++;
}
}*/

/*
int n,p,k,pl,pr,pr2,pl2;
cin>>n>>p>>k;
pl=p-k;
pl2=pl;
pr=p+1;
pr2=pr-1;

	if (pl>1){
		cout<<"<< ";

	while (pl<p){
		cout<<pl<<" ";
		pl++;
	}
	cout<<"("<<p<<") ";
	if (pr2+k<n){
		while (pr<=pr2+k){
			cout<<pr<<" ";
			pr++;
		}
		cout<<">>";
	}
	else {
		while (pr<=n){
			cout<<pr<<" ";
			pr++;
		}
	}
	cout<<endl;
	}
	else{
		pl=1;
		pl2=1;
		while (pl<p)
		{
			cout<<pl<<" ";
			pl++;
		}
		cout<<"("<<p<<") ";
		if (pr2+k<n){
		while (pr<=pr2+k){
			cout<<pr<<" ";
			pr++;
		}
		cout<<">>";
	}
		else{

		while (pr<=n){
			cout<<pr<<" ";
			pr++;
		}
	}
	cout<<endl;
	}*/

/*
bool b=true;
int x,l;
cin>>x>>l;
int *y=new int [x];
for (int i=0;i<x;i++)
	cin>>y[i];
for (int i=1;i<x;i++)
{
	if (y[i-1]>=y[i]){
		cout<<i+1<<endl;
		b=false;
		break;
	}
}
if (b)
	cout<<0<<endl;*/

/*Books

int sum=0,c=0,i,cf=0;
int r,l,mid=0;
int n,t;
cin>>n>>t;
l=0;
r=n-1;

int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
while (l<r){
	sum=0;
	mid=(l+r)/2;
	for (i=mid;i<n;i++){
		sum=sum+y[i];
	}
	c=n-mid;
	if (sum>t){
		l++;
	}
	else{
		r--;
		if (c>cf)
			cf=c;
	}
}

if (cf==0 &&t>=y[0])
	cout<<1<<endl;
else
	cout<<cf<<endl;*/

int a,b,c,d,sum=0;
int y[3];
cin>>y[0]>>y[1]>>y[2];
cin>>d;
sort(y,y+3);
cout<<max(0,d-(y[2]-y[1]))+max(0,d-(y[1]-y[0]))<<endl;

/*
int x,m,sum=0;
cin>>x>>m;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}

if (y[0]<=m)
cout<<"0 ";
else
cout<<"1 ";
for (int i=1;i<x;i++){
	sum=0;
	int *q=new int [i];
	for (int j=0;j<i;j++){
		sum=sum+y[j];
		q[j]=y[j];
	}
	sort(q,q+i);

	if (sum+y[i]<=m)
		cout<<0<<" ";
	else{
		int p=0,p1=0;
		int sum2=sum+y[i];

		for (int l=i-1;l>=0;l--){
			sum2=sum2-q[l];
			if (sum2<=m){
				cout<<i-l<<" ";
				break;
			}
		}

	}

}*/

    return 0;
}

#include<stdio.h>
int main()
{
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	int temp;
	int i,sum=0;
	if(a>b)
	{
		temp=a;;
	    a=b;
		b=temp;
	}
	if(a>c)
	{
		temp=a;;
		a=c;
		c=temp;
	}
	if(b>c)
	{
		temp=b;;
		b=c;
		c=temp;
	}

     if(c-a>=2*d)
	{
		if(b-a>=d&&c-b>=d)
			sum=0;
		if(b-a<d)
		{
			for(i=1;b+i-a<d;i++);
			sum=i;
		}
		if(c-b<d)
		{
			for(i=1;c+i-b<d;i++);
		    sum=i;
		}
		printf("%d\n",sum);
	}
	else
	{
		if(b-a>=c-b)
		{
			for(i=1;c+i-b<d;i++);
				sum+=i;
			for(i=0;b+i-a<d;i++);
			    sum+=i;
			printf("%d\n",sum);
		}
		else if(b-a<c-b)
		{
			for(i=1;b+i-a<d;i++);
			    sum+=i;
			for(i=0;c+i-b<d;i++);
				sum+=i;
			printf("%d\n",sum);
		}
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

int a[3];
int k;
int main()
{
    cin >>a[0] >> a[1] >> a[2]>>k;
    sort(a,a+3);
    cout << max(k-a[1]+a[0],0)+max(k-a[2]+a[1],0);
}

 #include <bits/stdc++.h>
using namespace std;
int main()
{
long long a,b,c,d,pb,pm,k=0;
cin>>a>>b>>c>>d;
if(a>b) swap(a,b);
if(b>c) swap(b,c);
if(a>b) swap(a,b);
pb=b+d;
pm=b-d;
if(a>pm)
{if(pm<0) {pm*=-1;k+=a+pm;}
else k+=a-pm;}
if(c<pb)
k+=pb-c;
cout<<k;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int A[3],x=0,d,y,n=0;
    cin>>A[0]>>A[1]>>A[2]>>d;
    sort(A,A+3);
    x=d-(A[1]-A[0]);
    y=d-(A[2]-A[1]);
    //cout<<x<<y<<endl;
    if(x>0)
        n+=x;
    if(y>0)
        n+=y;
    //printf("%lld %lld %lld\n",A[0],A[1],A[2]);
    printf("%lld\n",n);
    return 0;
}

#include <bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long ll;
ll a, b, c, d;

ll Abs(ll x) {
	if(x < 0) return -x; return x;
}
ll Min(ll x, ll y) {
	return x < y ? x : y;
}
int main() {
	scanf("%I64d%I64d%I64d%I64d", &a, &b, &c, &d);
	if(a > b) swap(a, b); if(b > c) swap(b, c); if(a > b) swap(a, b);
	ll ans1 = 0;
	if(b - a >= d) ans1 = (c - b < d) * Abs(c - b - d);
	else ans1 = Abs(b - a - d) + (c - a - d < d) * Abs(c - a - d - d);
	ll ans2 = (b - a < d) * Abs(b - a - d) + (c - b < d) * Abs(c - b - d);
	ll ans3 = 0;
	if(c - b >= d) ans3 = (b - a < d) * Abs(b - a - d);
	else ans3 = Abs(c - b - d) + (c - d - a < d) * Abs(c - d - a - d);
	printf("%I64d\n", Min(ans1, Min(ans2, ans3)));
	return 0;
}

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
	long long int a,b,c,d;
	cin>>a>>b>>c>>d;
	int v[3]={abs(a-b),abs(a-c),abs(b-c)};
	long long int res=0;
	sort(v,v+3);
	if(v[0]<d){
		res+=d-v[0];
	}
	if(v[1]<d){
		res+=d-v[1];
	}
	cout<<res;

}

#include<bits/stdc++.h>

using namespace std;

int main()
{
    long long int a[6],d,ans=0,i;

    for(i=0;i<3;i++)
    {
        cin>>a[i];
    }

    cin>>d;

    sort(a,a+3);

    if(abs(a[0]-a[1])<d)
    {
        ans+=d-abs(a[0]-a[1]);
    }
    if(abs(a[1]-a[2])<d)
    {
        ans+=d-abs(a[1]-a[2]);
    }

    cout<<ans<<endl;

    return 0;
}

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
int main()
{
	LL a,b,c,d;
	cin>>a>>b>>c>>d;
	int t;
	if(a>b)
	{
		t=a;
		a=b;
		b=t;
	}
	if(b>c)
	{
		t=b;
		b=c;
		c=t;
	}

	if(a>b)
	{
		t=a;
		a=b;
		b=t;
	}
	int i,j,k;
	i=b-a;
	j=c-b;
	if(i<d)
	{
		k=d-i;
	}
	if(j<d)
	{
		k=k+d-j;
	}
	cout<<k<<endl;
}

#include <iostream>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <map>
#include <string>
#include <set>
using namespace std;

int main()
{
	int a, b, c, d, d1, d2, res;
	cin >> a >> b >> c >> d;

	d1 = c - b;
	d2 = c - a;
	res = 0;

	if ((d1 <= 0 && d2 >= 0) || (d1 >= 0 && d2 <= 0)) {
		d1 = abs(d1);
		d2 = abs(d2);
	}
	else {
		d1 = abs(d1);
		d2 = abs(d2);
		if (d1 < d2)
			d2 = d2 - d1;
		else
			d1 = d1 - d2;
	}
	if (d1 < d)
		res += d - d1;
	if (d2 < d)
		res += d - d2;
	cout << res;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int arr[3],d;
    cin>>arr[0]>>arr[1]>>arr[2]>>d;
    sort(arr,arr+3);
    cout<<max(0,d-(arr[1]-arr[0]))+max(0,d-(arr[2]-arr[1]));
}

#include <bits/stdc++.h>
using namespace std;

int main() {
	int p[3], d;
	cin >> p[0] >> p[1] >> p[2] >> d;
	sort(p, p + 3);
	cout << max(0, d + p[0] - p[1]) + max(0, d + p[1] - p[2]) << endl;
}

#include<bits/stdc++.h>
using namespace std;

int main(){
    //ios_base::syc_with_stdio(false);
    //cin.tie(NULL);
    int a,b,c,d; cin>>a>>b>>c>>d;
    int ar[3];
    ar[0] = a; ar[1]= b; ar[2] = c;
    sort(ar,ar+3);
    int count = 0;
    if((ar[1]-ar[0])<d)
     count+= d-(ar[1]-ar[0]);
    if((ar[2]-ar[1])<d)
     count+= d-(ar[2]-ar[1]);

    cout<<count;
     return 0;

}

#include <iostream>
using namespace std;
int main() {
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	int arr[3] = { a,b,c };
	int x;
	int i, j;
	for (i = 0; i < 3 - 1; i++)
		for (j = 0; j < 3 - i - 1; j++)
			if (arr[j] > arr[j + 1]) {
				x = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = x;
			}
	if (arr[1] - arr[0] >= d) {
		if (arr[2] - arr[1] >= d) cout << 0;
		else cout << arr[1] + d - arr[2];
	}
	else {
		i =abs(arr[0] + d -arr[1]);
		if (arr[2] - arr[1] >= d) cout << i;
		else cout <<i+ arr[1] + d - arr[2];
	}
	return 0;

}

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    int a, b, c;
    cin >> a >> b >> c;
    int d;
    cin>>d;
    int m1 = max(a, max(b, c));
    int m3 = min(a, min(b, c));
    int m2 = a + b + c - m1 - m3;
    int r1 = m2-m3;
    int r2 = m1-m2;
    cout<<max(d-r1,0)+max(d-r2,0);
}

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<list>
#include<deque>
#include<algorithm>
using namespace std;
int main()
{
    long long a[3],d,ans=0;
    cin>>a[0]>>a[1]>>a[2]>>d;
    sort(a,a+3);
    if(a[1]-a[0]<d)ans+=d-(a[1]-a[0]);
    if(a[2]-a[1]<d)ans+=d-(a[2]-a[1]);
    cout<<ans;
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define Angola ios_base::sync_with_stdio(0),cin.tie(),cout.tie()
#define Begin int main()
Begin {
Angola;
ll a,b,c,d;
cin>>a>>b>>c>>d;
vector <ll> q;
q.push_back(a);
q.push_back(b);
q.push_back(c);
sort(q.begin(),q.end());
a = q[0];
b = q[1];
c = q[2];
ll ans = 0;
if (abs(a-b)<d) {ans+=abs(d-abs(a-b));a+=d-abs(a-b);}
if (abs(b-c)<d) {ans+=abs(d-abs(b-c));b +=d-abs(b-c);}
if (abs(a-c)<d) {ans+=abs(d-abs(a-c));c+=d-abs(a-c);}
sort(q.rbegin(),q.rend());
ll ans1 = 0;
a = q[0];
b = q[1];
c = q[2];
//ll ans = 0;
if (abs(a-b)<d) {ans1+=abs(d-abs(a-b));a+=d-abs(a-b);}
if (abs(b-c)<d) {ans1+=abs(d-abs(b-c));b+=d-abs(b-c);}
if (abs(a-c)<d) {ans1+=abs(d-abs(a-c));c+=d-abs(a-c);}
cout<<max(0LL,min(ans,ans1));return 0;
}
/*Монакина Алёна
Вязьма, Российская Федерация
Вяземский железнодорожный техникум
Задача A
*/

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    ll x[4];
    x[0]=a;
    x[1]=b;
    x[2]=c;
    sort(x,x+3);
    ll e=0,f=0,g=0;
    if(x[1]-x[0]<d){
    e=x[1]-d;
    g+=abs(e-x[0]);
    }
    if(x[2]-x[1]<d){
    f=x[1]+d;
    g+=abs(f-x[2]);
    }
    cout<<g;

}

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define F first
#define S second
#define KOD ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define sz size()
#define S second
#define F first
#define mkp	 make_pair
#define FIX(x) fixed << setprecision(x)
#define SORT(x) sort(x.begin(),x.end())
#define Rev(a) reverse((a).begin() , (a).end());
#define pb push_back
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef map<int,int> wi;
typedef set<int> si;
main(){
	int a[3]   , d;
	cin >> a[0] >> a[1] >> a[2]>>d;
	sort(a,a+3);
	cout << max((int)0 , d - (a[1]- a[0])) + max((int)0 , d - (a[2]- a[1]) );
}

//============================================================================
// Name        : First.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main() {
	//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
   long long int a,b,c,d,min,max,mid,flag,sec=0;
   cin>>a>>b>>c>>d;

   if(a>=b)
   {
	   if(a>=c)
	   {
		   max=a;
		   flag=c;
	   }
	   else{
		  max=c;
		  flag=a;
	   }
	   if(b>=flag){
		  min=flag;
		  mid=b;
	   }
	   else{
		   min=b;
		   mid=flag;
	   }
   }

   if(b>a)
      {
   	   if(b>=c)
   	   {
   		   max=b;
   		   flag=c;
   	   }
   	   else{
   		  max=c;
   		  flag=b;
   	   }

   	   if(a>=flag){
   		  min=flag;
   		 mid=a;
   	   }
   	   else{
   		   min=a;
   		 mid=flag;
   	   }
      }
     // int sec=0;
    // cout<<min<<mid<<max<<endl;
     if(abs(mid-min)<d)
     {
    	 sec=d-(mid-min);
     }
     if(abs(max-mid)<d)
         {
        	 sec=sec + (d-(max-mid));
         }
     cout<<sec<<endl;
	return 0;
}

#include <cstdio>
#include <algorithm>

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;
}

//By      :Prajwal Sonawane
//Email   :prajwalsonawane35@gmail.com
//Language:C++
#include <bits/stdc++.h>
#define ll long long
#define deb(x) cout << #x << "=" << x;
#define test unsigned int t;cin >> t;while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;

int main(void)
{
    fastio
    int a[3],d,ans = 0;
    cin>>a[0]>>a[1]>>a[2]>>d;

    sort(a,a+3);
    if(a[1] - a[0] < d)
        ans += a[0]-a[1]+d;
    if(a[2] - a[1] < d)
        ans += a[1]+d-a[2];
    cout << ans;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[5500],sum,nm=0;
int main()
{
	//freopen("1.in","r",stdin);
	for(int i=1;i<=4;++i) scanf("%lld",&a[i]);
	sort(a+1,a+4);
	sum=max(nm,a[4]-(a[2]-a[1]))+max(nm,a[4]-(a[3]-a[2]));
	printf("%lld",sum);
	return 0;
}

#include<cstdio>
#include<algorithm>
using namespace std;
int t1,t2,a[5],ans=0;
int main()
{
	for(int i=1;i<=4;i++)
	  scanf("%d",&a[i]);
	sort(a+1,a+4);
	t1=a[2]-a[1];t2=a[3]-a[2];
	if(t1<a[4]) ans+=a[4]-t1;
	if(t2<a[4]) ans+=a[4]-t2;
	printf("%d",ans);
	return 0;
}

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<set>
using namespace std;
int a,b,c,d,ans=0;
vector<int> s;
int main()
{
	cin>>a>>b>>c>>d;
	s.push_back(a);
	s.push_back(b);
	s.push_back(c);
	sort(s.begin(),s.end());
	if(s[2]-s[0]>=2*d&&s[1]-s[0]<d)
		ans=d-s[1]+s[0];
	else if(s[2]-s[0]>=2*d&&s[1]-s[0]>=d)
	{
		if(s[2]-s[1]<d)
			ans=d-s[2]+s[1];
	}
	else if(s[2]-s[0]<d)
		ans=2*d-s[2]+s[0];
	else if(s[1]-s[0]>=d)
		ans=d-s[2]+s[1];
	else if(s[2]-s[1]>=d)
		ans=d-s[1]+s[0];
	else
		ans=ans=2*d-s[2]+s[0];
	cout<<ans;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

void boostear()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
}

void files()
{
    freopen("input.txt", "r", stdin); // redirects standard input
    freopen("output.txt", "w", stdout); // redirects standard output
}

int main()
{
    boostear();

    long long a, b, c, d;
    cin >> a >> b >> c >> d;

    long long aux=0, res=0;

    aux=min(a, b);
    b=max(a, b);
    a=aux;
    aux=min(a, c);
    c=max(a, c);
    a=aux;
    aux=min(b, c);
    c=max(b, c);
    b=aux;

    if(abs(a-b) < d)
    {
        res+= (d-abs(a-b));
        a-= (d-abs(a-b));
        /*cout << abs(a-b) << " abs(a-b) " << a << " " << b <<  " " << c << endl;*/
    }
    if(abs(b-c) < d)
    {
        res+= (d-abs(b-c));
        c+= (d-abs(b-c));
        /*cout << abs(b-c) << " abs(b-c) " << a << " " << b <<  " " << c << endl;*/
    }

    cout << res;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int arr[3],d;
    cin>>arr[0]>>arr[1]>>arr[2]>>d;
    sort(arr,arr+3);
    cout<<max(0,d-(arr[1]-arr[0]))+max(0,d-(arr[2]-arr[1]));
}

#include<bits/stdc++.h>
using namespace std;

///memset(ar,0,sizeof ar);
typedef long long int ll;
#define mk(x,y) make_pair(x,y)

#define FOR(n) for(int i=0;i<n;i++)

int to(int x,int y)
{
    return (x*60)+(y);
}

ll gcd(ll a,ll b){return b==0 ? a:gcd(b,a%b);}

int main (){
ios_base::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);

ll arr[3],arr2[3],d,ans=0 ,ans2=0;

for(int i=0;i<3;i++)cin>>arr[i],arr2[i]=arr[i];
cin>>d;
sort(arr,arr+3);

if(arr[1] - arr[0] < d)ans += d - (arr[1] - arr[0]);
if(arr[2] - arr[1] < d)ans += d - (arr[2] - arr[1]);
cout<<ans;

cout<<endl;
//end code please accepted
 return 0; }

#include<algorithm>
#include<iostream>
#include<cstdio>
#define ll long long
using namespace std;
const int N=10;
ll a[N];
ll d,ans;
int main()
{
	for (int i=1;i<=3;i++)
	cin>>a[i];
	cin>>d;
	sort(a+1,a+3+1);
	if (a[2]-a[1]<d) ans+=d-a[2]+a[1];
	if (a[3]-a[2]<d) ans+=d-a[3]+a[2];
	cout<<ans;
	return 0;
}

// luogu-judger-enable-o2
#include <cstdio>
#include <algorithm>
#define ll long long
using namespace std;
ll a[10];

int main(){
	for(ll i=1;i<=3;++i) scanf("%d",a+i); sort(a+1,a+1+3);
	ll d; scanf("%lld",&d);
	ll ans=9999999999;
	ans=min( ans, max( a[1]+d-a[2],0LL ) + max( 0LL,(max(a[1]+d,a[2])+d-a[3]) ) );
	ans=min( ans, max( a[1]-(a[2]-d),0LL ) + max( 0LL, a[2]+d-a[3] ) );
	ans=min( ans, max( a[2]-(a[3]-d),0LL ) + max( 0LL,(a[1]-( min(a[3]-d,a[2])-d  )) ) );
	printf("%lld\n",ans);
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,x,y=0,i,j,n=0,m,k=0,z;
    vector<int>v;
    cin>>a>>b>>c>>d;
    v.push_back(a);
    v.push_back(b);
    v.push_back(c);
      sort(v.begin(),v.end());

      x=abs(v[0]-v[1]);
      if(x<d)
      {
          n+=d-x;
      }
      y=abs(v[2]-v[1]);
      if(y<d)
      {
          k+=d-y;
      }
      cout<<n+k<<endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main()
{
     int a[3],d;
     cin>>a[0]>>a[1]>>a[2]>>d;

     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];
     cout<<ans;

    return 0;
}

// CodeforcesGameSolution.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int t = 1;
	//cin >> t;
	while (t--)
	{
		int a[4];
		for (int i = 0; i < 4; i++)
		{
			cin >> a[i];
		}
		sort(a, a + 3);
		cout << max(0, a[3] - (a[1] - a[0])) + max(0, a[3] - (a[2] - a[1])) << endl;
	}
}

#include<iostream>
#include<cstdio>

using namespace std;

typedef long long ll;
ll a,b,c,d;
ll ans=0x7f7f7f;
int main(){
    scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
    ll t;
    if(a>b) {
        t=a;a=b;b=t;
    }
    if(a>c){
        t=a;a=c;c=t;
    }
    if(b>c){
        t=b;b=c;c=t;
    }
    if(b-a>=d){
        if(c-b>=d){
            ans=0;
        }else{
            ans=b+d-c;
        }
    }else{
        if(c-b>=d){
            ans=a-(b-d);
          //  cout << 2<< endl;
        }else{
            ll la=2*a+3*d-b-c;
            ll lb=a+2*d-c;
            ll lc=a+b+3*d-2*c;
          //  cout << la << " "<<lb<<" " << lc << endl;
            ans=min(la,lb);
            ans=min(ans,lc);
         //   cout << 3<< endl;
        }
    }
    printf("%lld\n",ans);
    return 0;
}

/*********************************************************************************\
*  _________  _      _  ________  _       _       __       ________               *
* |___   ___|| |    | ||  ______|| |     | |     /  \     |  ____  |              *
*     | |    | |    | || |______ | |_____| |    / /\ \    | |____| |              *
*     | |    | |    | ||______  ||  _____  |   / /__\ \   |  __  __|              *
*     | |    | |____| | ______| || |     | |  / ______ \  | |  \ \                *
*     |_|    |________||________||_|     |_| /_/      \_\ |_|   \_\               *
*                                                                                *
*   Department of Computer Science & Engineering                                  *
*   Student ID : 18CSE035                                                         *
*   Bangabnadhu Sheikh Mujibur Rahman Science & Technology University,Gopalgonj.  *
*                                                                                *
\*********************************************************************************/

//Now Write to Code ___________________________

#include <bits/stdc++.h>
using namespace std ;

typedef    long long             ll;
typedef    long double           ld;
typedef    unsigned long long    ull;
typedef    pair<int,int>         pii;
typedef    pair<ll,ll>           pll;
typedef    vector<int>           vi;
typedef    vector<ll>            vll;
typedef    vector<vector<int>>   vvi;

inline     int         Int(){int x; cin >> x; return x;}
inline     ll          Long(){ll x; cin >> x; return x;}
inline     float       Float(){float x; cin >> x; return x;}
inline     double      Double(){double x; cin >> x; return x;}
inline     void        Yes(){cout << "Yes" << endl;}
inline     void        No(){cout << "No" << endl;}
inline     void        YES(){cout << "YES" << endl;}
inline     void        NO(){cout << "NO" << endl;}

const      int N       =(int)2e5 + 5;
const      int maxN    =(int)1e6 + 6;
const      ll  Mod     =(ll)1e9 + 7;
const      int inf     =(int)2e9;
const      ll  Inf     =(ll)1e18;

#define    fastio      ios_base::sync_with_stdio(false),cin.tie(NULL)
#define    T           int t,q; cin >> t; for(q=1;q<=t;q++)
#define    Forn(i,n)   for(int i=0;i<n;i++)
#define    ForN(i,n)   int i;for(i=n-1;n>=0;i--)
#define    forn(i,n)   for(int i=1;i<=n;i++)
#define    forN(i,n)   int i;for(i=n;n>=1;i--)
#define    fors(i,s)   for(int i=0;i<s.size();i++)
#define    Sort(s)     sort(s.begin(),s.end())
#define    debug(x)    cerr << #x << " = " << x << '\n' ;
#define    rep(i,b,e)  for(__typeof(e) i=(b); i!=(e+1)-2*(b>e); i=i+1-2*(b>e))
#define    gcd(a,b)    __gcd(a , b)
#define    lcm(a,b)    (a*(b/__gcd(a,b)))
#define    gt          greater<int>()
#define    Int         Int()
#define    Long        Long()
#define    Float       Float()
#define    Double      Double()
#define    all(x)      x.begin() , x.end()
#define    sz(x)       (int)x.size()
#define    ff          first
#define    ss          second
#define    pb          push_back
#define    eb          emplace_back
#define    mp          make_pair
#define    mem(a)      memset(a,0,sizeof a)
#define    memn(a)     memset(a,-1,sizeof a)
#define    biday       return 0
#define    nl          endl
#define    sp(n)       fixed<<setprecision(n)
#define    pi          acos(-1)
#define    PI          3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342

int main()
{
    fastio;
    ll a[3],d,res=0;
    Forn(i,3)cin >> a[i];
    sort(a,a+3);
    cin >> d;

    if(a[1]-a[0]<d)res+=d-(a[1]-a[0]);
    if(a[2]-a[1]<d)res+=d-(a[2]-a[1]);
    cout << res << endl;

    biday;
}

//...............BYE BYE................

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    long long timeC = 0;
    long long x0 = 0;
    if(abs(a - b) < d)
    {

        x0 = d - (long long)(abs(a - b));
    }
    long long x1 = 0;
    if(abs(b - c) < d)
    {
        x1 = d - (long long)(abs(b - c));
    }
    long long x2 = 0;
    if(abs(a-c) < d)
    {
        x2 = d - (long long)(abs(a - c));
    }
    long long Arr[4] = {};
    Arr[0] = x0;
    Arr[1] = x1;
    Arr[2] = x2;
    sort(Arr, Arr + 3, greater<long long>());
    timeC += Arr[0];
    timeC += Arr[1];
    cout << timeC << endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char const *argv[])
{
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	long long int sum=a+b+c;
	long long int temp1, temp2;
	temp1=min(min(a,b),c);
	temp2=max(max(a,b),c);
	a=temp1;
	c=temp2;
	b=sum-(a+c);
	long long int t;
	if((b-a)>=d&&(c-b)>=d)
		t=0;
	else if((b-a)>=d&&(c-b)<d)
		t= d+b-c;
	else if((b-a)<d&&(c-b)>=d)
		t=d+a-b;
	else
	   t= 2*d+a-c;
	cout << t <<'\n';
	return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int sum = 0;
	vector<int> a(3, 0);
	for (int i = 0; i < 3; i++) cin >> a[i];
	int d; cin >> d;
	sort(a.begin(), a.end());

	/*
	2 5 6 3
	1 3 5 6 -> 1 7 5 -> 1 5 7 -> 1 7 7 -> 1 7 13
	*/
	for (int i = 0; i < 2; i++)
	{
		int temp = a[i+1] - a[i]; if (temp < 0) temp *= -1;
		int temptemp = temp - d; if (temptemp < 0)
		{
			temptemp *= -1;
			sum += temptemp;
		}
	}

	cout << sum;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[5500],sum,nm=0;
int main()
{
	//freopen("1.in","r",stdin);
	for(int i=1;i<=4;++i) scanf("%lld",&a[i]);
	sort(a+1,a+4);
	sum=max(nm,a[4]-(a[2]-a[1]))+max(nm,a[4]-(a[3]-a[2]));
	printf("%lld",sum);
	return 0;
}

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
#include<math.h>

using namespace std;
int main()
{
    long long int a,b,c,d;
    cin>>a>>b>>c>>d;
    long long int t = 0;
    vector <long long int> v = {a,b,c};
    sort(v.begin(), v.end());
    if(d >= (v[2]-v[1]) && d >= (v[1]-v[0])){
        t = 2*d - (v[2]-v[1]) - (v[1]-v[0]);
    }
    else if(d >= (v[2]-v[1]) && d < (v[1]-v[0])){
        t = d - (v[2]-v[1]);
    }
    else if (d < (v[2] - v[1]) && d >= (v[1]-v[0])){
        t = d - (v[1] - v[0]);
    }
    cout<<t;
    return 0;
}

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int d;
    vector<int> vec(3);
    for (int i = 0; i < 3; ++i) {
        cin >> vec[i];
    }
    cin >> d;
    sort(vec.begin(), vec.end());
    cout << max(0, d - (vec[1] - vec[0])) + max(0, d - (vec[2] - vec[1])) << endl;
return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
int a[3],dis,res;
int main()
{
	cin>>a[0]>>a[1]>>a[2]>>dis;
	sort(a,a+3);
	if(a[1] - a[0] >=dis && a[2]-a[1] >= dis) cout<<"0"<<endl;
	else if(a[1] - a[0] >= dis || a[2] - a[1] >= dis){
		int minn = min(a[1] - a[0], a[2] - a[1]);
		cout<<dis-minn<<endl;
	}
	else cout<<2*dis-(a[2]-a[1] + a[1] - a[0])<<endl;
	return 0;
}

#include "bits/stdc++.h"
using namespace std;

int a, b, c, d, mmax, mmin, mmed, r;

int main()
{
    cin >> a >> b >> c >> d;
    mmax = max(a, max(b, c));
    mmin = min(a, min(b, c));
    mmed = a+b+c-mmax-mmin;
    if(mmax - mmin >= 2*d)
        if((mmax-mmed) >= d && (mmed-mmin) >= d) r = 0;
        else r = abs(d-(mmed-mmin));
    else
        r = max(d-(mmed-mmin), 0) + max(d-(mmax-mmed), 0);
    cout << r << endl;
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
int main()
{
    int num[3];
    int d;
    scanf("%d %d %d %d",&num[0],&num[1],&num[2],&d);
    sort(num,num+3);
    int ans=0;
    if(num[1]-num[0]<d)
        ans+=d-num[1]+num[0];
    if(num[2]-num[1]<d)
        ans+=d-num[2]+num[1];
    printf("%d\n",ans);
}

#include <iostream>
using namespace std;
int a,b,c,d,g,s,x,n,m;
bool y;
int main() {
cin>>a>>b>>c>>d;
  g=0;s=0;

  x=a;
  if (x<b){ x=b;}
  if (x<c){ x=c;}

  n=a;
  if (n>b){ n=b;}
  if (n>c){ n=c;}

  m=a+b+c-x-n;

 while(x-m<d){
     x=x+1;
     s=s+1;
     }

 while(m-n<d){
     n=n-1;
     g=g+1;
 }

   cout<<s+g;

}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a[3],d;
    int ans1=0,ans2=0;
    cin>>a[0]>>a[1]>>a[2]>>d;
    sort(a,a+3);
    if(a[0]>a[1]-d)ans1=a[0]-a[1]+d;
    if(a[2]<a[1]+d)ans2=a[1]+d-a[2];
    cout<<ans1+ans2<<endl;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[3],d,cnt=0;
	cin>>a[0]>>a[1]>>a[2]>>d;
	sort(a,a+3);
	int l = a[1]-a[0];
	int r = a[2]-a[1];
	while(l<d||r<d)
	{
		if(l<d)
		{
			l++;
		}
		else if(r<d)
		{
			r++;
		}
		cnt++;
	}
	cout<<cnt;
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define pii pair<int,int>
#define pb push_back
#define eb emplace_back
#pragma GCC optimize("unroll-loops")

using namespace std;
using namespace __gnu_pbds;

typedef long long int ll;
typedef unsigned long long int ull;
int MOD=1000000007;
int MOD2=998244353;
vector<int> bases;
const ll LLINF=1ll<<60;
const char en='\n';

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void yes() {cout<<"YES"<<en; exit(0);}
void no() {cout<<"NO"<<en; exit(0);}
inline int rund() {int x576363482791fuweh=rng();return abs(x576363482791fuweh)%RAND_MAX;}
template<class T>
void prVec(vector<T> w)
{
	cout<<w.size()<<endl;
	for (int i=0;i<int(w.size())-1;++i) cout<<w[i]<<' ';
	if (w.size()) cout<<w[w.size()-1]<<endl;
}

void M998()
{
	swap(MOD,MOD2);
}

ll raand()
{
	ll a=rund();
	a*=RAND_MAX;
	a+=rund();
    return a;
}

#define rand raand

ll raaand()
{
	return raand()*(MOD-7)+raand();
}

string to_upper(string a)
{
	for (int i=0;i<(int)a.size();++i) if (a[i]>='a' && a[i]<='z') a[i]-='a'-'A';
	return a;
}

string to_lower(string a)
{
	for (int i=0;i<(int)a.size();++i) if (a[i]>='A' && a[i]<='Z') a[i]+='a'-'A';
	return a;
}

ll sti(string a)
{
	ll k=0;
	for (int i=0;i<(int)a.size();++i)
	{
		k*=10;
		k+=a[i]-'0';
	}
	return k;
}

string its(ll k)
{
	if (k==0) return "0";
	string a;
	while (k)
	{
		a.push_back((k%10)+'0');
		k/=10;
	}
	reverse(a.begin(),a.end());
	return a;
}

ll min(ll a,int b)
{
	if (a<b) return a;
	return b;
}

ll min(int a,ll b)
{
	if (a<b) return a;
	return b;
}

ll max(ll a,int b)
{
	if (a>b) return a;
	return b;
}

ll max(int a,ll b)
{
	if (a>b) return a;
	return b;
}

ll gcd(ll a,ll b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
	return a/gcd(a,b)*b;
}

template<class T,class K>
pair<T,K> mp(T a,K b)
{
	return make_pair(a,b);
}

inline int mult(ll a,ll b)
{
	return (a*b)%MOD;
}

inline int pot(int n,int k)
{
	if (k==0) return 1;
	ll a=pot(n,k/2);
	a=mult(a,a);
	if (k%2) return mult(a,n);
	else return a;
}

int divide(int a,int b)
{
	return mult(a,pot(b,MOD-2));
}

inline int sub(int a,int b)
{
	if (a-b>=0) return a-b;
	return a-b+MOD;
}

inline int add(int a,int b)
{
	if (a+b>=MOD) return a+b-MOD;
	return a+b;
}

bool prime(ll a)
{
	if (a==1) return 0;
	for (int i=2;i<=round(sqrt(a));++i)
	{
		if (a%i==0) return 0;
	}
	return 1;
}

ll has(string x)
{
	ll h1=0,h2=0;
	x=to_lower(x);
	for (char a: x)
	{
		h1*=bases[0];
		h1+=a-'a';
		h1%=bases[3];
		h2*=bases[1];
		h2+=a-'a';
		h2%=bases[4];
	}
	return h1*(MOD+13893829)+h2;
}

const int N=300010;
ll a,b,c,d,r;
int dx[]={0,1,0,-1};
int dy[]={1,0,-1,0};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	for (int i=0;i<10;++i) bases.push_back(rand()%(MOD-13893829)+13893829);
	cin>>a>>b>>c>>d;
	if (a>b) swap(a,b);
	if (a>c) swap(a,c);
	if (b>c) swap(b,c);
	if (b-a<d && c-b<d)
	{
		ll a1=b-d,c1=b+d;
		r+=a-a1;
		r+=c1-c;
		c=c1;
		a=a1;
		//cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
	}
	if (b-a<d)
	{
		ll a1=b-d;
		r+=a-a1;
		a=a1;
	}
	if (c-b<d)
	{
		ll c1=b+d;
		r+=c1-c;
		c=c1;
	}
	cout<<r;
}

#include <bits/stdc++.h>

using namespace std;

int main(){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    int t=0;

    if (a>=b && a>=c){
        swap(c,a);
    }
    else if (a>=b && a<=c){
        c=c;
        }
        else if (a>=c && a<=b){
                swap(c,b);
            }
    if (b>a){
        b=b;
    }
    else swap(a,b);
    if (b>c){
        swap(b,c);
    }

    while((c-b)<d){
            c++; t++;
    }
    cout << "\n";

    while((b-a)<d){
        a--; t++;
    }
    cout << t;

return 0;
}

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef __int64  ll;
const int inf=0x3f3f3f3f;
int main()
{
	ll a[3],d;
	scanf("%I64d%I64d%I64d%I64d",&a[0],&a[1],&a[2],&d);
	sort(a,a+3);
	ll 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("%I64d\n",ans);
}

#include<bits/stdc++.h>
using namespace std;

void sort(int arr[],int n)
{
    int i,j,temp;

    for(i=0;i<n-1;++i)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j])
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
}

int main()
{
    int d,i,j,d1=0,d2=0,moves=0;

    int arr[3];
    for(i=0;i<3;++i)
    {
        cin>>arr[i];
    }

    cin>>d;
    sort(arr,3);

    d1=arr[1]-arr[0];
    d2=arr[2]-arr[1];

    if(d1<d)
    moves+=(d-d1);

    if(d2<d)
    moves+=(d-d2);

    cout<<moves;
}

#include<cstdio>
#include<algorithm>
using namespace std;

int A[10];
int D;

int main(){

	scanf("%d%d%d%d",&A[1],&A[2],&A[3],&D);

	sort(A+1,A+4);

	printf("%d",max(0,A[1]-A[2]+D)+max(0,A[2]-A[3]+D));

	return 0;

}

/*********************************************************************************\
*  _________  _      _  ________  _       _       __       ________               *
* |___   ___|| |    | ||  ______|| |     | |     /  \     |  ____  |              *
*     | |    | |    | || |______ | |_____| |    / /\ \    | |____| |              *
*     | |    | |    | ||______  ||  _____  |   / /__\ \   |  __  __|              *
*     | |    | |____| | ______| || |     | |  / ______ \  | |  \ \                *
*     |_|    |________||________||_|     |_| /_/      \_\ |_|   \_\               *
*                                                                                *
*   Department of Computer Science & Engineering                                  *
*   Student ID : 18CSE035                                                         *
*   Bangabnadhu Sheikh Mujibur Rahman Science & Technology University,Gopalgonj.  *
*                                                                                *
\*********************************************************************************/

//Now Write to Code ___________________________

#include <bits/stdc++.h>
using namespace std ;

typedef    long long             ll;
typedef    long double           ld;
typedef    unsigned long long    ull;
typedef    pair<int,int>         pii;
typedef    pair<ll,ll>           pll;
typedef    vector<int>           vi;
typedef    vector<ll>            vll;
typedef    vector<vector<int>>   vvi;

inline     int         Int(){int x; cin >> x; return x;}
inline     ll          Long(){ll x; cin >> x; return x;}
inline     float       Float(){float x; cin >> x; return x;}
inline     double      Double(){double x; cin >> x; return x;}
inline     void        Yes(){cout << "Yes" << endl;}
inline     void        No(){cout << "No" << endl;}
inline     void        YES(){cout << "YES" << endl;}
inline     void        NO(){cout << "NO" << endl;}

const      int N       =(int)2e5 + 5;
const      int maxN    =(int)1e6 + 6;
const      ll  Mod     =(ll)1e9 + 7;
const      int inf     =(int)2e9;
const      ll  Inf     =(ll)1e18;

#define    fastio      ios_base::sync_with_stdio(false),cin.tie(NULL)
#define    T           int t,q; cin >> t; for(q=1;q<=t;q++)
#define    Forn(i,n)   for(int i=0;i<n;i++)
#define    ForN(i,n)   int i;for(i=n-1;n>=0;i--)
#define    forn(i,n)   for(int i=1;i<=n;i++)
#define    forN(i,n)   int i;for(i=n;n>=1;i--)
#define    fors(i,s)   for(int i=0;i<s.size();i++)
#define    Sort(s)     sort(s.begin(),s.end())
#define    debug(x)    cerr << #x << " = " << x << '\n' ;
#define    rep(i,b,e)  for(__typeof(e) i=(b); i!=(e+1)-2*(b>e); i=i+1-2*(b>e))
#define    gcd(a,b)    __gcd(a , b)
#define    lcm(a,b)    (a*(b/__gcd(a,b)))
#define    gt          greater<int>()
#define    Int         Int()
#define    Long        Long()
#define    Float       Float()
#define    Double      Double()
#define    all(x)      x.begin() , x.end()
#define    sz(x)       (int)x.size()
#define    ff          first
#define    ss          second
#define    pb          push_back
#define    eb          emplace_back
#define    mp          make_pair
#define    mem(a)      memset(a,0,sizeof a)
#define    memn(a)     memset(a,-1,sizeof a)
#define    biday       return 0
#define    nl          endl
#define    sp(n)       fixed<<setprecision(n)
#define    pi          acos(-1)
#define    PI          3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342

int main()
{
    fastio;
    ll a[3],d,res=0;
    Forn(i,3)cin >> a[i];
    sort(a,a+3);
    cin >> d;
    if(a[1]-a[0]<d)
    {
    	res+=d-(a[1]-a[0]);
    	a[0]-=d-(a[1]-a[0]);
    }
    if(a[2]-a[1]<d)
    {
    	res+=d-(a[2]-a[1]);
    	a[2]+=d-(a[2]-a[1]);
    }
    if(a[2]-a[0]<d)res+=d-(a[2]-a[0]);
    cout << res << endl;

    biday;
}

//...............BYE BYE................

#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	vector<int> v(3,0);
	v[0] = a;
	v[1] = b;
	v[2] = c;
	sort(v.begin(),v.end());

	int s = 0;
	int s1,s2;
	s1 = v[1]-v[0];
	s2 = v[2]-v[1];
	if(s1<d)
		s += (d-s1);
	if(s2<d)
		s += (d-s2);
	cout<<s;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int a[5],d,ans;
int main(){
    scanf("%d%d%d%d",&a[1],&a[2],&a[3],&d);
    sort(a+1,a+4);
    ans=max(0,d+a[1]-a[2])+max(0,d+a[2]-a[3]);
    printf("%d",ans);
    return 0;
}

#include<stdio.h>
#include<math.h>

int main()
{
    long long int num[3],d;
    scanf("%lld %lld %lld %lld",&num[0],&num[1],&num[2],&d);
    long long int mid,i,j,temp;

    for(i=0;i<3;i++){
        for(j=0;j<3-i-1;j++){
            if(num[j] > num[j+1]){
                temp = num[j];
                num[j] = num[j+1];
                num[j+1] = temp;
            }
        }
    }
    //printf("%lld %lld %lld\n",num[0],num[1],num[2]);
    mid = num[1]-num[0];
    if(mid<d)
        i = d-mid;
    else i=0;
    mid = num[2]-num[1];
    if(mid<d)
        j = d-mid;
    else j=0;
    temp = i+j;
    printf("%lld\n",temp);
    return 0;
}

#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
int main()
{
	int d,ans=0;
	int a[3];
	int i;
	cin>>a[0]>>a[1]>>a[2]>>d;
	sort(a,a+3);
	if(a[1]-a[0]<d)
	  ans+=(d-(a[1]-a[0]));
	if(a[2]-a[1]<d)
	  ans+=(d-(a[2]-a[1]));
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a[3], d;
    cin >> a[0]>> a[1] >> a[2] >> d;
    sort(a, a+3);
    int res = 0;
    if(a[1]-a[0] < d)   res += d-a[1]+a[0];
    if(a[2]-a[1] < d)   res += d-a[2]+a[1];
    cout << res << endl;

}

#include<iostream>
using namespace std;
int main()
{
    int a, b, c, d, x, result=0;
    cin>>a>>b>>c>>d;

    while((a>=b && b>=c)!=1)
    {
        if(a<b)
        {
            x=b;
            b=a;
            a=x;
        }
        if(b<c)
        {
            x=c;
            c=b;
            b=x;
        }

    }
    for( ; ((a-b)>=d && (b-c)>=d)!=1;  )
    {
        if(((a-b)>=d)!=true)
        {
            a++;
            result++;
        }
        if(((b-c)>=d)!=true)
        {
            c--;
            result++;
        }
    }
    cout<<result;
}

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long a,b,c,d,x=0;
    cin >> a >> b >> c >> d;
    if (a==b && b==c && c==d)
    {
        cout << a*2;
        return 0;
    }
    long long y[3];
    y[0]=a;
    y[1]=b;
    y[2]=c;
    sort (y,y+3);
    if (y[1]-y[0]<d)
        x+=d-(y[1]-y[0]);
    if (y[2]-y[1]<d)
        x+=d-(y[2]-y[1]);
    cout << x;
    return 0;
}

#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;
long long a,b,c,d,t;
int main()
{
	t=0;
	cin>>a>>b>>c>>d;
	if(a>b)
		swap(a,b);
	if(b>c)
		swap(b,c);
	if(a>b)
		swap(a,b);
	if(b-a<d)
		t=t+d-(b-a);
	if(c-b<d)
		t=t+d-(c-b);
	//cout<<a<<" "<<b<<" "<<c<<endl;
	cout<<t;
	return 0;
}

#include<bits/stdc++.h>
#define int long long
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
#define fordec(i,a,b) for(int i=a;i>=b;--i)
using namespace std;

int a,b,c,d,res1,res2,res3,res;
main()
{
    cin>>a>>b>>c>>d;
    int mi=min({a,b,c});
    int ma=max({a,b,c});
    int mid=a+b+c-mi-ma;
    res1=mid-mi,res2=ma-mid;
    cout<< (d-res1>0?d-res1:0)+(d-res2>0?d-res2:0);

}

    #include<bits/stdc++.h>
    using namespace std;

    int main()
    {
        int a[3],cnt=0,d;
        for(int i=0;i<3;i++){
            cin>>a[i];
        }
        cin>>d;
        sort(a,a+3);
        if(a[1]-a[0]<d){
            cnt+=d-(a[1]-a[0]);
        }
        if(a[2]-a[1]<d){
            cnt+=d-(a[2]-a[1]);
        }
        cout<<cnt;
        return 0;
    }

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

#include <iostream>
#include <vector>
#include <bits/stdc++.h>

using namespace std;

int main(){
    int d, step1 = 0, step2 = 0;
    vector <int> a(3);
    cin >> a[0] >> a[1] >> a[2] >> d;
    sort(a.begin(), a.end());
    if(abs(a[1] - a[0]) < d) {
        step1 = abs(abs(a[1] - a[0]) - d);
    }
    if(abs(a[2] - a[1]) < d){
        step2 = abs(abs(a[2] - a[1]) - d);
    }
    cout << step1 + step2;
}

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define vfor(v) for(auto &&x: v)
#define pb push_back
using namespace std;
typedef long long ll;

void solve(){
    ll a[3],t,g=0,p,q;

    for(ll x=0;x<3;x++)cin>>a[x];

    cin>>p;
    sort(a,a+3);

    t=abs(a[0]-a[1]);q=p-t;
    if(t<p){g+=q;a[1]+=q;}
    t=abs(a[1]-a[2]);q=p-t;
    if(t<p){g+=q;a[2]+=q;}
    t=abs(a[0]-a[2]);q=p-t;
    if(t<p){g+=q;a[2]+=q;}
    cout<<g;
}

int main(){
    IOS;
    solve();
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{

        vector <long long int> d(3,0);
        vector <long long int>  x(3,0);
        vector <long long int>  arr(4,0);
        int i=0;
            cin >> arr[0] ;
            cin >> arr[1] ;
            cin >> arr[2] ;
            cin >> arr[3] ;

          //cout << i << endl;
        x[0]=arr[0];
        x[1]=arr[1];
        x[2]=arr[2];

        long long int  dis=arr[3];
        sort(x.begin(), x.end());

        d[0]=abs(x[0]-x[1]);
        d[1]=abs(x[0]-x[2]);
        d[2]=abs(x[1]-x[2]);

        long long int  m = min(d[0],min(d[1],d[2]));
        //cout<< m<<endl;
        if(m>=dis){
            cout<< 0<<endl;
        }

        long long int  time=0;
             if(d[0]<dis){
                time=time+(dis-d[0]);
                x[0]=x[0]-(dis-d[0]);
                d[0]=abs(x[0]-x[1]);
                d[1]=abs(x[0]-x[2]);
             }
            if(d[1]<dis){
                time=time+(dis-d[1]);
                x[2]=x[2]+(dis-d[1]);
                d[1]=abs(x[0]-x[2]);
                d[2]=abs(x[1]-x[2]);
             }
            if(d[2]<dis){
                time=time+(dis-d[2]);
                x[2]=x[2]+(dis-d[2]);
                d[1]=abs(x[0]-x[2]);
                d[2]=abs(x[1]-x[2]);
             }

        cout << time << endl;

    return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int a[3],d,cnt=0;
	cin>>a[0]>>a[1]>>a[2]>>d;
	sort(a,a+3);
	int l = a[1]-a[0];
	int r = a[2]-a[1];
	cout<<l<<r<<endl;
	while(l<d||r<d)
	{
		if(l<3)
		{
			l++;
		}
		else if(r<3)
		{
			r++;
		}
		cnt++;
	}
	cout<<cnt;
	return 0;
}

#include <bits/stdc++.h>

#define pb push_back
#define  mp make_pair
#define pp pop_back
#define pf push_front
#define fi first
#define se second
#define maxn 500005
#define mod 1000000007
typedef long long ll;
using namespace std;
#define pi pair<ll,ll >
#define debug_iv_ac ios::sync_with_stdio(false);cin.tie(NULL);

struct node{
    ll i,w,s;
    node(){}

    node(ll _i,ll _s){
        s = _s,i = _i;
    }

    bool operator < (node X)const{
        if(s!=X.s) return s < X.s;
        //if(pos!=X.pos) return X.pos<pos;
        return i < X.i;
    }
};

ll power(ll x, ll y, ll p)
{
    ll res = 1;      // Initialize result

    x = x % p;  // Update x if it is more than or
                // equal to p

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x) % p;

        // y must be even now
        y = y>>1; // y = y/2
        x = (x*x) % p;
    }
    return res;
}

ll add(ll a, ll b)
{
    a%=mod;b%=mod;
    return (a+b)%mod;
}

ll sub(ll a,ll b)
{
    a%=mod;b%=mod;
    return (a-b+mod)%mod;
}

ll prod(ll a,ll b)
{
    a%=mod;
    b%=mod;
    return (a*b)%mod;
}
const int MAXN = 500005;

ll a[10];

int main()
{
	debug_iv_ac
	for(int i=1;i<=4;i++) cin>>a[i];
	ll d = a[4];
	// cout<<d<<endl;
	sort(a+1,a+4);
	ll ans = 0,x = a[2]-a[1],y = a[3]-a[2];
	// cout<<x<<" "<<y<<endl;
	if(x>=d&&y<=d)
	{
		ll e = min(x-d,d-y);
		y += min(x-d,d-y);ans += e;
		if(y<d) ans += d-y;
	}
	else if(x<=d&&y>=d)
	{
		ll e = min(d-x,y-d);
		x += min(d-x,y-d);ans+=e;
		if(x<d) ans += d-x;
	}
	else
	{
		ans = d-x + d-y;

	}
	cout<<ans;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

void sort(int arr[],int n)
{
    int i,j,temp;

    for(i=0;i<n-1;++i)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j])
            {
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }
}

int main()
{
    int d,i,j,d1=0,d2=0,moves=0;

    int arr[3];
    for(i=0;i<3;++i)
    {
        cin>>arr[i];
    }

    cin>>d;
    sort(arr,3);

    d1=arr[1]-arr[0];
    d2=arr[2]-arr[1];

    if(d1<d)
    moves+=(d-d1);

    if(d2<d)
    moves+=d2;

    cout<<moves;
}

#include <bits/stdc++.h>
#define ll long long int
#define P pair
#define F first
#define S second
#define m_p make_pair
#define ld long double
//#include <wadim>

using namespace std;

string alpha = "abcdefghijklmnopqrstuvwxyz";
string Alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string glas = "aeiou";
ll Mod = 998244353;
ll ctoi(char x) {
	ll xx = int(x) - 48;
	return xx;
}

ll one = 1;
ll null = 0;

int main() {
	iostream::sync_with_stdio(false);
	cin.tie(0);
    ll a,b,c,d; cin >> a >> b >> c >> d;
    if(a>b) swap(a,b);
    if(b>c) swap(b,c);
    ll sum=max(null,d-(b-a));
    b+=max(null,d-(b-a));
    sum+=min(max(null,d-c+b),c-a+d);
    cout << sum;
}

#include <iostream>
using namespace std;
int main() {
    int a, b, c, d;
    cin>>a>>b>>c>>d;
    if(abs(c-a)<abs(b-a)) swap(b,a);
    cout<<(d - abs(c-b)) *((d-abs(c-b))>0)+(d - abs(b-a)) *((d-abs(b-a))>0);
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long int a,b,c,d,t,count=0;
    cin>>a>>b>>c>>d;
    if(a>b)
    {
        t=a;a=b;b=t;
    }
    if(b>c)
    {
        t=b;b=c;c=t;
    }

    if(c-b<d)   count+=d-c+b;
    if(b-a<b)   count+=d-b+a;

    cout<<count;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
 int n;
 cin>>n;

}

#include<bits/stdc++.h>
using namespace std;
int a[3];
int main()
{
	int d;
	cin>>a[0]>>a[1]>>a[2]>>d;
	sort(a,a+3);
	int b=a[0]+d;
	int c=b+d;
    printf("%d",max(b-a[1],c-a[2])<0?0:max(b-a[1],c-a[2])<0);
    return 0;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include <initializer_list>
#include<unordered_set>
#include<map>
#include <iterator>
#include<stack>
#include<queue>
#include <iomanip>
using namespace std;

int main(){
    /*int x,sum=0;
    string o,n;
    cin>>x;
    cin>>o>>n;
    for (int i=0;i<x;i++){
        if (abs(o[i]-n[i])<=5)
            sum=sum+abs(o[i]-n[i]);
        else
            sum=sum+(10-abs(o[i]-n[i]));
    }
    cout<<sum<<endl;*/
    /*int x,c=1,r;
    cin>>x;
    r=x;
    string s,q;
    while (x!=0){
    cin>>s;
    if (c==1){
    if (s[0]=='O' && s[1]=='O')
    {
        s[0]='+';
        s[1]='+';
            c--;

    }
    else if ( s[3]=='O'&&s[4]=='O')
    {
        s[3]='+';
        s[4]='+';

        c--;

    }
    }
    q=q+s;
    x--;
    }
    if (c==0){
    cout<<"YES\n";

    for (int i=0;i<q.length();i++){
        cout<<q[i];
    if ((i+1)%5==0)
        cout<<endl;
    }
    }
    else
        cout<<"NO\n";*/
    /*bool t=false;
    char c;
    int x,y;
    cin>>x>>y;
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            cin>>c;
            if (c!='W' && c!='B' && c!='G')
                t=true;
        }
    }
    if (t==true)
        cout<<"#Color\n";
    else
        cout<<"#Black&White\n";*/
    /*int x,y;
    cin>>x;
    int c=0;
    int *b=new int [x];
    for (int i=0;i<x;i++){
        cin>>b[i];
    }
    cin>>y;
    int *g=new int [y];
    for (int i=0;i<y;i++){
        cin>>g[i];
    }
    sort(b,b+x);
    sort(g,g+y);
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            if (g[j]!=102 && b[i]==g[j] || abs(b[i]-g[j])==1){
                g[j]=102;
                c++;
                break;
            }
        }
    }
    cout<<c<<endl;*/
    /*int x,c=1,sum=0,con=0;
    cin>>x;
    int *y=new int [x];
    for (int i=0;i<x;i++)
        cin>>y[i];
    int d,m;
    cin>>d>>m;
    if (x==1){
        if (y[0]==d)
        cout<<1<<endl;
    }
    else {
    for (int i=0;i<x;i++){
        sum=0;
        sum=sum+y[i];
        c=1;
		while (c<=m){
        for (int j=i+1;j<x;j++){

            c++;
            sum=sum+y[j];
            if (c==m){
                if (sum==d)
                    con++;

            }
			if (c>m)
				break;
        }
		break;
        }

    }
    cout<<con<<endl;
    }*/
/*int n;
    cin>>n;
    int *ar=new int [n];
int s[6]={0,0,0,0,0,0};
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
      s[ar[i]-1]++;

    }

    int fact=1;
   for (int i = 1; i <= n; i++) {
        fact = fact * i;
   }
    int fact1=1;
    for(int i=1;i<=6;i++)
    {
        fact1=1;
        for(int j=1;j<=s[i-1];j++)
        {
            fact1 = fact1 * j;

        }
        fact/=fact1;

    }
    cout<<fact;*/
/*int gcd,l,b,n;
cin>>n;
while (n!=0){
	cin>>l>>b;
	if (l>=b){
	 for(int i=1;i<l;i++)
    {
        if(l%i==0 && b%i==0)
        {
            gcd=i;
        }
    }
	}
	else{
		 for(int i=1;i<b;i++)
    {
        if(l%i==0 && b%i==0)
        {
            gcd=i;
        }
    }
	}
	cout<<(b*l)/(gcd*gcd)<<endl;
	n--;
}*/
/*int x;
cin>>x;
long q,n,y,z;
while (x!=0)
{
	z=(ceil(q/2.0)*y);
	cin>>q>>n>>y;
	if ((ceil(q/2.0)*y<(q*n)))
		cout<<z<<endl;
	else
		cout<<q*n<<endl;
	x--;

}*/
/*int n;
cin>>n;
int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
long sum=0,k=0;
for (int i=0;i<n;i++)
{
	sum=0;
	for (int j=0;j<n;j++){
		if (y[i]==y[j] || abs(y[i]-y[j])!=1)
			sum=sum+y[j];
	}
	if (sum>k)
		k=sum;

}
 cout<<k<<endl;*/
/*long a,b,c,m,n,h;
cin>>a>>b>>c;
m=max(a*b*c,a+b+c);
n=max(a*(b+c),(a+b)*c);
cout<<max(n,m)<<endl;*/
/*long long x;
cin>>x;
if (x%2==0)
	cout<<(long long)x/2<<endl;
else
	cout<<(long long)(ceil(x/2.0)*-1)<<endl;
    system("pause");*/
/*long x,t,c=0,k=0;
cin>>x;
long y[4]={0,0,0,0};
for (int i=0;i<x;i++){
	cin>>t;
	y[t-1]++;
}
c=y[3];
if (y[1]%2==0){
	if (y[2]==y[0]){
	c=c+y[0];
	c=c+y[1]/2;
	cout<<c<<endl;
	}
	else if (y[2]>y[0]){
	c=c+y[1]/2;
	c=c+y[0];
	c=c+y[2]-y[0];
	cout<<c<<endl;
	}
	else{
	c=c+y[1]/2;
	c=c+y[2];
	c=c+(y[0]-y[2])/4;
	if ((y[0]-y[2])%4!=0)
		c++;
	cout<<c<<endl;
	}
}
else{
	if (y[2]==y[0]){
	c=c+y[0];
	c=c+y[1]/2+1;
	cout<<c<<endl;
	}

	else if (y[2]>y[0]){
	c=c+y[0];
	c=c+y[1]/2+1;
	c=c+(y[2]-y[0]);
	cout<<c<<endl;
	}
	else {
	c=c+y[2];
	y[0]=y[0]-y[2];

	c=c+y[1]/2;

	y[1]=1;

	for (int i=y[0];i>=1;i--){

		if ((2*y[1]+i)%4==0){
			c=c+((2*y[1]+i)/4);

			y[0]=y[0]-i;
			break;
		}
	}

	if (y[0]%4==0)
	c=c+(y[0]/4);
	else
	c=c+(y[0]/4)+1;
	cout<<c<<endl;
		}

	}*/

	/*int x,q,y,c=0;
	cin>>x;
	while (x!=0){
		cin>>y>>q;
		if (y<=q-2)
			c++;
		x--;
	}

	cout<<c<<endl;*/
 /*int x,y;
 cin>>x>>y;
 if (x==1 && x==10)
	 cout<<-1<<endl;
 else{
	 if (y==10){
		 cout<<1;
		 for (int i=0;i<x-1;i++)
			 cout<<0;
	 }
	 else{
		 cout<<y;
		 for (int i=0;i<x-1;i++)
			 cout<<0;

	 }

 }
      cout<<endl; */
          /* if (s[0]>=97 && s[0]<=122)
           s[0]=s[0]-32;
           cout<<s<<endl;*/
/*int x,y;
cin>>x>>y;
if (x<y)
	cout<<-1<<endl;
else{
	if (y>=ceil(x/2.0))
		cout<<y<<endl;
	else{
		int sum=y;
		while (sum<=x/2){
			sum=sum+(y);
			if (sum==ceil(x/2.0))
				break;
		}
		cout<<sum<<endl;
	}
}*/

/*long long x,y;
cin>>x>>y;
if (y<=ceil(x/2.0))
	cout<<y*2-1<<endl;
else{
	y=y-ceil(x/2.0);
	cout<<y*2<<endl;
}*/
/*double sum=0;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
	sum=sum+((double)y[i]/100);
}

sum=(sum/x)*100;
printf("%3.12f\n",sum);*/
/*int t,a,b;
cin>>t;
while (t!=0){
	cin>>a>>b;
	cout<<(a>=b?"FunkyMonkeys\n":"WeWillEatYou\n");
	t--;
}*/
/*string t="AHIMOTUVWXY";
string s;
cin>>s;
int c=0;
for (int i=0;i<s.length();i++){
	for (int j=0;j<t.length();j++){
		if (s[i]==t[j]){
			c++;
			break;
		}
	}
}
if (c!=s.length())
	cout<<"no\n";
else{
	bool t=true;
	for (int i=s.length()-1;i>=0;i--){
		if (s[i]!=s[s.length()-i-1])
		{
			cout<<"no\n";
			t=false;
			break;
		}
	}
	if (t)
		cout<<"yes\n";
}*/
/*int x,sum=0,y=1;
cin>>x;
if (x==1)
	cout<<1<<endl;
else if (x==2)
	cout<<3<<endl;
else{
	sum=(x*(x+1))/2;
	for (int i=x-1;i>1;i--){
		sum=sum+(i-1)*y;
		y++;
	}
	cout<<sum<<endl;
}*/

/*long x,sum=0;
cin>>x;
long *y=new long [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);

for (int i=0;i<x;i++){
	sum=sum+(y[x-1]-y[i]);
}
cout<<sum<<endl;*/
/*int x;
cin>>x;
if (x%2==0){
for (int i=x;i>0;i--){
	if (i==1)
		cout<<"I love it ";
	else if (i%2==0)
		cout<<"I hate that ";
	else
		cout<<"I love that ";

}
}
else{
	for (int i=x;i>0;i--){
	if (i==1)
		cout<<"I hate it ";
	else if (i%2==0)
		cout<<"I love that ";
	else
		cout<<"I hate that ";

}
}*/
/*int x,y,t=240,u=5,sum=0,k=0;
cin>>x>>y;
t=t-y;

for (int i=1;i<=x;i++){

		sum=sum+i*u;
		if (sum<=t)
		k++;

}
cout<<k<<endl;*/
/*int x;
cin>>x;
cout<<(x==2?2:1)<<endl;*/
/*int x,y,sum=0,j,l=0;
cin>>x>>y;
j=x;
l=y;
sum=y;

if (l==x-1){
	cout<<l<<endl;
}
else if (l>=x)
	cout<<x-1<<endl;
else{
for (int i=2;i<=x;i++){
	l--;
	sum=sum+i;
	l++;
	if (x-i==l)
		break;
}
cout<<sum<<endl;
}*/
/*int u1=0,u2=0,u3=0,u4=0,u5=0,ch=0,ce=0,cl=0,cl2=0,co=0;
string s;
cin>>s;
for (int i=0;i<s.length();i++){
	if (s[i]=='h'){
		ch++;
		u1=i;
		break;
	}
}
for (int i=u1+1;i<s.length();i++){
	if (s[i]=='e'){
		u2=i;
		ce++;
		break;
	}
}
for (int i=u2+1;i<s.length();i++){
	if (s[i]=='l'){
		u3=i;
		cl++;
		break;
	}
}
for (int i=u3+1;i<s.length();i++){
	if (s[i]=='l'){
		u4=i;
		cl2++;
		break;
	}
}
for (int i=u4+1;i<s.length();i++){
	if (s[i]=='o'){

		co++;
		break;
	}
}
if (co==1 && ce==1 && cl==1 && cl2==1 && ch==1)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

/*int n,a,b;
cin>>n>>a>>b;
if (b>=0){

	if ((b+a)%n==0)
		cout<<n<<endl;
	else
		cout<<(a+b)%n<<endl;

}
else {

		if(-1*b < a)
		{
			cout<< a-b;
		}
		else
		{
			cout<< n-(((-1*b)-a)%n);
		}

}*/
/*long x,y;
cin>>x>>y;
cout<<y%((long)(pow(2.0,x)))<<endl;*/

/*long long x;
cin>>x;
if (x!=0&&x%2==0)
	cout<<x+1<<endl;
else
	cout<<x/2+1<<endl;*/

/*bool t=false;
long x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
for (int i=0;i<x;i++){
	if (t==true)
		break;
	for (int j=i+1;j<x-1;j++){
		if (y[i]+y[j]>y[j+1]){
			cout<<"YES\n";
			t=true;
			break;
		}
}
}
if (t==false)
	cout<<"NO\n";*/

/*long *y=new long[4];
for (long i=0;i<4;i++){
	cin>>y[i];
}
sort(y,y+4);
for (long i=0;i<3;i++){
	cout<<y[3]-y[i]<<" ";
}*/
/*int n,t,x,z;
cin>>n>>t;
int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
int *o;
while (t--){
	cin>>x>>z;
	o=new int [z];
	for (int i=x;i<z;i++){
		o[i]=y[i];
	}
	sort(o,o+z);
	cout<<o[0]<<endl;
}*/

/*vector <int>y;
int x,q,c=0;
cin>>x;
while (true){
    cin>>q;
    if (q==0)
        break;
	y.push_back(q);
    c++;
}

int n=0,r=c-1,b=0,o=0;

long int u=0;
while (r>n){
    if (u>abs(y[n]+y[r]-x)){
        u=abs(y[n]+y[r]-x);
        b=r;
        o=n;
    }
    if (abs(y[n]+y[r])>x)
        r--;
    else
        n++;
}

cout<<"The closest pair is "<<y[o]<<" and "<<y[b]<<endl;

 long long x,c=0,q;
    cin>>x;
    q=ceil(x/2.0);
    while (q!=0)
    {
        c++;
        q=q/2;
    }
    cout<<c<<endl;*/
/*int x,y,c=0,l;
cin>>x>>y;
l=x;

while (x<y&& y%x==0){
	x=x*l;
	c++;
}
if (x==y)
	cout<<"YES\n"<<c<<endl;
else
	cout<<"NO\n";*/
/*long x,sum=0;
cin>>x;
while (x!=0){
	if (x>=100){
		sum=sum+(x/100);
		x=x%100;
	}

	else if (x>=20){
		sum=sum+(x/20);
		x=x%20;
	}
	else if (x>=10){
		sum=sum+(x/10);
		x=x%10;
	}
	else if (x>=5){
		sum=sum+(x/5);
		x=x%5;
	}
	else{
	sum=sum+x;
	x=0;
	}
}
cout<<sum<<endl;*/
/*int x,c=1,j=0;
cin>>x;
int *y=new int [x];
int *q=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
for (int i=0;i<x;i++){
	q[i]=0;
}
for (int i=0;i<x-1;i++){
	if (y[i]<y[i+1])
		c++;
	else{
		q[j]=c;
		j++;
		c=1;
	}
}
sort(q,q+x);
if (c==x|| c>q[x-1])
	cout<<c<<endl;
else
cout<<q[x-1]<<endl;*/
/*int w,a,b,m=0,l=0,hh=0;

cin>>w>>a>>b;
string x;
cin>>x;
if (((x.length())>w*b) || ((x.length())<w*a) )
	cout<<"No solution\n";
else{
	int m = ((double)x.length() / w), d = x.length() % w;
	for (int i=1;i<=w;i++){
		if (i<w){
			for(int j=0; j<m; j++)
				cout<<x[hh++];
            cout<<endl;
		}
		else
		{
			while (hh<x.length()){
				cout<<x[hh++];
			}
            cout<<endl;

		}
      }

}
*/
/*int x;
cin>>x;
int *y=new int [x];
y[0]=1;
y[1]=1;
for (int i=2;i<x;i++){
	y[i]=y[i-1]+y[i-2];
}
cout<<y[x-1]<<endl;*/
/*int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
long q;
cin>>q;
bool t=false,l=false;
long *u=new long [q];
for (int i=0;i<q;i++){
	cin>>u[i];
}
sort(y,y+x);
long sum=0;
long beg=0,end=x-1;
for (int i=0;i<q;i++){
	cout<<upper_bound(y,y+x,u[i])-y<<endl;
}*/

/*int x,c=0,sum=0;
	vector<int>v;
	cin>>x;
	string s;
	cin>>s;
	for (int i=0;i<x;i++){
		if (s[i]=='x')
			c++;
		else{
			v.push_back(c);
			c=0;
		}
	}
	if (c!=0)
		v.push_back(c);
	for (int i=0;i<v.size();i++){
		if (v[i]>=3)
			sum=sum+(v[i]-2);
	}
	cout<<sum<<endl;*/
/*int x,y;
cin>>x>>y;
int c=1;
vector <int>b(100001);
int *u=new int [y];
int *v=new int [x];
int *p=new int [y];
for (int i=0;i<x;i++){
	cin>>v[i];
}
for (int i=0;i<y;i++){
	cin>>p[i];
}
for (int i=0;i<y;i++){
	c=1;
for (int j=p[i]-1;j<x;j++){
	if (b[v[j]]==0)
	{
		b[v[j]]=c;
		c++;
	}

}
u[i]=c-1;
	fill(b.begin(), b.end(), 0);
}
for (int i=0;i<y;i++){
	cout<<u[i]<<endl;
}
cout<<endl;*/
/*int x,y,z;
cin>>x>>y;
set<int>s1;
int *p=new int[x+1];
int *u=new int [x+1];
for (int i=1;i<=x;i++){
	cin>>u[i];
}

for (int i=x;i>=1;i--){
	s1.insert(u[i]);
	p[i]=s1.size();
}
for (int i=0;i<y;i++){
	cin>>z;
	cout<<p[z]<<endl;
}*/

/*bool o=false;
string x[5]={"S","M","L","XL","XXL"};
int y[5];

for (int i=0;i<5;i++){
	cin>>y[i];
}
int n,l;
cin>>n;
string s;
while (n--){
	o=false;
	cin>>s;

		for (int i=0;i<5;i++)
		{
			if (s==x[i]){
				l=i;
				if (y[l]>0){
					cout<<x[l]<<endl;
					y[l]--;
					o=true;
				}
				else if (l!=0&&l!=4 &&y[l]==0 &&y[l+1]==0&&y[l-1]>0){
					cout<<x[l-1]<<endl;
					y[l-1]--;
					o=true;
				}
				else{
				for (int j=l;j<5;j++)
				{
					if (y[j]>0){
						cout<<x[j]<<endl;
						y[j]--;
						o=true;
						break;
					}
				}
				}

		if (o==false){
			for (int j=l;j>=0;j--)
				{
					if (y[j]>0){
						y[j]--;
						cout<<x[j]<<endl;

						break;
					}
				}
		}
			}
		}

}*/
/*int c=0,sum=0;
long x;

scanf("%d",&x);
long *y=new long [x];
for (int i=0;i<x;i++){
	scanf("%d",&y[i]);
}
sort(y,y+x);
for (int i=0;i<x;i++){
	if (sum<=y[i])
		sum=sum+y[i];
	else
		c++;
}
printf("%d\n",x-c);*/
/*int x,y,max=1,c=1;
scanf("%d",&x);
scanf("%d",&y);
int *q=new int [x];
for (int i=0;i<x;i++){
	scanf("%d",&q[i]);
}
for (int i=0;i<x-1;i++){
	if (q[i]!=q[i+1])
		c++;
	else{
		if (max<c)
			max=c;
		c=1;
	}
}
if (c>max)
	cout<<c<<endl;
else
	cout<<max<<endl;*/

/*int x,max=0,j=0;
cin>>x;
int *y=new int [x];
int q[5]={0,0,0,0,0};
for (int i=0;i<x;i++){
	cin>>y[i];
	q[y[i]]++;
}
for (int i=0;i<5;i++){
	if (max<q[i]){
		max=q[i];
		j=i+1;
	}
}
cout<<j<<endl;*/
/*string  numbers[] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
        "twenty",
        "twenty one",
        "twenty two",
        "twenty three",
        "twenty four",
        "twenty five",
        "twenty six",
        "twenty seven",
        "twenty eight",
        "twenty nine"
    };
int h,m;
cin>>h>>m;
if (m==0)
	cout<<numbers[h]<<" o' clock\n";
else if (m==15)
	cout<<"quarter past "<<numbers[h]<<endl;
else if (m<30)
	cout<<numbers[m]<<(m==1?" minute past ":" minutes past ")<<numbers[h]<<endl;
else if (m==30)
	cout<<"It's half past "<<numbers[h]<<endl;
else if (m==45)
	cout<<"It's quarter to "<<numbers[h]<<endl;
else
	cout<<"It's "<<numbers[60-m]<<" to "<<numbers[h]<<endl;*/
/*bool cab=false,cba=false,k=false;
string s;
cin>>s;
for (int i=0;i<s.length()-1;i++){

	if (s[i]=='A' && s[i+1]=='B'){
		cab=true;
		if (i<s.length()-2)
		i=i+2;
		k=true;
	}

	if (k==true&&s[i]=='B' && s[i+1]=='A'){
		cba=true;
		i=i+2;
}

}
	if (cab ==true && cba ==true){
		cout<<"YES\n";

	}
	else{
		cba=false;
		cab=false;
		k=false;
		for (int i=0;i<s.length()-1;i++){

	if (s[i]=='B' && s[i+1]=='A'){
		cba=true;
		if (i<s.length()-2)
		i=i+2;
		k=true;
	}

	if (k==true&&s[i]=='A' && s[i+1]=='B'){
		cab=true;
		i=i+2;
}

}

	if (cab ==true && cba ==true)
		cout<<"YES\n";
	else{
		cout<<"NO\n";

	}
	}*/
/*int be=0,en=10;
int x,z;
bool t=false;

int y[11]={0,0,0,0,0,0,0,0,0,0,0};
for (int i=0;i<6;i++){
	cin>>x;
	y[x]++;
}

for (int i=0;i<11;i++){

if (y[i]>=4){
	z=i;
	t=true;
}
}

if (t==true)
{
	for (int i=0;i<11;i++){
		if (y[i]==2 ||y[i]==6){
		cout<<"Elephant\n";
		be=1;
		break;
		}

	}
	if (be==0)
		cout<<"Bear\n";
}
else
	cout<<"Alien\n";*/

/*long long x,y=2520,c=0;
cin>>x;
c=x/y;
cout<<c<<endl;*/
/*long sum=0;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
for (int i=x-1;i>=0;i--){
	sum=sum+(pow(2.0,x-1-i)*y[i]);
}
cout<<sum<<endl;*/
/*int sum=0;
int x,k;
cin>>x>>k;
vector<pair<int ,int >>v(x);
for (int i=0;i<x;i++){
	cin>>v[i].first;
	cin>>v[i].second;
}
sort(v.begin(),v.end());
/*for (int i=0;i<x;i++){
	cout<<v[i].first<<"        "<<v[i].second<<endl;
}*/

/*for (int i=x-1;i>=0;i--){
	if (v[i].second==0)
		sum=sum+v[i].first;
	else{
		if (k>0){
		k--;
		sum=sum+v[i].first;
		}
		else{
			sum=sum-v[i].first;
		}

	}
}
cout<<sum<<endl;*/

/*int x;
cin>>x;
string *y=new string [x];

set <string> s;
for (int i=0;i<x;i++)
	cin>>y[i];
cout<<endl;
for (int i=x-1;i>=0;i--){
	if (s.count(y[i])==0)
	cout<<y[i]<<endl;
	s.insert(y[i]);
}*/

/*bool b=false;
int x;
vector <int>v;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}

for (int i=0;i<x-1;i++){
	for (int j=i+1;j<x;j++){
		if (y[i]==y[j]){
			b=true;
			v.push_back(j-i);
		}
	}
}
if (b==true){
sort(v.begin(),v.end());
cout<<v.at(0)<<endl;
}
else
	cout<<-1<<endl;*/

/*int x,k,c=0;
cin>>x;
int *y=new int [x];
vector <int >v;
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
while (y[x-1]!=0){
	c=0;
	for (int i=0;i<x;i++){
		if (y[i]>0){
			k=y[i];
			break;
		}
	}
	for (int i=0;i<x;i++){
		y[i]=y[i]-k;
	}
	for (int i=0;i<x;i++){
		if (y[i]>0)
			c++;
	}
	v.push_back(c);
}
cout<<x<<endl;
for (auto i=v.begin();i!=v.end()-1;i++){
	cout<<*i<<endl;
}*/

/*
int x;
cin>>x;

int** a = new int*[x];
for(int i = 0; i < x; ++i)
    a[i] = new int[x];

for (int i=0;i<x;i++){
	for (int j=0;j<x;j++){
		scanf("%1d", &a[i][j]);
	}
}
for (int i=0;i<x;i++){
	for (int j=0;j<x;j++){
		if ((i>=1)&&(i<x-1) && (j>=1)&&(j<x-1)){
		if ( a[i][j] > a[i-1][j] &&
             a[i][j] > a[i+1][j] &&
             a[i][j] > a[i][j-1] &&
             a[i][j] > a[i][j+1])
              printf("X");
         else
            printf("%d",a[i][j]);
		}
		else
			 printf("%d",a[i][j]);
	}

	printf("\n");
}
*/

/*int n=2;
int x,y;
cin>>x>>y;
if (x<y && x!=y && y%2!=0)
	cout<<"No\n";
else if ((x==y && y%2!=0))
	cout<<"Yes\n";
else if (x>y && x%y==0)
	cout<<"Yes\n";
else{
	for (int i=1;;i++)
	{
		n=pow(2.0,i);
		if (n==y){
			cout<<"Yes\n";
			break;
		}
		else if (n>y){
			if ( y==x || y==(x*2) ||(x==3 && y%x==0)){
				cout<<"Yes\n";
				break;
			}
			else{
				cout<<"No\n";
				break;
			}
		}
	}
}*/

/*int x,y,z;
cin>>x>>y>>z;
if (x>y+z)
	cout<<"+"<<endl;
else if (y>x+z)
	cout<<"-"<<endl;
else if (y==x+z && x==y+z )
	cout<<"0"<<endl;
else
	cout<<"?"<<endl;*/

/*bool v=false;
string suit="DCSH";
string rank="23456789TJQKA";
string t,h;
cin>>t;
for (int i=0;i<5;i++){
	cin>>h;
	if (h[0]==t[0] ||h[1]==t[1])
		v=true;
}
if (v)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

/*string s,s1,s2;
cin>>s;
for (int i=0;i<s.length();i++){
	s1=s1+s[i];
	if (s[i]!='a')
		s2=s2+s[i];
	if (s1.length()+s2.length()==s.length())
		break;
}
if (s1+s2==s)
	cout<<s1<<endl;
else
	cout<<":(\n";*/

/*long x,k;
cin>>x>>k;
while (k--){
	if (x%10>0)
		x--;
	else
		x/=10;
}
cout<<x<<endl;*/

/*bool t=false;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
	if (y[i]==1)
		t=true;
}
if (t)
	cout<<"HARD\n";
else
	cout<<"EASY\n";*/

/*int sum=0;
int x;
scanf("%d",&x);
string y;
while (x--){
	cin>>y;
	if (y=="Icosahedron")
		sum=sum+20;
	else if (y=="Cube")
		sum=sum+6;
	else if (y=="Tetrahedron")
		sum=sum+4;
	else if (y=="Dodecahedron")
		sum=sum+12;
	else
		sum=sum+8;
}
printf("%d\n",sum);*/

/*int c=0;
int x;
cin>>x;
for (int i=1;i<x;i++){
	if (x%i==0)
		c++;
}
cout<<c<<endl;*/

/*
long long n,x,y,sum=0;
cin>>n;

while (n--){
cin>>x;
cin>>y;
	sum=0;
while (true){
	if (x==0)
		break;
	if (x%y==0){
		sum++;
		x=x/y;
	}
	else{
		sum=sum+x%y;
		x=x-x%y;
	}
}
cout<<sum<<endl;
}
*/

/*int x;
cin>>x;
if (x%2==0)
	cout<<"Mahmoud\n";
else
	cout<<"Ehab\n";*/

/*int san=0,sea=0;
int x;
cin>>x;
string n;
cin>>n;
for (int i=0;i<n.length()-1;i++){
	if (n[i]=='S' && n[i+1]=='F')
		san++;
	else if (n[i]=='F' && n[i+1]=='S')
		sea++;
}
if (san!=0&&san>sea)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

   /*long double x,y,z,c=0;
    cin>>x>>y;
    z=(int)ceil(y/x);
    cout<<fixed<<setprecision(0)<<z<<endl;*/

/*bool t=true;
  long long n,k,sum=0,c=1,h=0;
  cin>>n>>k;
  sum=k;
  if (n==1)
      cout<<0<<endl;
  else if ((2+k)*(k-1)/2-(k-2)<n)

        cout<<"-1"<<endl;
  else
  {
	  k--;
	  n--;
     long long r=k;
	 long long l=0;
	long  long mid=(r+l)/2;
	  while (l<r){
		  mid=(r+l)/2;
		  sum=((((k*(k+1))/2))-(((mid*(mid+1))/2)));
		  if (n<sum)
			  l=mid+1;
		  else if (n>sum)
			  r=mid;
		  else
		  {
			  t=false;
			  cout<<k-mid<<endl;
			  break;
		  }
	  }
	  if (t&&sum>n){
		  cout<<k-mid<<endl;

	  }
	  else if (t && sum<n){
		  cout<<k-mid+1<<endl;

	  }

  }
  */

/*int x;
cin>>x;
int** a = new int*[x];
for(int i = 0; i < x; i++)
    a[i] = new int[x];
for(int i = 0; i< x;i++){
	for(int j = 0; j< x;j++){
		if (i==0 ||j==0)
			a[i][j]=1;
		else
			a[i][j]=a[i-1][j]+a[i][j-1];
	}
}
cout<<a[x-1][x-1]<<endl;*/

/*int x,t,z,cm=0,cc=0;
cin>>x;
while (x--){
	cin>>t>>z;
	if (t>z)
		cm++;
	else if (t<z)
		cc++;
}
if (cm>cc)
	cout<<"Mishka\n";
else if (cc>cm)
	cout<<"Chris\n";
else
	cout<<"Friendship is magic!^^\n";*/

/*
int x[4],sum=0;
for (int i=0;i<4;i++){
	cin>>x[i];
}
string s;
cin>>s;
for (int i=0;i<s.length();i++){
	sum=sum+x[s[i]-'1'];
}
cout<<sum<<endl;*/

/*long long a,b,k;
int x;
cin>>x;
while (x--){
cin>>a>>b>>k;

	cout<<fixed<<setprecision(0)<<(a*(k/2+k%2))-(b*(k/2))<<endl;
}*/
/*
int x;
cin>>x;
long long y,sum=0;
while (x--){
	sum=0;
	cin>>y;
	while (y>1){
		if (y%2==0){
			y=y/2;
			sum++;
		}
		else if (y%3==0){
			y=y/3*2;
			sum++;
		}
		else if (y%5==0)
		{
			y=y/5*4;
			sum++;
		}
		else
			break;
	}

	if (y==1)
		cout<<sum<<endl;
	else
		cout<<-1<<endl;
}*/

/*
int sum1=0,sum2=0;
int s,v1,v2,t1,t2;
cin>>s>>v1>>v2>>t1>>t2;
sum1=2*t1+(v1*s);
sum2=2*t2+(v2*s);
if (sum1<sum2)
	cout<<"First\n";
else if (sum1>sum2)
	cout<<"Second\n";
else
	cout<<"Friendship\n";*/

/*bool t=false;
int x;
cin>>x;
int*y=new int [x];
int q[10];
for (int i=0;i<x;i++){
	cin>>y[i];
}
for (int i=0;i<10;i++){

	cin>>q[i];
}
sort(y,y+x);

for (int i=0;i<10;i++){
	if (y[0]>=q[i])
		t=true;
}
if (t)
	cout<<10<<endl;
else
	cout<<y[0]<<endl*/

/*
int x,sum=0,l=0;
cin>>x;
for (int i=1;;i++){
	sum=sum+(i*(i+1))/2;
	if (sum==x){

		cout<<i<<endl;
		break;
	}
	else if (sum>x){

		cout<<i-1<<endl;
		break;
	}
}*/

/*
long long x,y,z,c=0;
cin>>x>>y>>z;
if (x+y<z)
	cout<<0<<" "<<0<<endl;
else if (x%z==0 || y%z==0||(x%z+y%z)<z)
	cout<<((x+y)/z)<<" "<<0<<endl;
else  {
	cout<<((x+y)/z)<<" "<<min(z-(y%z),z-(x%z))<<endl;
}*/

/*
int x,t1,t2,p,sum=0;
cin>>x>>t1>>t2;
t2=2*t2;
for (int i=0;i<x;i++){
	cin>>p;
	if (p==2)
	{
		if (t2%2==0)
		t2=t2-p;
		else
			sum=sum+p;
	}
	if (p==1){
		if (t1>0)
			t1--;
		else if (t2>0)
			t2--;
		else
			sum++;
	}
}
cout<<sum<<endl;*/

/*
bool p2=true;
bool p=true;
int k=2;
int x,i,l=0;
cin>>x;
if ((x*1+1)%2==0&& x!=1)
	cout<<1<<endl;
else{
while (true){
	p2=true;
	p=true;
for ( i=2;i<k;i++){
	if (k%i==0)
		p=false;
}
if (i==k)
	p=true;
if (p){
	l=x*k+1;
	for (int i=2;i<l;i++){
		if (l%i==0){
			cout<<k<<endl;
			p2=false;
			break;
		}
	}
	}
if (!p2)
	break;
	else
		k++;
}
}*/

/*
int n,p,k,pl,pr,pr2,pl2;
cin>>n>>p>>k;
pl=p-k;
pl2=pl;
pr=p+1;
pr2=pr-1;

	if (pl>1){
		cout<<"<< ";

	while (pl<p){
		cout<<pl<<" ";
		pl++;
	}
	cout<<"("<<p<<") ";
	if (pr2+k<n){
		while (pr<=pr2+k){
			cout<<pr<<" ";
			pr++;
		}
		cout<<">>";
	}
	else {
		while (pr<=n){
			cout<<pr<<" ";
			pr++;
		}
	}
	cout<<endl;
	}
	else{
		pl=1;
		pl2=1;
		while (pl<p)
		{
			cout<<pl<<" ";
			pl++;
		}
		cout<<"("<<p<<") ";
		if (pr2+k<n){
		while (pr<=pr2+k){
			cout<<pr<<" ";
			pr++;
		}
		cout<<">>";
	}
		else{

		while (pr<=n){
			cout<<pr<<" ";
			pr++;
		}
	}
	cout<<endl;
	}*/

/*
bool b=true;
int x,l;
cin>>x>>l;
int *y=new int [x];
for (int i=0;i<x;i++)
	cin>>y[i];
for (int i=1;i<x;i++)
{
	if (y[i-1]>=y[i]){
		cout<<i+1<<endl;
		b=false;
		break;
	}
}
if (b)
	cout<<0<<endl;*/

/*Books

int sum=0,c=0,i,cf=0;
int r,l,mid=0;
int n,t;
cin>>n>>t;
l=0;
r=n-1;

int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
while (l<r){
	sum=0;
	mid=(l+r)/2;
	for (i=mid;i<n;i++){
		sum=sum+y[i];
	}
	c=n-mid;
	if (sum>t){
		l++;
	}
	else{
		r--;
		if (c>cf)
			cf=c;
	}
}

if (cf==0 &&t>=y[0])
	cout<<1<<endl;
else
	cout<<cf<<endl;*/

int a,b,c,d,sum=0;
cin>>a>>b>>c>>d;

	if ((abs(c-b)>=d && abs(c-a)>=d && abs(a-b)<d))
	{
		sum=sum+d-abs(a-b);
	}
	else if ((abs(c-b)>=d&& abs(b-a)>=d && abs(c-a)<d)){
		sum=sum+d-abs(c-a);
	}
	else if ((abs(c-a)>=d&& abs(b-a)>=d && abs(c-b)<d)){
		sum=sum+d-abs(c-b);
	}
	else if (abs(c-b)>=d && abs(c-a)>=d && abs(a-b)<d)
		sum=sum+abs(c-a)+abs(b-a);

	else if ((abs(c-b)<d&& abs(b-a)>=d && abs(c-a)<d)){
		sum=sum+abs(a-c)+abs(b-c);
	}
	else if ((abs(c-b)>=d&& abs(b-a)<d && abs(c-a)<d)){
		sum=sum+abs(c-a)+abs(a-b);
	}
	else if ((abs(c-b)<d&& abs(b-a)<d && abs(c-a)>=d)){
		sum=sum+abs(c-b)+abs(a-b);
	}
	else if ((abs(c-b)>=d && abs(c-a)>=d && abs(a-b)>=d))
		sum=0;
	else
	{
		int mx = max(a,b);
mx = max(mx, c);
int mn=min(a,b);
mn=min(mn,c);

if (mx== a && mn==b)
	sum=sum+(d-(a-c))+(d-(c-b));
else if (mx==a && mn==c)
	sum=sum+(d-(a-b))+(d-(b-c));
else if (mx==b && mn==a)
	sum=sum+(d-(b-c))+(d-(c-a));
else if (mx==b && mn==c)
	sum=sum+(d-(b-a))+(d-(a-c));
else if (mx==c && mn==a)
	sum=sum+(d-(c-b))+(d-(b-a));
else if (mx==c && mn==b)
	sum=sum+(d-(c-a))+(d-(a-b));
	}
	cout<<sum<<endl;

    return 0;
}

#include<stdio.h>
int main(){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
int min,max,ort;
if(a>b && a>c){
    max=a;
    if(b>c){
        ort=b;
        min=c;
    }
    else{
        ort=c;
        min=b;
    }
}
else if(b>a && b>c){
    max=b;
    if(a>c){
        ort=a;
        min=c;
    }
    else{
        ort=c;
        min=a;
    }
}
else{
    max=c;
    if(a>b){
        ort=a;
        min=b;
    }
    else{
        ort=b;
        min=a;
    }
}
int ans=0;
if(max-ort<d) ans+=d-(max-ort);
if(ort-min<d) ans+=d-(ort-min);
printf("%d",ans);
return 0;
}

#include<bits/stdc++.h>
using namespace std;

int main(){
  int n,m,i,j=0,count=0,diff1,diff2;
  int a,b,c,d;
  cin>>a>>b>>c>>d;
  diff1 = b-a;
  diff2 = c-b;
  if(abs(diff1)<d && abs(diff2)<d){
    cout<<2*d-diff1-diff2<<endl;
  }
  else if(abs(diff1<d)){
    cout<<d-diff1<<endl;
  }
  else if(abs(diff2)<d){
    cout<<d-diff2<<endl;
  }
  else{
    cout<<0<<endl;
  }
  return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()

{
   int a,b,sub,sub1,sub2,c,d,cont=0,cont1=0,temp,i,j;
   int arr[10];
   for(i=0;i<3;i++)
   {
       cin>>arr[i];
   }
   cin>>d;
   for(i=0;i<3;i++)
   {
       for(j=0;j<3;j++)
       if(arr[j]>arr[j+1])
       {
        swap(arr[j],arr[j+1]);
       }
   }

  a=arr[0];
  b=arr[1];
 c=arr[2];
  sub=b-a;
  sub1=c-b;
  sub2=sub+sub1;

  if(sub>=d)
  {
      cont=cont+0;

  }
  else
  {
     int sub5=sub-d;
     cont1=-1*sub5;

  }

    if(sub1>=d)
  {
      cont=cont+0;

  }
  else
  {
     cont=cont+d-sub1;
  }
    if(sub2+cont1>=d)
  {
      cont=cont+0;

  }
  else
  {
     cont=cont+d-sub2;
  }

  cout<<cont+cont1<<endl;
cont=0;

}

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e3 + 10;
const int inf = 0x3f3f3f3f;
typedef long long ll;
ll a[10], d;
int main()
{
	cin >> a[1] >> a[2] >> a[3] >> d;
	sort(a + 1, a + 4);
	ll ans = 0;
	ans += max(0LL, 2 * d - (a[3] - a[1]));
	if (a[3] - a[1] >=2*d)
	{
		ans += min(abs(a[3] - d - a[2]), abs(a[1] + d - a[2]));
	}
	else{
		ll mid = (a[3] + a[1]) / 2;
		if (a[2] == mid){
			ans += 0;
		}
		else if (a[2] > mid)
		{
			ans += abs(a[1] + d - a[2]);
		}
		else
		{
			ans += abs(a[3] - d - a[2]);
		}
	}
	cout << ans << endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const LL INF = 3e9 + 5;

LL a[3], d, b[2], tmp, ans = INF;

void solve() {
	scanf("%I64d%I64d%I64d%I64d", a, a + 1, a + 2, &d);
	for (int i = 0; i < 3; i++) {
		//b[0] = a[i];
		for (int j = 0; j < 3; j++) {
			if (j == i) continue;
			tmp = 0;
			b[0] = a[i];
			LL d1 = abs(b[0] + d - a[j]), d2 = abs(b[0] - d - a[j]);
			//cout<<d1<<" "<<d2<<endl;
			if (d1 < d2) tmp += d1, b[1] = b[0] + d;
			else tmp += d2, b[1] = b[0] - d, swap(b[0], b[1]);
			int k = 3 - i - j;
			d1 = abs(b[0] - d - a[k]), d2 = abs(b[1] + d - a[k]);
			//cout<<d1<<" "<<d2<<endl;
			if (d1 < d2) tmp += d1;
			else tmp += d2;
			//cout<<"tmp"<<tmp<<endl;
			ans = min(ans, tmp);
		}
	}
	printf("%I64d\n", ans);
}

int main() {
	//freopen("in.txt", "r", stdin);
	solve();
	return 0;
}

#include<bits/stdc++.h>
using namespace std;

const int MAXN = 1e5+5;
int INF = 1e9+7;
int a[MAXN];

int main()
{
    for(int i=0;i<3;++i) cin >> a[i];
    int mins = INF;
    int b[3];b[0]=a[0];b[1]=a[1];b[2]=a[2];
    sort(a,a+3);int d;cin >> d;int res=0;
    if(a[1]<a[0]+d) {res+=a[0]+d-a[1];a[1]=a[0]+d;}
    sort(a,a+3);
    if(a[2]<a[1]+d) {res+=a[1]+d-a[2];}mins=res;res=0;
    if(a[1]<a[0]+d) {res+=a[0]+d-a[1];}
    if(a[2]<a[1]+d) {res+=a[1]+d-a[1];}mins = min(mins,res);
    cout << mins;;
}

#include<bits/stdc++.h>
using namespace std;
int check(long long int a[],long long int d){
	if((a[2]-a[1]>=d) && (a[1]-a[0]>=d))
	return 1;
	else
	return 0;
}
int main(){
	long long int a[3],d;
	cin>>a[0]>>a[1]>>a[2]>>d;
	sort(a,a+3);
	int mean=(a[2]+a[0])/2;
	if(check(a,d)==1)
	cout<<0;
	else{
		if(a[2]-a[0]<(2*d))
			cout<<(d)-min((a[2]-a[1]),(a[1]-a[0]));
		else if(a[2]-a[0]==(2*d))
			cout<<abs(mean-a[1]);
		else
			cout<<d;
	}
}

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    int arr[3],d;

    for(int i = 0;i<3;i++)
    cin>>arr[i];
    cin>>d;
    sort(arr,arr+3);

    int s1 = arr[0] - arr[1] + d <= 0? 0 :arr[0] - arr[1] + d;
    int s2 = arr[1] - arr[2] + d <= 0? 0 :arr[2] - arr[1] - d;
    int sec = s1 + s2;
    cout<<sec;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,c,d,ans=0,mn,mx,se;
    cin>>a>>b>>c>>d;
    mn=min(min(a,b),c);
    mx=max(max(a,b),c);
    se=max(min(a,b),c);

    if(abs(mn-se)<d)
        ans+=abs(d-abs(mn-se));
    if(abs(mx-se)<d)
        ans+= abs(d-abs(mx-se));
    cout<<ans<<endl;
  /*  if(abs(a-b)<d)
        ans+=abs(d-abs(a-b));
    else if(abs(a-c)<d)
        ans+=abs(d-abs(a-c));
    else if(abs(b-c)<d)
        ans+=abs(d-abs(b-c));
    cout<<ans<<endl;*/
}

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int a[3],b,c,d,sum=0;
    cin>>a[0]>>a[1]>>a[2]>>d;
    sort(a,a+3);
    if(a[1]-a[0]<d);
    sum+=d-(a[1]-a[0]);
    if(a[2]-a[1]<d);
    sum+=d-(a[2]-a[1]);
    cout<<sum<<endl;
    return 0;
}

#include <bits/stdc++.h>
typedef long long lli;
using namespace std;

int main() {
    lli a[3],d,p=0,q=0;cin>>a[0]>>a[1]>>a[2]>>d;
    sort(a,a+3);
    if(abs(a[2]-a[0])<2*d)
    p=abs((a[2]-a[0])-2*d);
    q=abs((a[1]-a[0])-d);
    if(p>=q)
    cout<<p<<endl;
    else
    cout<<q<<endl;
	return 0;
}

//
// Created by kiruha on 18.06.19.
//
#include <iostream>
#include <vector>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>

using namespace std;

#define forn(i, n) for (int i = 0; i < static_cast<int>(n); ++i)
#define sz(a) static_cast<int>((a).size())
#define f first
#define s second

#ifdef KIRYA
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define dbg(...)
#endif

template<typename T>
void _dbg(const char* _s, T _h) {
    cerr << _s << " = " << _h << "\n";
}

template<typename T, typename... Ts>
void _dbg(const char*_s, T _h, Ts... _t) {
    int _b = 0;

    while (((_b += *_s == '(') -= *_s == ')') != 0 || *_s != ',')
        cerr << *_s++;

    cerr << " = " << _h << ",";
    _dbg(_s + 1, _t...);
}

int main() {
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios_base::sync_with_stdio(false);

    vector<int64_t> vec(3);
    int64_t d;
    cin >> vec[0] >> vec[1] >> vec[2] >> d;
    sort(vec.begin(), vec.end());
    cout << min(max(d - (vec[1] - vec[0]), (int64_t)0) + max(d - (vec[2] - vec[1]), (int64_t)0),
                2 * d - (vec[2] - vec[0]) + abs(((vec[2] + vec[0]) / 2) - vec[1])) << '\n';
}

//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define ll              long long
#define ull             unsigned long long
#define pb              push_back
#define fastread()      (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
int main()
{
    fastread();
    ll a,b,c,d,e = 0,f = 0;
    cin>>a>>b>>c>>d;
    if(a > b){
        swap(a,b);
        if(a > c){
            swap(a,c);
        }
    }
    if(b > c){
        swap(b,c);
    }
    ll ans = 0;
    e = b - a;
    f = c - b;
    if(e < d){
        ans += d - e;
    }
    if(f < d){
        ans += d - f;
    }
    cout<<ans<<endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;
#define cinfast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);srand(time(NULL))
#define coutfix(A) cout<<fixed<<setprecision(A)
#define pb push_back
#define sc second()
#define fr first()
#define dbg(x) cerr << #x << " = " << x << endl

#define N 100100
typedef long long ll;

ll a[4];
int main()
{
   cinfast;
   ll d;
   cin>>a[0]>>a[1]>>a[2]>>d;
   sort(a,a+3);
   ll ans=0;
   if(a[1]-a[0]<d)
   {

       ans+=d-(a[1]-a[0]);
         a[1]+=ans;
   }
   if(abs(a[2]-a[1])<d)
   ans+=d-abs(a[2]-a[1]);

   cout<<ans<<endl;

    return 0;
}

#include <stdio.h>
#include <algorithm>
using namespace std ;

int main() {
int a, b, c, d;
scanf("%d %d %d %d", &a, &b, &c, &d);
if (b > c) swap(b, c);
if (a > b) swap(a, b);
if (b - a >= d)
{
if (c - b >= d) return puts("0");
printf("%d\n", d - (c - b));
return 0;
}
if (c - b >= d)
{
printf("%d\n", d - (b - a));
return 0;
}
printf("%d\n", d - (b - a) + d - (c - b));
}

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define ft first
#define sd second
using namespace std;

ll a, b, c, d;
ll x[4];

int main()
{
    scanf("%lld %lld %lld %lld",&x[0],&x[1],&x[2],&d);
    sort( x, x + 3 );
    a = x[0];
    b = x[1];
    c = x[2];

    if( b - a >= d && c - d  >= d ){
        printf("0");
        return 0;
    }
    ll ans1 = d + a - b;
    ll ans2 = d + b + ans1 - c;
    printf("%lld", max( ans1, ans2));
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main ()
{
	int a,b,c,d,d1,d2,d3;
	cin>>a>>b>>c>>d;
	if(a>=b && a>=c)
	{
		int temp = c;
		c = a;
		a = temp;
		if (a > b)
		{
			int temp = a;
			 a = b;
			 b = temp;
			 //cout<<a<<b<<c<<"\n";
		}

	}
	else if ( b>=c && b>=a)
	{
		int temp = c;
		c = b;
		b = temp;

		if (a > b)
		{
			int temp = a;
			 a = b;
			 b = temp;
			 //cout<<a<<b<<c<<"\n";
		}
	}

    else  if ( a > b )
	{
		int temp = a;
		 a = b;
		 b = temp;
	}
	//cout<<a<<" "<<b<<" "<<c<<"\n";

	d1 = (b-a);
	d2 = (c-b);
	d3 = (c-a);

	if((d1 >= d && d2 >= d) && d3 >= d )
		cout<<0;
	else if( d3 >= 2*d && d1 < d)
	{
		cout<<d-d1;
	}
	else if( d3 >= 2*d && d2 < d)
	{
		cout<<d-d2;
	}

	else
		cout<<2*d-d1-d2;

}

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long int a,b,c,d,t,count=0;
    cin>>a>>b>>c>>d;
    if(a>b)
    {
        t=a;a=b;b=t;
    }
    if(b>c)
    {
        t=b;b=c;c=t;
    }

    if(c-b<d)   count+=d-c+b;
    if(b-a<b)   count+=d-b+a;

    cout<<count;

    return 0;
}

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define vfor(v) for(auto &&x: v)
#define pb push_back
using namespace std;
typedef long long ll;

void solve(){
    ll a[3],t,g=0,p;

    for(ll x=0;x<3;x++){
        cin>>a[x];
    }
    cin>>p;
    sort(a,a+3);

    t=abs(a[0]-a[1]);
    if(t<p){g+=abs(p-t);a[1]+=abs(p-t);}
    t=abs(a[1]-a[2]);
    if(t<p){g+=abs(p-t);a[2]+=abs(p-t);}
    t=abs(a[0]-a[1]);
    if(t<p){g+=abs(p-t);a[2]+=abs(p-t);}
    cout<<g;
}

int main(){
    IOS;
    solve();
    return 0;
}

#include <iostream>
#include<math.h>
#include<algorithm>
using namespace std;

int main()
{
  int a,b,c,d;
  cin>>a>>b>>c>>d;
  int l =abs(a-b);
  int m= abs(a-c);
  int n=abs(b-c);
  if( l >=d &&  m>=d &&  n>=d)
  {
      cout<<"0"<<endl;
  }
  else
  {
      if(a>b && a>c)
      { int t1=0,t2=0;
          if(b>c)
          {
              if(abs(a-b)<d)
              {
                   t1=d-abs(a-b);
              }
              if(abs(b-c)<d)
              {
                   t2=d-abs(b-c);
              }
              cout<<t1+t2<<endl;
          }
          if(c>b)
          {
              if(abs(a-c)<d)
              {
                  int t1=d-abs(a-c);
              }
              if(abs(b-c)<d)
              {
                  int t2=d-abs(b-c);
              }
              cout<<t1+t2<<endl;
          }
      }
      if(b>a && b>c)
      {
          int t1=0,t2=0;
          if(a>c)
          {
              if(abs(a-b)<d)
              {
                  int t1=d-abs(a-b);
              }
              if(abs(b-c)<d)
              {
                  int t2=d-abs(b-c);
              }
              cout<<t1+t2<<endl;
          }
          if(c>a)
          {
              if(abs(a-c)<d)
              {
                  int t1=d-abs(a-c);
              }
              if(abs(b-c)<d)
              {
                  int t2=d-abs(b-c);
              }
              cout<<t1+t2<<endl;
          }
      }

      if(c>a && c>b)
      {
          int t1=0,t2=0;
          if(b>a)
          {
              if(abs(a-b)<d)
              {
                  t1=d-abs(a-b);
              }
              if(abs(b-c)<d)
              {
                   t2=d-abs(b-c);
              }
              cout<<t1+t2<<endl;
          }
          if(a>b)
          {
              if(abs(a-c)<d)
              {
                   t1=d-abs(a-c);
              }
              if(abs(b-c)<d)
              {
                   t2=d-abs(b-c);
              }
              cout<<t1+t2<<endl;
          }
      }
  }
    return 0;
}

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <stdlib.h>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1<<30;
const LL maxn = 1e5+10;

int n[3], d;
int main()
{
    cin >> n[0] >> n[1] >> n[2] >> d;
    sort(n, n+3);
    int ans = inf;

    if((n[1]-n[0])+(n[2]-n[1])>=2*d)
        ans = min(abs(d-(n[1]-n[0])), abs(d-(n[2]-n[1])));

    int tmp = 0;
    if(n[1]-n[0] < d) tmp += d-(n[1]-n[0]);
    if(n[2]-n[1] < d) tmp += d-(n[2]-n[1]);
    ans = min(ans, tmp);

    cout << ans << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,ara[1000];
    cin>>ara[1]>>ara[2]>>ara[0]>>d;
    sort(ara,ara+3);
    long long int no=abs(ara[0]+d-ara[1]);
    long long int no1=abs(ara[0]+2*d-ara[2]);
    cout<<max(no,no1)<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define cool ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
int main()
{
    cool;
    long long int a,b,c,d,ans=0;
    cin>>a>>b>>c>>d;
    if(abs(a-b)<d)
    {
    	ans+=(d-(abs(a-b)));
    	if(abs(a-c)<d)
    	ans+=(d-(abs(a-c)));
	}
	else if(abs(b-c)<d)
	{
		ans+=(d-(abs(b-c)));
		if(abs(a-c)<d)
		ans+=(d-(abs(a-c)));
	}
	else if(abs(a-c)<d)
	ans+=(d-(abs(a-c)));
	cout<<ans;
	return 0;

}

#include<bits/stdc++.h>
#define INF 2000000000
#define in(x) scanf("%d",&x);
#define vi vector<int>::iterator
#define si set<int>::iterator
//#define USE_IOSTREAM true
#define DEBUG_MODE true
#define fori(i,n) for(int i=0;i<(n);i++)
#define forit(it,x) for(vi it=(x).begin();it!=(x).end();it++) // change vi to si is okay
#define print(x) printf("%d\n",x);

#ifdef DEBUG_MODE
    #define showvec(x) showvec_active(x);
    #define showarray(x,n) showarray_active(x,n);
    #define show(x) printf(#x": %d\n",x);
#else
    #define showvec(x)
    #define showarray(x,n)
    #define show(x)
#endif // DEBUG_MODE
using namespace std;
void arrclear(int *x,int n)
{
memset(x,0,sizeof(int)*n);
}
void showarray_active(int *x,int n)
{

            printf("@ %d:",x);
        for(int i=0;i<n;i++)
            printf("%d ",x[i]);
        printf("\n");

}
void showvec_active(vector<int> x)
{
        printf("@%d: ",x);
        for(vi it=x.begin(); it!=x.end();it++)
            printf("%d ",*it);
        printf("\n");

}

int main()
{
    #ifdef USE_IOSTREAM
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
    #endif // USE_IOSTREAM
int a,b,c,d;
int temp;
    in(a);in(b);in(c);in(d);
    if(a>b)
    {
        temp=b;
        b=a;
        a=temp;
    }
    if(a>c)
    {
        temp=c;
        c=a;
        a=temp;
    }
    if(b>c)
    {
        temp=b;
        b=c;
        c=temp;
    }
    //printf("%d %d %d",a,b,c);
    //fix a
    int fixa=0;
    fixa+=(b-a>d)?0:d-(b-a);
    int nextpos=(b-a>d)?b:a+d;
    fixa+=(c-nextpos>d)?0:d-(c-nextpos);
        int fixc=0;
    fixc+=(c-b>d)?0:d-(c-b);
    nextpos=(c-b>d)?b:c-d;
    fixc+=(nextpos-a>d)?0:d-(nextpos-a);
    int fixb=0;
    fixb+=(c-b>d)?0:d-(c-b);
    fixb+=(b-a>d)?0:d-(b-a);
    int result=min(min(fixa,fixb),fixc);
    printf("%d\n",result);
    return 0;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include <initializer_list>
#include<unordered_set>
#include<map>
#include <iterator>
#include<stack>
#include<queue>
#include <iomanip>
using namespace std;

int main(){
    /*int x,sum=0;
    string o,n;
    cin>>x;
    cin>>o>>n;
    for (int i=0;i<x;i++){
        if (abs(o[i]-n[i])<=5)
            sum=sum+abs(o[i]-n[i]);
        else
            sum=sum+(10-abs(o[i]-n[i]));
    }
    cout<<sum<<endl;*/
    /*int x,c=1,r;
    cin>>x;
    r=x;
    string s,q;
    while (x!=0){
    cin>>s;
    if (c==1){
    if (s[0]=='O' && s[1]=='O')
    {
        s[0]='+';
        s[1]='+';
            c--;

    }
    else if ( s[3]=='O'&&s[4]=='O')
    {
        s[3]='+';
        s[4]='+';

        c--;

    }
    }
    q=q+s;
    x--;
    }
    if (c==0){
    cout<<"YES\n";

    for (int i=0;i<q.length();i++){
        cout<<q[i];
    if ((i+1)%5==0)
        cout<<endl;
    }
    }
    else
        cout<<"NO\n";*/
    /*bool t=false;
    char c;
    int x,y;
    cin>>x>>y;
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            cin>>c;
            if (c!='W' && c!='B' && c!='G')
                t=true;
        }
    }
    if (t==true)
        cout<<"#Color\n";
    else
        cout<<"#Black&White\n";*/
    /*int x,y;
    cin>>x;
    int c=0;
    int *b=new int [x];
    for (int i=0;i<x;i++){
        cin>>b[i];
    }
    cin>>y;
    int *g=new int [y];
    for (int i=0;i<y;i++){
        cin>>g[i];
    }
    sort(b,b+x);
    sort(g,g+y);
    for (int i=0;i<x;i++){
        for (int j=0;j<y;j++){
            if (g[j]!=102 && b[i]==g[j] || abs(b[i]-g[j])==1){
                g[j]=102;
                c++;
                break;
            }
        }
    }
    cout<<c<<endl;*/
    /*int x,c=1,sum=0,con=0;
    cin>>x;
    int *y=new int [x];
    for (int i=0;i<x;i++)
        cin>>y[i];
    int d,m;
    cin>>d>>m;
    if (x==1){
        if (y[0]==d)
        cout<<1<<endl;
    }
    else {
    for (int i=0;i<x;i++){
        sum=0;
        sum=sum+y[i];
        c=1;
		while (c<=m){
        for (int j=i+1;j<x;j++){

            c++;
            sum=sum+y[j];
            if (c==m){
                if (sum==d)
                    con++;

            }
			if (c>m)
				break;
        }
		break;
        }

    }
    cout<<con<<endl;
    }*/
/*int n;
    cin>>n;
    int *ar=new int [n];
int s[6]={0,0,0,0,0,0};
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
      s[ar[i]-1]++;

    }

    int fact=1;
   for (int i = 1; i <= n; i++) {
        fact = fact * i;
   }
    int fact1=1;
    for(int i=1;i<=6;i++)
    {
        fact1=1;
        for(int j=1;j<=s[i-1];j++)
        {
            fact1 = fact1 * j;

        }
        fact/=fact1;

    }
    cout<<fact;*/
/*int gcd,l,b,n;
cin>>n;
while (n!=0){
	cin>>l>>b;
	if (l>=b){
	 for(int i=1;i<l;i++)
    {
        if(l%i==0 && b%i==0)
        {
            gcd=i;
        }
    }
	}
	else{
		 for(int i=1;i<b;i++)
    {
        if(l%i==0 && b%i==0)
        {
            gcd=i;
        }
    }
	}
	cout<<(b*l)/(gcd*gcd)<<endl;
	n--;
}*/
/*int x;
cin>>x;
long q,n,y,z;
while (x!=0)
{
	z=(ceil(q/2.0)*y);
	cin>>q>>n>>y;
	if ((ceil(q/2.0)*y<(q*n)))
		cout<<z<<endl;
	else
		cout<<q*n<<endl;
	x--;

}*/
/*int n;
cin>>n;
int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
long sum=0,k=0;
for (int i=0;i<n;i++)
{
	sum=0;
	for (int j=0;j<n;j++){
		if (y[i]==y[j] || abs(y[i]-y[j])!=1)
			sum=sum+y[j];
	}
	if (sum>k)
		k=sum;

}
 cout<<k<<endl;*/
/*long a,b,c,m,n,h;
cin>>a>>b>>c;
m=max(a*b*c,a+b+c);
n=max(a*(b+c),(a+b)*c);
cout<<max(n,m)<<endl;*/
/*long long x;
cin>>x;
if (x%2==0)
	cout<<(long long)x/2<<endl;
else
	cout<<(long long)(ceil(x/2.0)*-1)<<endl;
    system("pause");*/
/*long x,t,c=0,k=0;
cin>>x;
long y[4]={0,0,0,0};
for (int i=0;i<x;i++){
	cin>>t;
	y[t-1]++;
}
c=y[3];
if (y[1]%2==0){
	if (y[2]==y[0]){
	c=c+y[0];
	c=c+y[1]/2;
	cout<<c<<endl;
	}
	else if (y[2]>y[0]){
	c=c+y[1]/2;
	c=c+y[0];
	c=c+y[2]-y[0];
	cout<<c<<endl;
	}
	else{
	c=c+y[1]/2;
	c=c+y[2];
	c=c+(y[0]-y[2])/4;
	if ((y[0]-y[2])%4!=0)
		c++;
	cout<<c<<endl;
	}
}
else{
	if (y[2]==y[0]){
	c=c+y[0];
	c=c+y[1]/2+1;
	cout<<c<<endl;
	}

	else if (y[2]>y[0]){
	c=c+y[0];
	c=c+y[1]/2+1;
	c=c+(y[2]-y[0]);
	cout<<c<<endl;
	}
	else {
	c=c+y[2];
	y[0]=y[0]-y[2];

	c=c+y[1]/2;

	y[1]=1;

	for (int i=y[0];i>=1;i--){

		if ((2*y[1]+i)%4==0){
			c=c+((2*y[1]+i)/4);

			y[0]=y[0]-i;
			break;
		}
	}

	if (y[0]%4==0)
	c=c+(y[0]/4);
	else
	c=c+(y[0]/4)+1;
	cout<<c<<endl;
		}

	}*/

	/*int x,q,y,c=0;
	cin>>x;
	while (x!=0){
		cin>>y>>q;
		if (y<=q-2)
			c++;
		x--;
	}

	cout<<c<<endl;*/
 /*int x,y;
 cin>>x>>y;
 if (x==1 && x==10)
	 cout<<-1<<endl;
 else{
	 if (y==10){
		 cout<<1;
		 for (int i=0;i<x-1;i++)
			 cout<<0;
	 }
	 else{
		 cout<<y;
		 for (int i=0;i<x-1;i++)
			 cout<<0;

	 }

 }
      cout<<endl; */
          /* if (s[0]>=97 && s[0]<=122)
           s[0]=s[0]-32;
           cout<<s<<endl;*/
/*int x,y;
cin>>x>>y;
if (x<y)
	cout<<-1<<endl;
else{
	if (y>=ceil(x/2.0))
		cout<<y<<endl;
	else{
		int sum=y;
		while (sum<=x/2){
			sum=sum+(y);
			if (sum==ceil(x/2.0))
				break;
		}
		cout<<sum<<endl;
	}
}*/

/*long long x,y;
cin>>x>>y;
if (y<=ceil(x/2.0))
	cout<<y*2-1<<endl;
else{
	y=y-ceil(x/2.0);
	cout<<y*2<<endl;
}*/
/*double sum=0;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
	sum=sum+((double)y[i]/100);
}

sum=(sum/x)*100;
printf("%3.12f\n",sum);*/
/*int t,a,b;
cin>>t;
while (t!=0){
	cin>>a>>b;
	cout<<(a>=b?"FunkyMonkeys\n":"WeWillEatYou\n");
	t--;
}*/
/*string t="AHIMOTUVWXY";
string s;
cin>>s;
int c=0;
for (int i=0;i<s.length();i++){
	for (int j=0;j<t.length();j++){
		if (s[i]==t[j]){
			c++;
			break;
		}
	}
}
if (c!=s.length())
	cout<<"no\n";
else{
	bool t=true;
	for (int i=s.length()-1;i>=0;i--){
		if (s[i]!=s[s.length()-i-1])
		{
			cout<<"no\n";
			t=false;
			break;
		}
	}
	if (t)
		cout<<"yes\n";
}*/
/*int x,sum=0,y=1;
cin>>x;
if (x==1)
	cout<<1<<endl;
else if (x==2)
	cout<<3<<endl;
else{
	sum=(x*(x+1))/2;
	for (int i=x-1;i>1;i--){
		sum=sum+(i-1)*y;
		y++;
	}
	cout<<sum<<endl;
}*/

/*long x,sum=0;
cin>>x;
long *y=new long [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);

for (int i=0;i<x;i++){
	sum=sum+(y[x-1]-y[i]);
}
cout<<sum<<endl;*/
/*int x;
cin>>x;
if (x%2==0){
for (int i=x;i>0;i--){
	if (i==1)
		cout<<"I love it ";
	else if (i%2==0)
		cout<<"I hate that ";
	else
		cout<<"I love that ";

}
}
else{
	for (int i=x;i>0;i--){
	if (i==1)
		cout<<"I hate it ";
	else if (i%2==0)
		cout<<"I love that ";
	else
		cout<<"I hate that ";

}
}*/
/*int x,y,t=240,u=5,sum=0,k=0;
cin>>x>>y;
t=t-y;

for (int i=1;i<=x;i++){

		sum=sum+i*u;
		if (sum<=t)
		k++;

}
cout<<k<<endl;*/
/*int x;
cin>>x;
cout<<(x==2?2:1)<<endl;*/
/*int x,y,sum=0,j,l=0;
cin>>x>>y;
j=x;
l=y;
sum=y;

if (l==x-1){
	cout<<l<<endl;
}
else if (l>=x)
	cout<<x-1<<endl;
else{
for (int i=2;i<=x;i++){
	l--;
	sum=sum+i;
	l++;
	if (x-i==l)
		break;
}
cout<<sum<<endl;
}*/
/*int u1=0,u2=0,u3=0,u4=0,u5=0,ch=0,ce=0,cl=0,cl2=0,co=0;
string s;
cin>>s;
for (int i=0;i<s.length();i++){
	if (s[i]=='h'){
		ch++;
		u1=i;
		break;
	}
}
for (int i=u1+1;i<s.length();i++){
	if (s[i]=='e'){
		u2=i;
		ce++;
		break;
	}
}
for (int i=u2+1;i<s.length();i++){
	if (s[i]=='l'){
		u3=i;
		cl++;
		break;
	}
}
for (int i=u3+1;i<s.length();i++){
	if (s[i]=='l'){
		u4=i;
		cl2++;
		break;
	}
}
for (int i=u4+1;i<s.length();i++){
	if (s[i]=='o'){

		co++;
		break;
	}
}
if (co==1 && ce==1 && cl==1 && cl2==1 && ch==1)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

/*int n,a,b;
cin>>n>>a>>b;
if (b>=0){

	if ((b+a)%n==0)
		cout<<n<<endl;
	else
		cout<<(a+b)%n<<endl;

}
else {

		if(-1*b < a)
		{
			cout<< a-b;
		}
		else
		{
			cout<< n-(((-1*b)-a)%n);
		}

}*/
/*long x,y;
cin>>x>>y;
cout<<y%((long)(pow(2.0,x)))<<endl;*/

/*long long x;
cin>>x;
if (x!=0&&x%2==0)
	cout<<x+1<<endl;
else
	cout<<x/2+1<<endl;*/

/*bool t=false;
long x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
for (int i=0;i<x;i++){
	if (t==true)
		break;
	for (int j=i+1;j<x-1;j++){
		if (y[i]+y[j]>y[j+1]){
			cout<<"YES\n";
			t=true;
			break;
		}
}
}
if (t==false)
	cout<<"NO\n";*/

/*long *y=new long[4];
for (long i=0;i<4;i++){
	cin>>y[i];
}
sort(y,y+4);
for (long i=0;i<3;i++){
	cout<<y[3]-y[i]<<" ";
}*/
/*int n,t,x,z;
cin>>n>>t;
int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
int *o;
while (t--){
	cin>>x>>z;
	o=new int [z];
	for (int i=x;i<z;i++){
		o[i]=y[i];
	}
	sort(o,o+z);
	cout<<o[0]<<endl;
}*/

/*vector <int>y;
int x,q,c=0;
cin>>x;
while (true){
    cin>>q;
    if (q==0)
        break;
	y.push_back(q);
    c++;
}

int n=0,r=c-1,b=0,o=0;

long int u=0;
while (r>n){
    if (u>abs(y[n]+y[r]-x)){
        u=abs(y[n]+y[r]-x);
        b=r;
        o=n;
    }
    if (abs(y[n]+y[r])>x)
        r--;
    else
        n++;
}

cout<<"The closest pair is "<<y[o]<<" and "<<y[b]<<endl;

 long long x,c=0,q;
    cin>>x;
    q=ceil(x/2.0);
    while (q!=0)
    {
        c++;
        q=q/2;
    }
    cout<<c<<endl;*/
/*int x,y,c=0,l;
cin>>x>>y;
l=x;

while (x<y&& y%x==0){
	x=x*l;
	c++;
}
if (x==y)
	cout<<"YES\n"<<c<<endl;
else
	cout<<"NO\n";*/
/*long x,sum=0;
cin>>x;
while (x!=0){
	if (x>=100){
		sum=sum+(x/100);
		x=x%100;
	}

	else if (x>=20){
		sum=sum+(x/20);
		x=x%20;
	}
	else if (x>=10){
		sum=sum+(x/10);
		x=x%10;
	}
	else if (x>=5){
		sum=sum+(x/5);
		x=x%5;
	}
	else{
	sum=sum+x;
	x=0;
	}
}
cout<<sum<<endl;*/
/*int x,c=1,j=0;
cin>>x;
int *y=new int [x];
int *q=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
for (int i=0;i<x;i++){
	q[i]=0;
}
for (int i=0;i<x-1;i++){
	if (y[i]<y[i+1])
		c++;
	else{
		q[j]=c;
		j++;
		c=1;
	}
}
sort(q,q+x);
if (c==x|| c>q[x-1])
	cout<<c<<endl;
else
cout<<q[x-1]<<endl;*/
/*int w,a,b,m=0,l=0,hh=0;

cin>>w>>a>>b;
string x;
cin>>x;
if (((x.length())>w*b) || ((x.length())<w*a) )
	cout<<"No solution\n";
else{
	int m = ((double)x.length() / w), d = x.length() % w;
	for (int i=1;i<=w;i++){
		if (i<w){
			for(int j=0; j<m; j++)
				cout<<x[hh++];
            cout<<endl;
		}
		else
		{
			while (hh<x.length()){
				cout<<x[hh++];
			}
            cout<<endl;

		}
      }

}
*/
/*int x;
cin>>x;
int *y=new int [x];
y[0]=1;
y[1]=1;
for (int i=2;i<x;i++){
	y[i]=y[i-1]+y[i-2];
}
cout<<y[x-1]<<endl;*/
/*int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
long q;
cin>>q;
bool t=false,l=false;
long *u=new long [q];
for (int i=0;i<q;i++){
	cin>>u[i];
}
sort(y,y+x);
long sum=0;
long beg=0,end=x-1;
for (int i=0;i<q;i++){
	cout<<upper_bound(y,y+x,u[i])-y<<endl;
}*/

/*int x,c=0,sum=0;
	vector<int>v;
	cin>>x;
	string s;
	cin>>s;
	for (int i=0;i<x;i++){
		if (s[i]=='x')
			c++;
		else{
			v.push_back(c);
			c=0;
		}
	}
	if (c!=0)
		v.push_back(c);
	for (int i=0;i<v.size();i++){
		if (v[i]>=3)
			sum=sum+(v[i]-2);
	}
	cout<<sum<<endl;*/
/*int x,y;
cin>>x>>y;
int c=1;
vector <int>b(100001);
int *u=new int [y];
int *v=new int [x];
int *p=new int [y];
for (int i=0;i<x;i++){
	cin>>v[i];
}
for (int i=0;i<y;i++){
	cin>>p[i];
}
for (int i=0;i<y;i++){
	c=1;
for (int j=p[i]-1;j<x;j++){
	if (b[v[j]]==0)
	{
		b[v[j]]=c;
		c++;
	}

}
u[i]=c-1;
	fill(b.begin(), b.end(), 0);
}
for (int i=0;i<y;i++){
	cout<<u[i]<<endl;
}
cout<<endl;*/
/*int x,y,z;
cin>>x>>y;
set<int>s1;
int *p=new int[x+1];
int *u=new int [x+1];
for (int i=1;i<=x;i++){
	cin>>u[i];
}

for (int i=x;i>=1;i--){
	s1.insert(u[i]);
	p[i]=s1.size();
}
for (int i=0;i<y;i++){
	cin>>z;
	cout<<p[z]<<endl;
}*/

/*bool o=false;
string x[5]={"S","M","L","XL","XXL"};
int y[5];

for (int i=0;i<5;i++){
	cin>>y[i];
}
int n,l;
cin>>n;
string s;
while (n--){
	o=false;
	cin>>s;

		for (int i=0;i<5;i++)
		{
			if (s==x[i]){
				l=i;
				if (y[l]>0){
					cout<<x[l]<<endl;
					y[l]--;
					o=true;
				}
				else if (l!=0&&l!=4 &&y[l]==0 &&y[l+1]==0&&y[l-1]>0){
					cout<<x[l-1]<<endl;
					y[l-1]--;
					o=true;
				}
				else{
				for (int j=l;j<5;j++)
				{
					if (y[j]>0){
						cout<<x[j]<<endl;
						y[j]--;
						o=true;
						break;
					}
				}
				}

		if (o==false){
			for (int j=l;j>=0;j--)
				{
					if (y[j]>0){
						y[j]--;
						cout<<x[j]<<endl;

						break;
					}
				}
		}
			}
		}

}*/
/*int c=0,sum=0;
long x;

scanf("%d",&x);
long *y=new long [x];
for (int i=0;i<x;i++){
	scanf("%d",&y[i]);
}
sort(y,y+x);
for (int i=0;i<x;i++){
	if (sum<=y[i])
		sum=sum+y[i];
	else
		c++;
}
printf("%d\n",x-c);*/
/*int x,y,max=1,c=1;
scanf("%d",&x);
scanf("%d",&y);
int *q=new int [x];
for (int i=0;i<x;i++){
	scanf("%d",&q[i]);
}
for (int i=0;i<x-1;i++){
	if (q[i]!=q[i+1])
		c++;
	else{
		if (max<c)
			max=c;
		c=1;
	}
}
if (c>max)
	cout<<c<<endl;
else
	cout<<max<<endl;*/

/*int x,max=0,j=0;
cin>>x;
int *y=new int [x];
int q[5]={0,0,0,0,0};
for (int i=0;i<x;i++){
	cin>>y[i];
	q[y[i]]++;
}
for (int i=0;i<5;i++){
	if (max<q[i]){
		max=q[i];
		j=i+1;
	}
}
cout<<j<<endl;*/
/*string  numbers[] = {
        "zero",
        "one",
        "two",
        "three",
        "four",
        "five",
        "six",
        "seven",
        "eight",
        "nine",
        "ten",
        "eleven",
        "twelve",
        "thirteen",
        "fourteen",
        "fifteen",
        "sixteen",
        "seventeen",
        "eighteen",
        "nineteen",
        "twenty",
        "twenty one",
        "twenty two",
        "twenty three",
        "twenty four",
        "twenty five",
        "twenty six",
        "twenty seven",
        "twenty eight",
        "twenty nine"
    };
int h,m;
cin>>h>>m;
if (m==0)
	cout<<numbers[h]<<" o' clock\n";
else if (m==15)
	cout<<"quarter past "<<numbers[h]<<endl;
else if (m<30)
	cout<<numbers[m]<<(m==1?" minute past ":" minutes past ")<<numbers[h]<<endl;
else if (m==30)
	cout<<"It's half past "<<numbers[h]<<endl;
else if (m==45)
	cout<<"It's quarter to "<<numbers[h]<<endl;
else
	cout<<"It's "<<numbers[60-m]<<" to "<<numbers[h]<<endl;*/
/*bool cab=false,cba=false,k=false;
string s;
cin>>s;
for (int i=0;i<s.length()-1;i++){

	if (s[i]=='A' && s[i+1]=='B'){
		cab=true;
		if (i<s.length()-2)
		i=i+2;
		k=true;
	}

	if (k==true&&s[i]=='B' && s[i+1]=='A'){
		cba=true;
		i=i+2;
}

}
	if (cab ==true && cba ==true){
		cout<<"YES\n";

	}
	else{
		cba=false;
		cab=false;
		k=false;
		for (int i=0;i<s.length()-1;i++){

	if (s[i]=='B' && s[i+1]=='A'){
		cba=true;
		if (i<s.length()-2)
		i=i+2;
		k=true;
	}

	if (k==true&&s[i]=='A' && s[i+1]=='B'){
		cab=true;
		i=i+2;
}

}

	if (cab ==true && cba ==true)
		cout<<"YES\n";
	else{
		cout<<"NO\n";

	}
	}*/
/*int be=0,en=10;
int x,z;
bool t=false;

int y[11]={0,0,0,0,0,0,0,0,0,0,0};
for (int i=0;i<6;i++){
	cin>>x;
	y[x]++;
}

for (int i=0;i<11;i++){

if (y[i]>=4){
	z=i;
	t=true;
}
}

if (t==true)
{
	for (int i=0;i<11;i++){
		if (y[i]==2 ||y[i]==6){
		cout<<"Elephant\n";
		be=1;
		break;
		}

	}
	if (be==0)
		cout<<"Bear\n";
}
else
	cout<<"Alien\n";*/

/*long long x,y=2520,c=0;
cin>>x;
c=x/y;
cout<<c<<endl;*/
/*long sum=0;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
for (int i=x-1;i>=0;i--){
	sum=sum+(pow(2.0,x-1-i)*y[i]);
}
cout<<sum<<endl;*/
/*int sum=0;
int x,k;
cin>>x>>k;
vector<pair<int ,int >>v(x);
for (int i=0;i<x;i++){
	cin>>v[i].first;
	cin>>v[i].second;
}
sort(v.begin(),v.end());
/*for (int i=0;i<x;i++){
	cout<<v[i].first<<"        "<<v[i].second<<endl;
}*/

/*for (int i=x-1;i>=0;i--){
	if (v[i].second==0)
		sum=sum+v[i].first;
	else{
		if (k>0){
		k--;
		sum=sum+v[i].first;
		}
		else{
			sum=sum-v[i].first;
		}

	}
}
cout<<sum<<endl;*/

/*int x;
cin>>x;
string *y=new string [x];

set <string> s;
for (int i=0;i<x;i++)
	cin>>y[i];
cout<<endl;
for (int i=x-1;i>=0;i--){
	if (s.count(y[i])==0)
	cout<<y[i]<<endl;
	s.insert(y[i]);
}*/

/*bool b=false;
int x;
vector <int>v;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}

for (int i=0;i<x-1;i++){
	for (int j=i+1;j<x;j++){
		if (y[i]==y[j]){
			b=true;
			v.push_back(j-i);
		}
	}
}
if (b==true){
sort(v.begin(),v.end());
cout<<v.at(0)<<endl;
}
else
	cout<<-1<<endl;*/

/*int x,k,c=0;
cin>>x;
int *y=new int [x];
vector <int >v;
for (int i=0;i<x;i++){
	cin>>y[i];
}
sort(y,y+x);
while (y[x-1]!=0){
	c=0;
	for (int i=0;i<x;i++){
		if (y[i]>0){
			k=y[i];
			break;
		}
	}
	for (int i=0;i<x;i++){
		y[i]=y[i]-k;
	}
	for (int i=0;i<x;i++){
		if (y[i]>0)
			c++;
	}
	v.push_back(c);
}
cout<<x<<endl;
for (auto i=v.begin();i!=v.end()-1;i++){
	cout<<*i<<endl;
}*/

/*
int x;
cin>>x;

int** a = new int*[x];
for(int i = 0; i < x; ++i)
    a[i] = new int[x];

for (int i=0;i<x;i++){
	for (int j=0;j<x;j++){
		scanf("%1d", &a[i][j]);
	}
}
for (int i=0;i<x;i++){
	for (int j=0;j<x;j++){
		if ((i>=1)&&(i<x-1) && (j>=1)&&(j<x-1)){
		if ( a[i][j] > a[i-1][j] &&
             a[i][j] > a[i+1][j] &&
             a[i][j] > a[i][j-1] &&
             a[i][j] > a[i][j+1])
              printf("X");
         else
            printf("%d",a[i][j]);
		}
		else
			 printf("%d",a[i][j]);
	}

	printf("\n");
}
*/

/*int n=2;
int x,y;
cin>>x>>y;
if (x<y && x!=y && y%2!=0)
	cout<<"No\n";
else if ((x==y && y%2!=0))
	cout<<"Yes\n";
else if (x>y && x%y==0)
	cout<<"Yes\n";
else{
	for (int i=1;;i++)
	{
		n=pow(2.0,i);
		if (n==y){
			cout<<"Yes\n";
			break;
		}
		else if (n>y){
			if ( y==x || y==(x*2) ||(x==3 && y%x==0)){
				cout<<"Yes\n";
				break;
			}
			else{
				cout<<"No\n";
				break;
			}
		}
	}
}*/

/*int x,y,z;
cin>>x>>y>>z;
if (x>y+z)
	cout<<"+"<<endl;
else if (y>x+z)
	cout<<"-"<<endl;
else if (y==x+z && x==y+z )
	cout<<"0"<<endl;
else
	cout<<"?"<<endl;*/

/*bool v=false;
string suit="DCSH";
string rank="23456789TJQKA";
string t,h;
cin>>t;
for (int i=0;i<5;i++){
	cin>>h;
	if (h[0]==t[0] ||h[1]==t[1])
		v=true;
}
if (v)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

/*string s,s1,s2;
cin>>s;
for (int i=0;i<s.length();i++){
	s1=s1+s[i];
	if (s[i]!='a')
		s2=s2+s[i];
	if (s1.length()+s2.length()==s.length())
		break;
}
if (s1+s2==s)
	cout<<s1<<endl;
else
	cout<<":(\n";*/

/*long x,k;
cin>>x>>k;
while (k--){
	if (x%10>0)
		x--;
	else
		x/=10;
}
cout<<x<<endl;*/

/*bool t=false;
int x;
cin>>x;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
	if (y[i]==1)
		t=true;
}
if (t)
	cout<<"HARD\n";
else
	cout<<"EASY\n";*/

/*int sum=0;
int x;
scanf("%d",&x);
string y;
while (x--){
	cin>>y;
	if (y=="Icosahedron")
		sum=sum+20;
	else if (y=="Cube")
		sum=sum+6;
	else if (y=="Tetrahedron")
		sum=sum+4;
	else if (y=="Dodecahedron")
		sum=sum+12;
	else
		sum=sum+8;
}
printf("%d\n",sum);*/

/*int c=0;
int x;
cin>>x;
for (int i=1;i<x;i++){
	if (x%i==0)
		c++;
}
cout<<c<<endl;*/

/*
long long n,x,y,sum=0;
cin>>n;

while (n--){
cin>>x;
cin>>y;
	sum=0;
while (true){
	if (x==0)
		break;
	if (x%y==0){
		sum++;
		x=x/y;
	}
	else{
		sum=sum+x%y;
		x=x-x%y;
	}
}
cout<<sum<<endl;
}
*/

/*int x;
cin>>x;
if (x%2==0)
	cout<<"Mahmoud\n";
else
	cout<<"Ehab\n";*/

/*int san=0,sea=0;
int x;
cin>>x;
string n;
cin>>n;
for (int i=0;i<n.length()-1;i++){
	if (n[i]=='S' && n[i+1]=='F')
		san++;
	else if (n[i]=='F' && n[i+1]=='S')
		sea++;
}
if (san!=0&&san>sea)
	cout<<"YES\n";
else
	cout<<"NO\n";*/

   /*long double x,y,z,c=0;
    cin>>x>>y;
    z=(int)ceil(y/x);
    cout<<fixed<<setprecision(0)<<z<<endl;*/

/*bool t=true;
  long long n,k,sum=0,c=1,h=0;
  cin>>n>>k;
  sum=k;
  if (n==1)
      cout<<0<<endl;
  else if ((2+k)*(k-1)/2-(k-2)<n)

        cout<<"-1"<<endl;
  else
  {
	  k--;
	  n--;
     long long r=k;
	 long long l=0;
	long  long mid=(r+l)/2;
	  while (l<r){
		  mid=(r+l)/2;
		  sum=((((k*(k+1))/2))-(((mid*(mid+1))/2)));
		  if (n<sum)
			  l=mid+1;
		  else if (n>sum)
			  r=mid;
		  else
		  {
			  t=false;
			  cout<<k-mid<<endl;
			  break;
		  }
	  }
	  if (t&&sum>n){
		  cout<<k-mid<<endl;

	  }
	  else if (t && sum<n){
		  cout<<k-mid+1<<endl;

	  }

  }
  */

/*int x;
cin>>x;
int** a = new int*[x];
for(int i = 0; i < x; i++)
    a[i] = new int[x];
for(int i = 0; i< x;i++){
	for(int j = 0; j< x;j++){
		if (i==0 ||j==0)
			a[i][j]=1;
		else
			a[i][j]=a[i-1][j]+a[i][j-1];
	}
}
cout<<a[x-1][x-1]<<endl;*/

/*int x,t,z,cm=0,cc=0;
cin>>x;
while (x--){
	cin>>t>>z;
	if (t>z)
		cm++;
	else if (t<z)
		cc++;
}
if (cm>cc)
	cout<<"Mishka\n";
else if (cc>cm)
	cout<<"Chris\n";
else
	cout<<"Friendship is magic!^^\n";*/

/*
int x[4],sum=0;
for (int i=0;i<4;i++){
	cin>>x[i];
}
string s;
cin>>s;
for (int i=0;i<s.length();i++){
	sum=sum+x[s[i]-'1'];
}
cout<<sum<<endl;*/

/*long long a,b,k;
int x;
cin>>x;
while (x--){
cin>>a>>b>>k;

	cout<<fixed<<setprecision(0)<<(a*(k/2+k%2))-(b*(k/2))<<endl;
}*/
/*
int x;
cin>>x;
long long y,sum=0;
while (x--){
	sum=0;
	cin>>y;
	while (y>1){
		if (y%2==0){
			y=y/2;
			sum++;
		}
		else if (y%3==0){
			y=y/3*2;
			sum++;
		}
		else if (y%5==0)
		{
			y=y/5*4;
			sum++;
		}
		else
			break;
	}

	if (y==1)
		cout<<sum<<endl;
	else
		cout<<-1<<endl;
}*/

/*
int sum1=0,sum2=0;
int s,v1,v2,t1,t2;
cin>>s>>v1>>v2>>t1>>t2;
sum1=2*t1+(v1*s);
sum2=2*t2+(v2*s);
if (sum1<sum2)
	cout<<"First\n";
else if (sum1>sum2)
	cout<<"Second\n";
else
	cout<<"Friendship\n";*/

/*bool t=false;
int x;
cin>>x;
int*y=new int [x];
int q[10];
for (int i=0;i<x;i++){
	cin>>y[i];
}
for (int i=0;i<10;i++){

	cin>>q[i];
}
sort(y,y+x);

for (int i=0;i<10;i++){
	if (y[0]>=q[i])
		t=true;
}
if (t)
	cout<<10<<endl;
else
	cout<<y[0]<<endl*/

/*
int x,sum=0,l=0;
cin>>x;
for (int i=1;;i++){
	sum=sum+(i*(i+1))/2;
	if (sum==x){

		cout<<i<<endl;
		break;
	}
	else if (sum>x){

		cout<<i-1<<endl;
		break;
	}
}*/

/*
long long x,y,z,c=0;
cin>>x>>y>>z;
if (x+y<z)
	cout<<0<<" "<<0<<endl;
else if (x%z==0 || y%z==0||(x%z+y%z)<z)
	cout<<((x+y)/z)<<" "<<0<<endl;
else  {
	cout<<((x+y)/z)<<" "<<min(z-(y%z),z-(x%z))<<endl;
}*/

/*
int x,t1,t2,p,sum=0;
cin>>x>>t1>>t2;
t2=2*t2;
for (int i=0;i<x;i++){
	cin>>p;
	if (p==2)
	{
		if (t2%2==0)
		t2=t2-p;
		else
			sum=sum+p;
	}
	if (p==1){
		if (t1>0)
			t1--;
		else if (t2>0)
			t2--;
		else
			sum++;
	}
}
cout<<sum<<endl;*/

/*
bool p2=true;
bool p=true;
int k=2;
int x,i,l=0;
cin>>x;
if ((x*1+1)%2==0&& x!=1)
	cout<<1<<endl;
else{
while (true){
	p2=true;
	p=true;
for ( i=2;i<k;i++){
	if (k%i==0)
		p=false;
}
if (i==k)
	p=true;
if (p){
	l=x*k+1;
	for (int i=2;i<l;i++){
		if (l%i==0){
			cout<<k<<endl;
			p2=false;
			break;
		}
	}
	}
if (!p2)
	break;
	else
		k++;
}
}*/

/*
int n,p,k,pl,pr,pr2,pl2;
cin>>n>>p>>k;
pl=p-k;
pl2=pl;
pr=p+1;
pr2=pr-1;

	if (pl>1){
		cout<<"<< ";

	while (pl<p){
		cout<<pl<<" ";
		pl++;
	}
	cout<<"("<<p<<") ";
	if (pr2+k<n){
		while (pr<=pr2+k){
			cout<<pr<<" ";
			pr++;
		}
		cout<<">>";
	}
	else {
		while (pr<=n){
			cout<<pr<<" ";
			pr++;
		}
	}
	cout<<endl;
	}
	else{
		pl=1;
		pl2=1;
		while (pl<p)
		{
			cout<<pl<<" ";
			pl++;
		}
		cout<<"("<<p<<") ";
		if (pr2+k<n){
		while (pr<=pr2+k){
			cout<<pr<<" ";
			pr++;
		}
		cout<<">>";
	}
		else{

		while (pr<=n){
			cout<<pr<<" ";
			pr++;
		}
	}
	cout<<endl;
	}*/

/*
bool b=true;
int x,l;
cin>>x>>l;
int *y=new int [x];
for (int i=0;i<x;i++)
	cin>>y[i];
for (int i=1;i<x;i++)
{
	if (y[i-1]>=y[i]){
		cout<<i+1<<endl;
		b=false;
		break;
	}
}
if (b)
	cout<<0<<endl;*/

/*Books

int sum=0,c=0,i,cf=0;
int r,l,mid=0;
int n,t;
cin>>n>>t;
l=0;
r=n-1;

int *y=new int [n];
for (int i=0;i<n;i++){
	cin>>y[i];
}
while (l<r){
	sum=0;
	mid=(l+r)/2;
	for (i=mid;i<n;i++){
		sum=sum+y[i];
	}
	c=n-mid;
	if (sum>t){
		l++;
	}
	else{
		r--;
		if (c>cf)
			cf=c;
	}
}

if (cf==0 &&t>=y[0])
	cout<<1<<endl;
else
	cout<<cf<<endl;*/

int a,b,c,d,sum=0;
cin>>a>>b>>c>>d;

/*	if ((abs(c-b)>=d && abs(c-a)>=d && abs(a-b)<d))
	{
		sum=sum+d-abs(a-b);
	}
	else if ((abs(c-b)>=d&& abs(b-a)>=d && abs(c-a)<d)){
		sum=sum+d-abs(c-a);
	}
	else if ((abs(c-a)>=d&& abs(b-a)>=d && abs(c-b)<d)){
		sum=sum+d-abs(c-b);
	}
	else if (abs(c-b)>=d && abs(c-a)<d && abs(a-b)<d)
		sum=sum+abs(c-a)+abs(b-a);

	else if ((abs(c-b)<d&& abs(b-a)>=d && abs(c-a)<d)){
		sum=sum+abs(a-c)+abs(b-c);
	}

	else if ((abs(c-b)<d&& abs(b-a)<d && abs(c-a)>=d)){
		sum=sum+abs(c-b)+abs(a-b);
	}
	else if ((abs(c-b)>=d && abs(c-a)>=d && abs(a-b)>=d))
		sum=0;
	else
	{*/
		int mx = max(a,b);
mx = max(mx, c);
int mn=min(a,b);
mn=min(mn,c);
/*
if (mx== a && mn==b)
	sum=sum+(d-(a-c))+(d-(c-b));
else if (mx==a && mn==c)
	sum=sum+(d-(a-b))+(d-(b-c));
else if (mx==b && mn==a)
	sum=sum+(d-(b-c))+(d-(c-a));
else if (mx==b && mn==c)
	sum=sum+(d-(b-a))+(d-(a-c));
else if (mx==c && mn==a)
	sum=sum+(d-(c-b))+(d-(b-a));
else if (mx==c && mn==b)
	sum=sum+(d-(c-a))+(d-(a-b));
	*/
int t=d*3-(mn-1)-mx;
	cout<<t+mx<<endl;

/*
string a,b;
int x;
cin>>x;
while (x--){
	cin>>a>>b;

}*/

/*
int x,m,sum=0;
cin>>x>>m;
int *y=new int [x];
for (int i=0;i<x;i++){
	cin>>y[i];
}

if (y[0]<=m)
cout<<"0 ";
else
cout<<"1 ";
for (int i=1;i<x;i++){
	sum=0;
	int *q=new int [i];
	for (int j=0;j<i;j++){
		sum=sum+y[j];
		q[j]=y[j];
	}
	sort(q,q+i);

	if (sum+y[i]<=m)
		cout<<0<<" ";
	else{
		int p=0,p1=0;
		int sum2=sum+y[i];

		for (int l=i-1;l>=0;l--){
			sum2=sum2-q[l];
			if (sum2<=m){
				cout<<i-l<<" ";
				break;
			}
		}

	}

}*/

    return 0;
}

//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define ll              long long
#define ull             unsigned long long
#define pb              push_back
#define fastread()      (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
int main()
{
    fastread();
    ll a,b,c,d,e = 0;
    cin>>a>>b>>c>>d;
    if(a > b){
        swap(a,b);
        if(a > c){
            swap(a,c);
        }
    }
    if(b > c){
        swap(b,c);
    }
    ll ans = max(e,d - (b - a)) + max(e,d - (c - b));
    cout<<ans<<endl;
    return 0;
}

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	long long int a,b,c,d,max,min,mid;
	cin>>a>>b>>c>>d;
	if(a>b && a>c)
	    max=a;
	else if(b>a && b>c)
	    max=b;
	else if(c>b && c>a)
	    max=c;
	if(a<b && a<c)
	    min=a;
	else if(b<a && b<c)
	    min=b;
	else if(c<b && c<a)
	    min=c;
	if(a==max && b==min || a==min && b==max)
	    mid=c;
    else if(b==max && c==min || b==min && c==max)
        mid=a;
    else if(c==max && a==min|| c==min && a==max)
        mid=b;
	//cout<<max<<" "<<mid<<" "<<min;
    long long int e=mid-min;
    long long int f=max-mid;
    long long int minimum;
    if(e<d && f<d)
    {
        cout<<(2*d)-(e+f);
    }
    else if(e<f)
    {
         minimum=e;
         cout<<d-minimum;
    }
    else if(e>f)
    {
        minimum=f;
        cout<<d-minimum;
    }
	return 0;
}

#include <iostream>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <map>
#include <string>
#include <set>
using namespace std;

int main()
{
	int a, b, c, d, d1, d2, res;
	cin >> a >> b >> c >> d;

	d1 = c - b;
	d2 = c - a;
	res = 0;

	if (d1 * d2 <= 0) {
		d1 = abs(d1);
		d2 = abs(d2);
	}
	else {
		d1 = abs(d1);
		d2 = abs(d2);
		if (d1 < d2)
			d2 = d2 - d1;
		else
			d1 = d1 - d2;
	}
	if (d1 < d)
		res += d - d1;
	if (d2 < d)
		res += d - d2;
	cout << res;

	return 0;
}

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
	long long a,b,c,d;
	cin>>a>>b>>c>>d;
	if(a==b and b==c) cout<<2*d;
	else
		if(a==b and abs(c-a)>2*d) cout<<abs(abs(c-a)-d);
		else
			if(c==b and abs(c-a)>2*d) cout<<abs(abs(c-a)-d);
			else
				if(c==a and abs(c-b)>2*d) cout<<abs(abs(c-b)-d);
				else
					if(a==b and abs(c-a)>d) cout<<abs(c-a);
					else
						if(c==b and abs(c-a)>d) cout<<abs(c-a);
						else
							if(c==a and abs(c-b)>d) cout<<abs(c-b);
							else
								if(a<b and b<c) cout<<2*d-abs(c-b)-abs(b-a);
								else
									if(b<a and a<c) cout<<2*d-abs(c-a)-abs(b-a);
									else
										if(a<c and c<b) cout<<2*d-abs(c-a)-abs(c-b);

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define next Next
#define int long long
/*char buf[1<<21],*p1=buf,*p2=buf;
inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/
#define gc getchar
inline int read()
{
	int ret=0,f=0;char c=gc();
	while(!isdigit(c)){if(c=='-')f=1;c=gc();}
	while(isdigit(c)){ret=ret*10+c-48;c=gc();}
	if(f)return -ret;return ret;
}
signed main()
{
	int a=read(),b=read(),c=read();
	if(b>=a&&c>=a)puts("YES");
	else puts("NO");
}

#include<bits/stdc++.h>
using namespace std ;
int cnt , ans ;
int main ( ) {
    int a [ 3 ] , d ;
    for ( int i = 0 ; i < 3 ; i ++ )
        cin >> a [ 0 ] >> a [ 1 ] >> a [ 2 ] ;
    cin >> d ;
    sort ( a , a + 3 ) ;
    cnt = d - a [ 1 ] + a [ 0 ] ;
    ans = d - a [ 2 ] + a [ 1 ] ;
    cout << max ( 0 , cnt ) + max ( 0 , ans ) << endl ;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
	int d,a[5],ans=0;
	for(int i=0;i<3;i++)cin>>a[i];
	cin>>d;
	sort(a,a+3);
	if(a[1]<a[0]+d)ans=a[0]+d-a[1],a[1]=a[0]+d;
	else if(a[1]>a[0]+d){
		ans=a[1]-a[0]+d;
		a[1]=a[0]+d;
	}
	if(a[2]<a[1]+d)ans=max(ans,a[1]+d-a[2]);
	cout<<ans<<endl;
}

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
	int sum = 0;
	vector<int> a(3, 0);
	for (int i = 0; i < 3; i++) cin >> a[i];
	int d; cin >> d;
	sort(a.begin(), a.end());
	for (int i = 0; i < 2; i++)
	{
		int temp = a[i + 1] - a[i] - d;
		if (temp < 0) {
			temp *= -1;
			a[i + 1] += temp; sum += temp;
		}
		else continue;
	}
	cout << sum;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,minn,maxn,mid;
    cin>>a>>b>>c>>d;
    minn=min(a,min(b,c));
    maxn=max(a,max(b,c));
    mid=max(min(a,c),min((min(b,c),max(b,a)),(max(b,c),min(b,a))));
    int diff1=d-(maxn-mid);
    if(diff1<0)
    {
        diff1=0;
    }
    int diff2=d-(mid-minn);
    if(diff2<0)
    {
        diff2=0;
    }
    cout<<diff1+diff2;
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define pb push_back
#define ft first
#define sd second
using namespace std;

ll a, b, c, d;
ll x[4];

int main()
{
    scanf("%lld %lld %lld %lld",&x[0],&x[1],&x[2],&d);
    sort( x, x + 3 );
    a = x[0];
    b = x[1];
    c = x[2];
    ll ans1 = d + a - b;
    ll ans2 = d + b + ans1 - c;
    printf("%lld", max( ans1, ans2));
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
bool cmp(int a, int b) {
 return a<b;
}
int main() {
 int a[3],d;
 for(int i = 0 ;  i <=2 ;i++)
 cin>>a[i];
 sort(a,a+3,cmp);
 cout<<max(0,d-(a[1]-a[0])+max(0,d-(a[2]-a[1])));
 return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,x,y=0,i,j,n=0,m;
    vector<int>v;
    cin>>a>>b>>c>>d;
    v.push_back(a);
    v.push_back(b);
    v.push_back(c);
      sort(v.begin(),v.end());

      if(v[1]-v[0]<d){
          x=v[1]-v[0];
          y=d-x;
          v[1]=v[1]+y;
      }
      if(abs(v[2]-v[1])<d)
      {
          m=abs(v[2]-v[1]);
          n=d-m;
      }
      cout<<y+n<<endl;

    return 0;
}

#include <bits/stdc++.h>

using namespace std;

long long v[4], a, b, c, d;

int main()
{
    long long ans , p;
    cin >> a >> b >> c >> d;
    ans = 0;
    v[1] = a;
    v[2] = b;
    v[3] = c;
    sort(v + 1, v + 3 + 1);
    if(abs(v[2] - v[1]) < d)
    {
        p = d - abs(v[2] - v[1]);
        ans += p;
        if(v[1] - p <= 0)
            v[2] += p;
        else v[1] -= p;
    }
    if(abs(v[3] - v[2]) < d)
    {
        p = d - abs(v[3] - v[2]);
        ans += p;
    }
    cout << ans << "\n";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
    int i,d,j;
    int ar=0,arr[3];
    cin>>arr[0]>>arr[1]>>arr[2]>>d;
    sort(arr,arr+3);
    if(arr[1]-arr[0]<d)
    ar=abs(d-(arr[1]-arr[0]));
    if(arr[2]-arr[1]<d)
    ar=abs(d-(arr[2]-arr[1]));
    cout<<ar<<"\n";
}

#include<bits/stdc++.h>
#include<math.h>
using namespace std;
int big(int x,int y,int z)
{
    int t;
    if(x>y)
    {
        if(x>z)
        {
            t=x;
        }
        else
        {
            t=z;
        }
    }
    else
    {
        if(y>z)
        {
            t=y;
        }
        else
        {
            t=z;
        }
    }
}

int main()
{
    int a,b,c,d,A=0,B=0,C=0,A1=0,B1=0,C1=0,sum=0;
    cin>>a>>b>>c>>d;
    C=big(a,b,c);
    if(C==a)
       {
         if(b>c)
            {
                B=b;
                A=c;
            }
            else
            {
                A=b;
                B=c;
            }
       }
       else if(C==b)
       {
         if(a>c)
            {
                B=a;
                A=c;
            }
            else
            {
                A=a;
                B=c;
            }
       }
       else
       {
         if(b>a)
            {
                B=b;
                A=a;
            }
            else
            {
                A=b;
                B=a;
            }
       }
       A1=A;
       B1=B;
       C1=C;
       if(((A+C)/2)==B)
       {
         if(abs(B-A)<d)
       {
           A1=B-d;
       }
       if(abs(C-B1)<d)
       {
           C1=B1+d;
       }
       }
       else
       {
    if(abs(B-A)<d)
       {
           B1=A+d;
       }
       if(abs(C-B1)<d)
       {
           C1=B1+d;
       }
       }
       sum=abs(A-A1)+abs(B-B1)+abs(C-C1);
       cout<<sum<<endl;
    return 0;
}

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    int a[3],b;
    for(int i=0;i<3;i++)
    cin>>a[i];
    cin>>b;
    sort(a,a+3);
    int t=b-(a[2]-a[1]);
    int tt=b-(a[1]-a[0]);

    if(t>0 && tt>0)
    cout<<t+tt;
    else if(t>0 && tt<0)
    cout<<t;
    else
    cout<<tt;

}

#include <bits/stdc++.h>
using namespace std;
int main(){
    long int d;
    vector<long int> v(3);
    cin>>v[0]>>v[1]>>v[2]>>d;
    sort(v.begin(),v.end());
    long int d1= v[1]-v[0];
    long int d2=v[2]-v[1];
    if(d1==d2==d) {
        cout<<"0"<<endl;
        }
    else if(d1<d&&d2<d){
            cout<<(2*d-d1-d2)<<endl;
        }
    else if(d1<d&&d2>=d)
        cout<<d-d1;
    else if(d1>=d&&d2<d)
        cout<<d-d2<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int arr[3],step=0,d;
    for(int i=0;i<3;i++)
    {
        cin>>arr[i];
    }
    cin>>d;
    sort(arr,arr+3);
    long long int e,f;
    e=d-abs(arr[0]-(arr[1]));
    step+=e;
    f=d-abs(arr[1]-arr[2]);
    step+=f;
    cout<<step<<endl;
}

//Bismillahir Rahmanir Rahim
#include<bits/stdc++.h>
#define ll              long long
#define ull             unsigned long long
#define pb              push_back
#define fastread()      (ios_base:: sync_with_stdio(false),cin.tie(NULL));
using namespace std;
int main()
{
    fastread();
    ll a,b,c,d,e = 0,f = 0;
    cin>>a>>b>>c>>d;
    if(a > b){
        swap(a,b);
        if(a > c){
            swap(a,c);
        }
    }
    if(b > c){
        swap(b,c);
    }
    ll ans = 0;
    e = b - a;
    f = c - b;
    if(e < d){
        ans += d - e;
    }
    if(f < d){
        ans += d - f;
    }
    cout<<ans<<endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
int main(){
    int a[3], d;
    cin >> a[0] >> a[1] >> a[2] >> d;
    sort(a, a+3);
    int x = abs(d - (a[1] - a[0]));
    int y = abs( d - (a[2] - a[1]));
    cout << x+y << endl;
}

#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define vfor(v) for(auto &&x: v)
#define pb push_back
using namespace std;
typedef long long ll;

void solve(){
    ll k,n,s,p,t,g=0;
    cin>>k>>n>>s>>p;

    if(n<=s)t=1;
    else {
        t=n/s;
        if(n%s)t++;
    }
    t*=k;
    if(t<=p)cout<<t;
    else{
        g=t/p;
        if(t%p)g++;

        cout<<g;
    }

}
int main(){
    IOS;
    solve();
    return 0;
}

#include <bits/stdc++.h>

using namespace std;

int main()
{
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int l = min(a, b);
  l = min(l, c);
  int r = max(a, b);
  r = max(r, c);
  int h = a+b+c-l-r;
  int ans = 2*d-r+l;
  cout << ans;
}

#include <bits/stdc++.h>

#define ll long long

using namespace std;

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t, a, b, c, d;

	vector<int> x(3);

	cin >> x[0] >> x[1] >> x[2] >> d;

	sort(x.begin(), x.end());

	int sum = max(0, d - (a - b)) + max(0, d - (a - c));

	cout << sum << endl;

	return 0;
}

#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll,ll> ii;
typedef pair<string,ll> si;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<si> vsi;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vsi> vvsi;

// bool sieve[100005];

// void sieveoferat()
// {
// 	for(ll p = 2; p*p<100005 ; p++)
// 	{
// 		if(sieve[p])
// 		{
// 			for(int j = p*p; j<100005; j+=p)
// 				sieve[j] = false;
// 		}
// 	}
// }

// void dp(sp_1, sp_2.....)
// {
// 	if()//exit statement
// 	if()//using the memoization
// 	ll ans = //default/dummy answer
// 	//recursions
// 			//max/min
// 	return memo[][].. = ans;

// }

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
 	cout.tie(NULL);
 	//memset(memo, -1, sizeof memo);
 	// memset(sieve, true, sizeof sieve);
 	// sieveoferat();
 	ll d;
 	ll a[3];
 	for(int i = 0 ; i <3; i++)
 		cin>>a[i];
 	cin>>d;
 	ll mini = INT_MAX;

 	for(int i= 0; i < 3; i++)
 	{
 		ll cost = 0;
 		for(int j =0 ; j < 3; j++)
 			cost = max(cost,abs(a[i]+j*d - a[(j+i)%3]));
		mini = min(mini,cost);
 		cost =0;
 		for(int j =0 ; j < 3; j++)
 			cost = max(cost,abs(a[i]-j*d - a[(j+i)%3]));
 		mini = min(mini,cost);
 	}
 	cout<<mini<<endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    long long timeC = 0;
    if(abs(a - b) < d)
    {
        long long aa = max(a, b);
        long long bb = min(a, b);
        long long x = d - aa + bb;
        timeC += x;
    }
    long long x1 = 0;
    if(abs(b - c) < d)
    {
        long long aa = max(c, b);
        long long bb = min(c, b);
        x1 = d - aa + bb;
    }
    long long x2 = 0;
    if(abs(a-c) < d)
    {
        long long aa = max(a, c);
        long long bb = min(a, c);
        x2 = d - aa + bb;
    }
    long long xx = max(x1, x2);
    timeC += xx;
    cout << timeC << endl;
	return 0;
}

#include<iostream>
#include<iomanip>

#include<algorithm>
using namespace std;

int main()
{

int arr[3],d;
cin>>arr[0]>>arr[1]>>arr[2]>>d;

sort(arr,arr+3);
if(arr[2]-arr[0]>=2*d)
{
	cout<<arr[0]+d-arr[1];
}

else{
int a=arr[1]+d-arr[2];
int b=arr[0]-(arr[1]-d);
cout<<a+b;}
	//system("pause");
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b,c,d; cin>>a>>b>>c>>d;
	if(a>b) swap(a,b);
	if(b>c) swap(b,c);
	int sum=0;
	if(b-a<d) sum+=d-(b-a);
	if(c-b<d) sum+=d-(c-b);
	cout<<sum<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD=1e9+7;
const int base = 13331;
const int MX=50;
ll arr[MX];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll d;
    cin>>arr[0]>>arr[1]>>arr[2]>>d;
    sort(arr,arr+3);
    ll a=abs((arr[1]-d)-arr[0]);
    ll b=abs((arr[1]+d)-arr[2]);
    if(abs(arr[0]-arr[1])>=d) a=0;
    if(abs(arr[1]-arr[2])>=d) b=0;
    ll ans=a+b;

    a=abs((arr[0]+d)-arr[1]);
    b=abs((arr[0]+2*d)-arr[2]);
    if(abs(arr[0]-arr[1])>=d) a=0;
    if(abs(arr[1]-arr[2])>=d) b=0;
    ans=min(ans,a+b);

    a=abs((arr[2]-d)-arr[1]);
    b=abs((arr[2]-2*d)-arr[0]);
    if(abs(arr[0]-arr[1])>=d) a=0;
    if(abs(arr[1]-arr[2])>=d) b=0;
    ans=min(ans,a+b);
    cout<<ans<<endl;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define Angola ios_base::sync_with_stdio(0),cin.tie(),cout.tie()
#define Begin int main()
Begin {
Angola;
ll a,b,c,d;
cin>>a>>b>>c>>d;
vector <ll> q;
q.push_back(a);
q.push_back(b);
q.push_back(c);
sort(q.begin(),q.end());
a = q[0];
b = q[1];
c = q[2];
ll ans = 0;
if (abs(a-b)<d) {ans+=abs(d-abs(a-b));b+=d-abs(a-b);}
if (abs(b-c)<d) {ans+=abs(d-abs(b-c));c+=d-abs(b-c);}
if (abs(a-c)<d) {ans+=abs(d-abs(a-c));c+=d-abs(a-c);}
cout<<ans;return 0;
}
/*Монакина Алёна
Вязьма, Российская Федерация
Вяземский железнодорожный техникум
Задача A
*/

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long int a,b,c,d,ara[1000];
    cin>>ara[1]>>ara[2]>>ara[0]>>d;
    sort(ara,ara+3);
    long long int no=ara[0]+d-ara[1];
    long long int no1=ara[0]+2*d-ara[2];
    cout<<max(no,no1)<<endl;
    return 0;
}

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

    }
    cout<<cnt<<endl;
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

///memset(ar,0,sizeof ar);
typedef long long int ll;
#define mk(x,y) make_pair(x,y)

#define FOR(n) for(int i=0;i<n;i++)

int to(int x,int y)
{
    return (x*60)+(y);
}

ll gcd(ll a,ll b){return b==0 ? a:gcd(b,a%b);}

int main (){
ios_base::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);

ll arr[3],arr2[3],d,ans=0 ,ans2=0;

for(int i=0;i<3;i++)cin>>arr[i],arr2[i]=arr[i];
cin>>d;
sort(arr,arr+3);
sort(arr2,arr2+3);
for(int i=1;i<3;i++)
{
    if(arr[i] -arr[i-1] < d)
    {
        int x = (d - (arr[i] - arr[i-1]));
        ans += x;
        arr[i] = arr[i] + x;
    }
}

for(int i=1;i>=0;i--)
{

    int x = abs(arr2[i]) - abs(arr2[i+1]);
    if(x < d)
    {
        x = d - x;
        ans2 += x;
        arr2[i] = arr2[i] - x;
    }
}
cout<<min(ans2,ans);

cout<<endl;
//end code please accepted
 return 0; }

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define Angola ios_base::sync_with_stdio(0),cin.tie(),cout.tie()
#define Begin int main()
Begin {
Angola;
ll a,b,c,d;
cin>>a>>b>>c>>d;
vector <ll> q;
q.push_back(a);
q.push_back(b);
q.push_back(c);
sort(q.begin(),q.end());
ll answer = 1e18;
while (next_permutation(q.begin(),q.end())) {
    a = q[0];
b = q[1];
c = q[2];
ll ans = 0;
if (abs(a-b)<d) {ans+=abs(d-abs(a-b));a+=d-abs(a-b);}
if (abs(b-c)<d) {ans+=abs(d-abs(b-c));c+=d-abs(b-c);}
if (abs(a-c)<d) {ans+=abs(d-abs(a-c));c+=d-abs(a-c);}
answer = min(answer,ans);
}
cout<<answer;return 0;
}
/*Монакина Алёна
Вязьма, Российская Федерация
Вяземский железнодорожный техникум
Задача A
*/

#include<bits/stdc++.h>
using namespace std;

int main(){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    vector<int> many_step;
//  sort(a,a+3);
//    int step = 0;
    if(abs(a-b) < d)
        many_step.push_back(d -  abs(a-b));
    if(abs(a-c) < d)
        many_step.push_back(d - abs(a-c));
    if(abs(b-c) < d)
        many_step.push_back(d - abs(b-c));
// cout << max(0,d-(a[1] - a[0])) + max(0,d - (a[2] - a[1]));

    sort(many_step.rbegin(), many_step.rend());
   cout << many_step[0] + many_step[1] << endl;
    return 0;
}

#include <bits/stdc++.h>

using namespace std;
int a[4];

int d;
int dem;
void nhap()
{
    for(int i=1;i<=3;i++)
        cin>>a[i];
    scanf("%d",&d);
}
void xuly()
{
    sort(a+1,a+3+1);
    if(a[2]-a[1]<d)
    {
        int k=d-(a[2]-a[1]);
        a[2]+=k;
        dem+=k;
    }
    sort(a+1,a+3+1);
    if(a[3]-a[2]<d)
    {
        int k=d-(a[3]-a[2]);
        a[3]+=k;
        dem+=k;
    }
    cout<<dem;
}
int main()
{
    nhap();
    xuly();
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int main()
{
    ll a,b,c,d,sum=0;
    cin>>a>>b>>c>>d;
    if((a<=b&&a>=c)||(a>=b&&a<=c))
    {
        if(abs(a-c)<d)
        sum+=d-abs(a-c);
        if(abs(a-b)<d)
        sum+=d-abs(a-b);

    }
    if((b<=c&&b>=a)||(b>=c&&b<=a))
    {
        if(abs(a-b)<d)
        sum+=d-abs(a-b);
        if(abs(b-c)<d)
        sum+=d-abs(b-c);

    }
    if((c<=a&&c>=b)||(c>=a&&c<=b))
    {
        if(abs(a-c)<d)
        sum+=d-abs(a-c);
        if(abs(b-c)<d)
        sum+=d-abs(b-c);
    }
    cout<<sum;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long
int min(int a,int b){ return a>b?b:a;}
int main()
{
  ll int arr[3];
  for(int i=0;i<3;i++)
  {
    cin>>arr[i];
  }
  ll int d;
  cin>>d;
  sort(arr,arr+3);
  ll int a = arr[0];
  ll int b = arr[1];
  ll int c = arr[2];
  if((arr[2]-arr[1])>=d&&(arr[1]-arr[0])>=d)
  {
    cout<<"0";
    return 0;
  }
  if((arr[2]-arr[0])>=2*d)
  {
    int x = min(arr[1]-arr[0],arr[2]-arr[1]);
    int ct  = d-x;
    cout<<ct;
    return 0;
  }
  else
  {
    int ct = d-(arr[1]-arr[0]);
    ct+=d-(arr[2]-arr[1]);
    cout<<ct;
  }
  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<math.h>
int main()
{
	int a, b, c, cnttmp,d,cnt;
	bool flag;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	cnt = 0;
	flag = true;
	while (flag)
	{
		flag = false;
		if (abs(a - b) < d)
		{
			flag = true;
			if (a > b)
			{
				cnttmp = abs(d - (a - b));
				b -= cnttmp;
				cnt += cnttmp;
			}
			else
			{
				cnttmp = abs(d - (b - a));
				a -= cnttmp;
				cnt += cnttmp;
			}
		}
		if (abs(a - c) < d)
		{
			flag = true;
			if (a > c)
			{
				cnttmp = abs(d - (a - c));
				c -= cnttmp;
				cnt += cnttmp;
			}
			else
			{
				cnttmp = abs(d - (c - a));
				a -= cnttmp;

			}
		}
		if (abs(c - b) < d)
		{
			flag = true;
			if (c > b)
			{
				cnttmp = abs(d - (c - b));
				b -= cnttmp;
				cnt += cnttmp;
			}
			else
			{
				cnttmp = abs(d - (b - c));
				c -= cnttmp;
				cnt += cnttmp;
			}
		}
	}

	printf("%d", cnt);
	return 0;
}

/* This a basic template for fast and efficient competitive programming*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl '\n'
#define mp make_pair
#define pb push_back
#define mod 1000000007

int main()
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);

	int a[3],d,ans =0;

	for(int i=0;i<3;i++)
		cin>>a[i];
	cin>>d;
	sort(a,a+3);
	int dif = a[2] - a[0];

	if(a[0] == a[2])
		ans = 2*d;
	else if(a[0] == a[1])
	{
		ans =d;
		if(dif < d)
			ans += d - dif;
	}
	else
	{
		if(dif >= 2*d)
		{

			int d1=a[1]-a[0],d2=a[2]-a[1];
			ans = max(0,d-min(d1,d2));
		}
		else
		{
			ans = 2*d - dif ;
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main(){
	int d,a[5],ans=0;
	for(int i=0;i<3;i++)cin>>a[i];
	cin>>d;
	sort(a,a+3);
	if(a[1]<a[0]+d)ans=a[0]+d-a[1],a[1]=a[0]+d;
	else if(a[1]>a[0]+d){
		ans=a[1]-a[0]+d;
		a[1]=a[0]+d;
	}
	if(a[2]<a[1]+d)ans=max(ans,a[1]+d-a[2]);
	cout<<ans<<endl;
}

#include <algorithm>
#include<stdio.h>
#include <iostream>
#include<cmath>
#include <vector>
#include<string>
#include<stack>
#include<set>
#include <numeric>
#include<queue>

using namespace std;

typedef  long long   ll;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	vector<ll> a(3);
	ll d;

	for (size_t i = 0; i < 3; i++)
	{
		cin >> a[i];

	}
	cin >> d;
	sort(a.begin(), a.end());
	ll sum = 0;
	sum += d - (a[1] - a[0]);

	sum += d - (a[2] - a[1]);

	cout << sum;

}

#include <iostream>
using namespace std;

int main() {
	// your code goes here
	long long int a,b,c,d,max,min,mid;
	cin>>a>>b>>c>>d;
	if(a>b && a>c)
	    max=a;
	else if(b>a && b>c)
	    max=b;
	else if(c>b && c>a)
	    max=c;
	if(a<b && a<c)
	    min=a;
	else if(b<a && b<c)
	    min=b;
	else if(c<b && c<a)
	    min=c;
	if(a==max && b==min || a==min && b==max)
	    mid=c;
    else if(b==max && c==min || b==min && c==max)
        mid=a;
    else if(c==max && a==min|| c==min && a==max)
        mid=b;
	//cout<<max<<" "<<mid<<" "<<min;
    long long int e=mid-min;
    long long int f=max-mid;
    long long int minimum;
    if(e<f)
        minimum=e;
    else
        minimum=f;
    cout<<d-minimum;
	return 0;
}

#include <stdio.h>
#include <algorithm>
using namespace std;

int main() {
	int rope[3], d, t = 0;

	scanf("%d %d %d %d", &rope[0], &rope[1], &rope[2], &d);
	sort(rope, rope + 3);
	if (rope[1] - rope[0] < d && rope[2] - rope[1]) {
		while (rope[1] - rope[0] < d) {
			t++;
			rope[0]--;
		}
		while (rope[2] - rope[1] < d) {
			t++;
			rope[2]++;
		}
	}
	else if (rope[1] - rope[0] >= d) {
		while (rope[1] - rope[0] == d) {
			t++;
			rope[1]--;
		}
		while (rope[2] - rope[1] < d) {
			t++;
			rope[2]++;
		}
	}
	else if (rope[2] - rope[1] >= d) {
		while (rope[2] - rope[1] == d) {
			t++;
			rope[1]++;
		}
		while (rope[1] - rope[0] < d) {
			t++;
			rope[0]--;
		}
	}
	printf("%d", t);
}

#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
	long long a,b,c,d;
	cin>>a>>b>>c>>d;
	if(a==b and b==c) cout<<2*d;
	else
		if(a==b)
			if(abs(c-a)<=d) cout<<a*d-abs(c-a);
			else cout<<d;
		else
			if(a==c)
				if(abs(b-a)<=d) cout<<a*d-abs(b-a);
				else cout<<d;
			else
				if(b==c)
					if(abs(c-a)<=d) cout<<a*d-abs(b-a);
					else cout<<d;
				else
					if(a<b and b<c)
						if(abs(a-b)<d and abs(c-b)<d)
							cout<<2*d-abs(c-b)-abs(b-a);
						else
							if(abs(a-b)<d and abs(c-b)>=d)
								cout<<2*d-abs(b-a);
							else
								cout<<2*d-abs(c-b);
					else
						if(b<a and a<c)
							if(abs(a-b)<d and abs(c-a)<d)
								cout<<2*d-abs(c-a)-abs(b-a);
							else
								if(abs(a-b)<d and abs(a-c)>=d)
									cout<<2*d-abs(b-a);
								else
									cout<<2*d-abs(c-a);

						else
							if(a<c and c<b)
								cout<<2*d-abs(c-a)-abs(c-b);
							else
								if(abs(c-b)<d and abs(a-c)>=d)
									cout<<2*d-abs(b-c);
								else
									cout<<2*d-abs(c-a);

	return 0;
}

//!@#$%^&*Mr.YaSwAnThSaI.NuKaVaRaPu @......
//Template(most of the template information is extracted from bellamkonda kartheek's template)
//Thanks to BK(cf handle: karthee)(my cp guru!!)
#include <bits/stdc++.h>
#include<math.h>
#include<cmath>

using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("O3")
#define lli long long int
#define forl(i,n) for(lli i=0;i<=n;++i)
#define test lli tt; cin>>tt;while(tt--)
#define print(n) cout<<n<<"\n";
#define pb(k) push_back(k);
#define fast ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);
#define ini(n) lli n;cin>>n;
#define ins(n) string n;cin>>n;
#define ina(a,n) lli a[n];for(lli i=0;i<n;i++){cin>>a[i];}
#define cint(n,k) lli(k);k=int(n)-48;
#define si size()
#define veci(v) vector < int >v;
#define vecs(v) vector < string >v;
#define vecc(v) vector < char >v;
#define sor(v) sort(v.begin(),v.end());
#define conupper(su) transform(su.begin(), su.end(), su.begin(), ::toupper);
#define conlower(sl) transform(sl.begin(), sl.end(), sl.begin(), ::tolower);
#define veccount(a,vect) count(vect.begin(), vect.end(), a);

bool isPrime(lli n)
{

    if (n <= 1)
        return false;
    for (lli i = 2; i < n; i++)
        if (n % i == 0)
            return false;

    return true;
}

int gcd(int a, int b)
{
if (b == 0)
    return a;
return gcd(b, a % b);

}
//code starts here @nys007

  int main ()
{
    fast;
    lli n=3;
    veci(v);
    while(n--){
        ini(a);
        v.pb(a)
    }
    sor(v);
    ini(d);
    lli k=v[2]-v[0];
    if(k<2*d){
        print(2*d-k);
    }
    else{
        if((v[1]-v[0])<=d){
            print(d-(v[1]-v[0]));
        }
        else if((v[2]-v[1])<=d){
            print(d-(v[2]-v[1]));

        }
        else{
            print(0);
        }
    }

}

#include <bits/stdc++.h>

#define ll long long

using namespace std;

int main() {

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int t, a, b, c, d;

	vector<int> x(3);

	cin >> x[0] >> x[1] >> x[2] >> d;

	sort(x.begin(), x.end());

	int sum = max(0, d - (a - b)) + max(0, d - (a - c));

	cout << sum << endl;

	return 0;
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long a,b,c,d;
	cin>>a>>b>>c>>d;
	long long arr[3];
	arr[0]=a;
	arr[1]=b;
	arr[2]=c;
	sort(arr,arr+3);
	if(arr[0]==arr[1]==arr[2])
	{
		cout<<2*d;
	}
	else if(arr[1]==arr[2])
	{
		if(arr[1]-arr[0]<=d)
		{
			cout<<d+arr[1]-arr[0];
		}
		else
		{
			cout<<d;
		}
	}
	else if(arr[0]==arr[1])
	{
		if(arr[2]-arr[1]<=d)
		{
			cout<<d+arr[2]-arr[1];
		}
		else
		{
			cout<<d;
		}
	}
	else
	{
		if(arr[2]-arr[1]<d && arr[1]-arr[0]<d)
			cout<<2*d-(arr[2]-arr[0]);
		else if(arr[2]-arr[1]>d && arr[1]-arr[0]>d)
		{
			cout<<'0';
		}
		else
		{
			cout<<d-min(arr[2]-arr[1],arr[1]-arr[0]);
		}
	}
}

#include <bits/stdc++.h>

#define fastcin ios_base::sync_with_stdio(0);cin.tie(0)
#define ll long long
#define pb push_back

#define pii pair<int,int>
#define pli pair<long long , int>
#define pil pair<int,long long>
#define pll pair<long long,long long>

#define all(c) c.begin(),c.end()

using namespace std;

int a,b,c,d;
int ans;

void solve(){
	if(a>c) swap(a,c);
	if(b<a) swap(a,b);
	cout << a << " " << b << " " << c << endl;
	int ans =0,ans2=0;

	if(b-a<d){
		ans += d-b+a;
		b = a + d;
	}
	if(c-b<d){
		ans += d - c + b;
	}
}

void run(){
	cin >> a >> b >> c >> d;
	solve();
	cout << ans << endl;
}

int main(){
	fastcin;
	run();
	return 0;
}

/// author: Mr.Hakimov

#include <bits/stdc++.h>
#include <chrono>

#define fi first
#define se second
#define pb push_back
#define pf push_front
#define Pb pop_back
#define Pf pop_front
#define all(x) (x).begin(), (x).end()
#define fin(s) freopen(s, "r", stdin);
#define fout(s) freopen(s, "w", stdout);

/* Just some advices:
1. If I use set/multiset, I will check it - is it correct to use set/multiset in a problem?
2. If I can't solve problem, I must write a program, which could help me to understand it much more better)
...
*/

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

typedef long long LL;
typedef long double LD;

int TN = 1;

void solve() {
    vector<LL> a(3);
    LL d;
    cin >> a[0] >> a[1] >> a[2];
    cin >> d;
    sort(all(a));

    LL sm = a[0] + a[1] + a[2];
    LL sr = sm / 3;
    LL answer = abs(a[1] - sr) + max(d - abs(a[0] - sr), 0ll) + max(d - abs(a[2] - sr), 0ll);

    LL sr1 = sm / 3 + 1;
    LL answer1 = abs(a[1] - sr1) + max(d - abs(a[0] - sr1), 0ll) + max(d - abs(a[2] - sr1), 0ll);

    LL sr2 = sm / 3 - 1;
    LL answer2 = abs(a[1] - sr2) + max(d - abs(a[0] - sr2), 0ll) + max(d - abs(a[2] - sr2), 0ll);
    cout << min(answer1, min(answer, answer2));
}

int main() {
    auto start = chrono::steady_clock::now();
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    /// =========================================
    /// fin("input.txt"); fout("output.txt");
    /// fin("file.in"); fout("file.out");
    /// cin >> TN;
    /// =========================================
    while (TN--) solve();
    auto finish = chrono::steady_clock::now();
    auto elapsed_ms = chrono::duration_cast<chrono::milliseconds>(finish - start);
    cerr << endl << "Time: " << elapsed_ms.count() << " ms\n";
    return 0;
}

#include<bits/stdc++.h>
using namespace std;
long long a[3],b,c,x,y,z,ans,ans1;
vector<long long> v;
bool o,l;
int main(){
    cin>>a[0]>>a[1]>>a[2]>>b;
    sort(a,a+3);
    c=a[1]-a[0]-b;
    l=a[2]-a[1]-b;
    if(c<=0 && l<=0)
    cout<<abs(c+l);
    else if(c<=0)
    cout<<abs(c);
    else cout<<abs(l);
}

# include<stdio.h>
# include<string.h>
# include<algorithm>
# include<math.h>
# include<iostream>
# include<string>
typedef long long ll;
using namespace std;

int a[4];

int main()
{
	int d;
	cin>>a[1]>>a[2]>>a[3]>>d;

	sort(a,a+4);

	int d1 = a[2]-a[1];
	int d2 = a[3]-a[2];

	int ans =0 ;

	if(d>a[1]-1)//左端点移不到那么远
	{
		if(d2>=d+d-a[2]+a[1])
			ans+=d-a[2]+a[1];
		else if(a[2]-a[1]<d)
		{
			ans += a[1]-1;
			ans += d-a[2]+a[1];
			a[2]+=d-a[2]+a[1];
		}
	}
	else
	{
		if(d2>=d+d-a[2]+a[1])
			ans+=d-a[2]+a[1];
		else if(a[2]-a[1]<d)
		{
			ans+=d-a[2]+a[1];
		}
	}

	if(a[3]-a[2]<d)
	{
		ans += d-a[3]+a[2];
	}

	cout<<ans;

	return 0;
}