import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	}
} import java.util.*; util.* java import java.math.*; math.* java public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	}
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	} main {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	} Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long a = sc.nextLong(); a sc.nextLong() sc.nextLong sc long b = sc.nextLong(); b sc.nextLong() sc.nextLong sc long c = sc.nextLong(); c sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        } a+b<=k a+b a b k {
            long ans = a-(k-a-b);
            System.out.println(ans);
        } long ans = a-(k-a-b); ans a-(k-a-b) a (k-a-b) k-a-b k a b System.out.println(ans) System.out.println System.out System System.out ans if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        } a<=k a k {
            System.out.println(a);
        } System.out.println(a) System.out.println System.out System System.out a if(a>k){
            System.out.println(k);
        } a>k a k {
            System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k String[] args args public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	}
} public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	}
} Main
import java.util.*;
import java.math.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        long a = sc.nextLong();
        long b = sc.nextLong();
        long c = sc.nextLong();
        long k = sc.nextLong();
        if(a+b<=k){
            long ans = a-(k-a-b);
            System.out.println(ans);
        } else if(a<=k){
            System.out.println(a);
        } else if(a>k){
            System.out.println(k);
        }
	}
}