/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
//2 1 1 3

import java.util.*;

public class Main
{
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	}
} import java.util.*; util.* java public class Main
{
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	}
} Main public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	} main {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a= sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int count =0; count 0 int res=0; res 0 if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	} k>0 k 0 {
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	} if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    } a>k a k {
	        res+=k;
	    } res+=k res k {
	        res+=a;
	        k-=a;
	    } res+=a res a k-=a k a if(k>0){
	    k-=b;
	} k>0 k 0 {
	    k-=b;
	} k-=b k b if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	} k>0 k 0 {
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	} if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    } c>k c k {
	        res-=k;
	    } res-=k res k {
	        res-=c;
	        
	    } res-=c res c System.out.println(res) System.out.println System.out System System.out res String[] args args public class Main
{
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	}
} public class Main
{
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	}
} Main
/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
//2 1 1 3

import java.util.*;

public class Main
{
	public static void main(String[] args) {
	Scanner sc = new Scanner(System.in);
	int a= sc.nextInt();
	int b = sc.nextInt();
	int c = sc.nextInt();
	int k = sc.nextInt();
	int count =0;
	int res=0;
	
	if(k>0){
	    if(a>k){
	        res+=k;
	    }else{
	        res+=a;
	        k-=a;
	    }
	}
	if(k>0){
	    k-=b;
	}
	if(k>0){
	    if(c>k){
	        res-=k;
	    }else{
	        res-=c;
	        
	    }
	}
	System.out.println(res);
// 	while(k>0){
// 	    count=a;
// 	  System.out.println(k+"a");
// 	    k-=count;
// 	      System.out.println(k+"av");
// 	    k-= b;
// 	      System.out.println(k+"an");
// 	    count= count -c;
// 	      System.out.println(k+"am");
// 	    k-= c;
// 	      System.out.println(k+"ao");
// 	}
// 	     System.out.println(count);

	}
}
