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 x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    }
} 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 x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    }
} 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 x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    } main {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        int c = in.nextInt();
        int k = in.nextInt();
        int x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    } 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 x =0; x 0 if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        } k<=a k a {
            System.out.println(k);
        } System.out.println(k) System.out.println System.out System System.out k {
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        } x=a x a k-=a k a if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            } k<=b k b {
                System.out.println(x);
            } System.out.println(x) System.out.println System.out System System.out x {
                k-=b;
                
                System.out.println(x+(k*-1));
            } k-=b k b System.out.println(x+(k*-1)) System.out.println System.out System System.out x+(k*-1) x (k*-1) k -1 1 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 x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    }
} 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 x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    }
} 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 x =0;
        if(k<=a){
            System.out.println(k);
        }
        else{
            x=a;
            k-=a;
            if(k<=b){
                System.out.println(x);
            }
            else{
                k-=b;
                
                System.out.println(x+(k*-1));
            }
        }
    }
}