import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    }
} import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    }
} Main public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    } main {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] strArray = br.readLine().split(" "); strArray br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int A = Integer.parseInt(strArray[0]); A Integer.parseInt(strArray[0]) Integer.parseInt Integer strArray[0] strArray 0 int B = Integer.parseInt(strArray[1]); B Integer.parseInt(strArray[1]) Integer.parseInt Integer strArray[1] strArray 1 int C = Integer.parseInt(strArray[2]); C Integer.parseInt(strArray[2]) Integer.parseInt Integer strArray[2] strArray 2 int K = Integer.parseInt(strArray[3]); K Integer.parseInt(strArray[3]) Integer.parseInt Integer strArray[3] strArray 3 int sum = 0; sum 0 if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	} A > 0 A 0 {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	} if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    } K - A > 0 K - A K A 0 {
		sum += A;
		K -= A;
	    } sum += A sum A K -= A K A {
		sum = K;
		K = 0;
	    } sum = K sum K K = 0 K 0 if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	} K > 0 && B > 0 K > 0 K 0 B > 0 B 0 {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	} if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    } K - B > 0 K - B K B 0 {
		K -= B;
	    } K -= B K B {
		K = 0;
	    } K = 0 K 0 if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	} K > 0 && C > 0 K > 0 K 0 C > 0 C 0 {
	    sum = sum + (K * -1);
	} sum = sum + (K * -1) sum sum + (K * -1) sum (K * -1) K -1 1 System.out.println(sum) System.out.println System.out System System.out sum String args[] args public class Main {
    public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    }
} public class Main {
    public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    }
} Main
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	String[] strArray = br.readLine().split(" ");
	int A = Integer.parseInt(strArray[0]);
	int B = Integer.parseInt(strArray[1]);
	int C = Integer.parseInt(strArray[2]);
	int K = Integer.parseInt(strArray[3]);
	int sum = 0;

	if (A > 0) {
	    if (K - A > 0) {
		sum += A;
		K -= A;
	    } else {
		sum = K;
		K = 0;
	    }
	}

	if (K > 0 && B > 0) {
	    if (K - B > 0) {
		K -= B;
	    } else {
		K = 0;
	    }
	}

	if (K > 0 && C > 0) {
	    sum = sum + (K * -1);
	}

	System.out.println(sum);
    }
}