import java.util.Scanner;

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 sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    }
} import java.util.Scanner; Scanner 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 sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    }
} 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 sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    } main {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    } 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 sum = 0; sum 0 if(A>=K) {
			sum += K;
			K=0;
		}
		else if(A<K) {
			sum += A*1;
			K -= A;
		} A>=K A K {
			sum += K;
			K=0;
		} sum += K sum K K=0 K 0 if(A<K) {
			sum += A*1;
			K -= A;
		} A<K A K {
			sum += A*1;
			K -= A;
		} sum += A*1 sum A*1 A 1 K -= A K A if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		} B>=K && K != 0 B>=K B K K != 0 K 0 {
			sum += K*0;
			K = 0;
		} sum += K*0 sum K*0 K 0 K = 0 K 0 if(B<K) {
			sum += B*0;
			K -= B;
		} B<K B K {
			sum += B*0;
			K -= B;
		} sum += B*0 sum B*0 B 0 K -= B K B if(C>=K && K != 0) {
			sum += K*-1;
			K = 0;
		}
		else if(C<K) {
			sum += C*-1;
			K -= C;
		} C>=K && K != 0 C>=K C K K != 0 K 0 {
			sum += K*-1;
			K = 0;
		} sum += K*-1 sum K*-1 K -1 1 K = 0 K 0 if(C<K) {
			sum += C*-1;
			K -= C;
		} C<K C K {
			sum += C*-1;
			K -= C;
		} sum += C*-1 sum C*-1 C -1 1 K -= C K C 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 sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();
		int K = sc.nextInt();
		int sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    }
} 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 sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    }
} Main
import java.util.Scanner;

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 sum = 0;

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

		if(B>=K && K != 0) {
			sum += K*0;
			K = 0;
		}
		else if(B<K) {
			sum += B*0;
			K -= B;
		}

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

		System.out.println(sum);

    }
}
