import java.util.*;
import java.io.*;
public class Main
{
    
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	}
}
 import java.util.*; util.* java import java.io.*; io.* java public class Main
{
    
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	}
} Main public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	} main {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	} BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s = br.readLine().trim().split(" "); s br.readLine().trim().split(" ") br.readLine().trim().split br.readLine().trim() br.readLine().trim br.readLine() br.readLine br " " int a = Integer.parseInt(s[0]); a Integer.parseInt(s[0]) Integer.parseInt Integer s[0] s 0 int b = Integer.parseInt(s[1]); b Integer.parseInt(s[1]) Integer.parseInt Integer s[1] s 1 int c = Integer.parseInt(s[2]); c Integer.parseInt(s[2]) Integer.parseInt Integer s[2] s 2 int k = Integer.parseInt(s[3]); k Integer.parseInt(s[3]) Integer.parseInt Integer s[3] s 3 int ans = 0; ans 0 if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    } k >= a k a {
	        ans += a;
	        k -= a;
	    } ans += a ans a k -= a k a {
	        ans = k;
	        k = 0;
	    } ans = k ans k k = 0 k 0 if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    } k >= b k b {
	        k -= b;
	    } k -= b k b {
	        k = 0;
	    } k = 0 k 0 if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    } k >= c k c {
	        ans -= c;
	        k -= c;
	    } ans -= c ans c k -= c k c {
	        ans = ans-k;
	    } ans = ans-k ans ans-k ans k System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main
{
    
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	}
} public class Main
{
    
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	}
} Main
import java.util.*;
import java.io.*;
public class Main
{
    
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] s = br.readLine().trim().split(" ");
		int a = Integer.parseInt(s[0]);
		int b = Integer.parseInt(s[1]);
		int c = Integer.parseInt(s[2]);
		int k = Integer.parseInt(s[3]);
		int ans = 0;
	    if(k >= a){
	        ans += a;
	        k -= a;
	    }
	    else{
	        ans = k;
	        k = 0;
	    }

	    if(k >= b){
	        k -= b;
	    }
	    else{
	        k = 0;
	    }
	
	    if(k >= c){
	        ans -= c;
	        k -= c;
	    }
	    else{
	        ans = ans-k;
	    }
	    System.out.println(ans);
	}
}

