import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	}
} import java.util.Scanner; Scanner java.util public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	}
} Main public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	} main {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	} Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = scan.nextInt(); a scan.nextInt() scan.nextInt scan int b = scan.nextInt(); b scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int k = scan.nextInt(); k scan.nextInt() scan.nextInt scan int sum = 0; sum 0 if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		} a <= k a k {
			k -= a;
			sum += a;
		} k -= a k a sum += a sum a {
			sum += k;
			k = 0;
		} sum += k sum k k = 0 k 0 if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		} k == 0 k 0 {

		} if(b <= k){
			k -= b;
		}else {
			k = 0;
		} b <= k b k {
			k -= b;
		} k -= b k b {
			k = 0;
		} k = 0 k 0 if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		} k == 0 k 0 {

		} if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		} c <= k c k {
			k -= c;
			sum -= c;
		} k -= c k c sum -= c sum c {
			sum -= k;
			k = 0;
		} sum -= k sum k k = 0 k 0 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) {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	}
} public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	}
} Main
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);

		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();
		int sum = 0;

		if(a <= k) {
			k -= a;
			sum += a;
		}else {
			sum += k;
			k = 0;
		}

		if(k == 0) {

		}else if(b <= k){
			k -= b;
		}else {
			k = 0;
		}

		if(k == 0) {

		}else if(c <= k){
			k -= c;
			sum -= c;
		}else {
			sum -= k;
			k = 0;
		}

		System.out.println(sum);
	}
}
