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