import java.util.Scanner;
 
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 sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    }
} import java.util.Scanner; Scanner 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 sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    }
} 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 sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    } main {
        Scanner in=new Scanner(System.in);
        int a=in.nextInt();
        int b=in.nextInt();
        int c=in.nextInt();
        int k=in.nextInt();
        int sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    } 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 sum=a sum a min=0; min 0 if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        } (a+b)<=k (a+b) a b k {
            min=k-(a+b);
            sum=sum-min;
        } min=k-(a+b) min k-(a+b) k (a+b) a b sum=sum-min sum sum-min sum min if(a>k){
            sum=k;
        } a>k a k {
            sum=k;
        } sum=k sum k System.out.println(sum) System.out.println System.out System System.out sum 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 sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    }
} 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 sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    }
} Main
import java.util.Scanner;
 
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 sum=a,min=0;
        if((a+b)<=k){
            min=k-(a+b);
            sum=sum-min;
        }else if(a>k){
            sum=k;
        }
        System.out.println(sum);
    }
}