import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	}
	
} import java.util.*; util.* java public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	}
	
} Main public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	} main {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	} Scanner in = new Scanner(System.in); in new Scanner(System.in) int a = in.nextInt(); a in.nextInt() in.nextInt in int b = in.nextInt(); b in.nextInt() in.nextInt in int c = in.nextInt(); c in.nextInt() in.nextInt in int k = in.nextInt(); k in.nextInt() in.nextInt in int total =0; total 0 total += Math.min(a, k) total Math.min(a, k) Math.min Math a k k -= Math.min(a, k) k Math.min(a, k) Math.min Math a k if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}} k==0 k 0 {
			System.out.println(total);} System.out.println(total) System.out.println System.out System System.out total {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}} k-= Math.min(b, k) k Math.min(b, k) Math.min Math b k if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);} k==0 k 0 {
				System.out.println(total);} System.out.println(total) System.out.println System.out System System.out total {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);} total -= Math.min(c, k) total Math.min(c, k) Math.min Math c k k -= Math.min(c, k) k Math.min(c, k) Math.min Math c k System.out.println(total) System.out.println System.out System System.out total String[] args args public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	}
	
} public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	}
	
} Main
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a = in.nextInt();
		int b = in.nextInt();
		int c = in.nextInt();
		int k = in.nextInt();
		int total =0;
		total += Math.min(a, k);
		k -= Math.min(a, k);
		if (k==0) {
			System.out.println(total);}
		else {
			k-= Math.min(b, k);
			if (k==0) {
				System.out.println(total);}
			else {
				total -= Math.min(c, k);
				k -= Math.min(c, k);
				System.out.println(total);}}
	}
	
}