//package Beg_167;
import java.util.*;

public class Main{
	public static void main(String[] args) {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	}
} import java.util.*; util.* java public class Main{
	public static void main(String[] args) {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	}
} Main public static void main(String[] args) {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	} main {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	} Scanner t = new Scanner(System.in); t new Scanner(System.in) String p = t.nextLine(); p t.nextLine() t.nextLine t String[] l = p.split(" "); l p.split(" ") p.split p " " long [] list=new long[4]; list new long[4] 4 for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		} int i=0; int i=0; i 0 i<l.length i l.length l l.length i++ i++ i {
			list[i]=Long.parseLong(l[i]);
			
		} {
			list[i]=Long.parseLong(l[i]);
			
		} list[i]=Long.parseLong(l[i]) list[i] list i Long.parseLong(l[i]) Long.parseLong Long l[i] l i long a = list[0]; a list[0] list 0 long b=list[1]; b list[1] list 1 long c=list[2]; c list[2] list 2 long k=list[3]; k list[3] list 3 if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		} a>=k a k {
			System.out.println(k);
		} System.out.println(k) System.out.println System.out System System.out k if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		} (a+b)>k (a+b) a b k {
			System.out.println(a);
		} System.out.println(a) System.out.println System.out System System.out a {
			long q = k-a-b;
			System.out.println(a-q);
		} long q = k-a-b; q k-a-b k-a-b k a b System.out.println(a-q) System.out.println System.out System System.out a-q a q String[] args args public class Main{
	public static void main(String[] args) {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	}
} public class Main{
	public static void main(String[] args) {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	}
} Main
//package Beg_167;
import java.util.*;

public class Main{
	public static void main(String[] args) {
		Scanner t = new Scanner(System.in);
		String p = t.nextLine();
		String[] l = p.split(" ");
		long [] list=new long[4];
		for (int i=0;i<l.length;i++) {
			list[i]=Long.parseLong(l[i]);
			
		}
		long a = list[0];
		long b=list[1];
		long c=list[2];
		long k=list[3];
		
		if (a>=k) {
			System.out.println(k);
		}
		else if ((a+b)>k) {
			System.out.println(a);
		}
		else {
			long q = k-a-b;
			System.out.println(a-q);
		}
	}
}