import java.util.Scanner;

public class Main {
    static Scanner stdIn = new Scanner(System.in);
    public static void main(String[] args){
        long a = stdIn.nextLong();
        long b = stdIn.nextLong();
        long c = stdIn.nextLong();
        long k = stdIn.nextLong();

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

        if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        }
    }
} Main static Scanner stdIn = new Scanner(System.in); stdIn new Scanner(System.in) public static void main(String[] args){
        long a = stdIn.nextLong();
        long b = stdIn.nextLong();
        long c = stdIn.nextLong();
        long k = stdIn.nextLong();

        if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        }
    } main {
        long a = stdIn.nextLong();
        long b = stdIn.nextLong();
        long c = stdIn.nextLong();
        long k = stdIn.nextLong();

        if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        }
    } long a = stdIn.nextLong(); a stdIn.nextLong() stdIn.nextLong stdIn long b = stdIn.nextLong(); b stdIn.nextLong() stdIn.nextLong stdIn long c = stdIn.nextLong(); c stdIn.nextLong() stdIn.nextLong stdIn long k = stdIn.nextLong(); k stdIn.nextLong() stdIn.nextLong stdIn if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        } k <= a k a {
            System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        } k <= a + b k a + b a b {
            System.out.println(a);
        } System.out.println(a) System.out.println System.out System System.out a {
            System.out.println(a - (k - a - b));
        } System.out.println(a - (k - a - b)) System.out.println System.out System System.out a - (k - a - b) a (k - a - b) k - a - b k a b String[] args args public class Main {
    static Scanner stdIn = new Scanner(System.in);
    public static void main(String[] args){
        long a = stdIn.nextLong();
        long b = stdIn.nextLong();
        long c = stdIn.nextLong();
        long k = stdIn.nextLong();

        if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        }
    }
} public class Main {
    static Scanner stdIn = new Scanner(System.in);
    public static void main(String[] args){
        long a = stdIn.nextLong();
        long b = stdIn.nextLong();
        long c = stdIn.nextLong();
        long k = stdIn.nextLong();

        if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        }
    }
} Main
import java.util.Scanner;

public class Main {
    static Scanner stdIn = new Scanner(System.in);
    public static void main(String[] args){
        long a = stdIn.nextLong();
        long b = stdIn.nextLong();
        long c = stdIn.nextLong();
        long k = stdIn.nextLong();

        if(k <= a){
            System.out.println(k);
        }else if(k <= a + b){
            System.out.println(a);
        }else{
            System.out.println(a - (k - a - b));
        }
    }
}