import java.util.Scanner;
 
class Main{
 
    public static void main(String[] args){
 
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int k = scanner.nextInt();
        scanner.close();
        
        int sum = 0;
        if(a>=k){
            sum = k;
            System.out.println(sum);
            return;
        }else{
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        }
    }
 
} import java.util.Scanner; Scanner java.util class Main{
 
    public static void main(String[] args){
 
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int k = scanner.nextInt();
        scanner.close();
        
        int sum = 0;
        if(a>=k){
            sum = k;
            System.out.println(sum);
            return;
        }else{
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        }
    }
 
} Main public static void main(String[] args){
 
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int k = scanner.nextInt();
        scanner.close();
        
        int sum = 0;
        if(a>=k){
            sum = k;
            System.out.println(sum);
            return;
        }else{
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        }
    } main {
 
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int k = scanner.nextInt();
        scanner.close();
        
        int sum = 0;
        if(a>=k){
            sum = k;
            System.out.println(sum);
            return;
        }else{
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        }
    } Scanner scanner = new Scanner(System.in); scanner new Scanner(System.in) int a = scanner.nextInt(); a scanner.nextInt() scanner.nextInt scanner int b = scanner.nextInt(); b scanner.nextInt() scanner.nextInt scanner int c = scanner.nextInt(); c scanner.nextInt() scanner.nextInt scanner int k = scanner.nextInt(); k scanner.nextInt() scanner.nextInt scanner scanner.close() scanner.close scanner int sum = 0; sum 0 if(a>=k){
            sum = k;
            System.out.println(sum);
            return;
        }else{
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        } a>=k a k {
            sum = k;
            System.out.println(sum);
            return;
        } sum = k sum k System.out.println(sum) System.out.println System.out System System.out sum return; {
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        } sum += a sum a if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            } b>=k-a b k-a k a {
                System.out.println(sum);
                return;
            } System.out.println(sum) System.out.println System.out System System.out sum return; {
                sum -= k-a-b;
                System.out.println(sum);
            } sum -= k-a-b sum k-a-b k-a-b k a b System.out.println(sum) System.out.println System.out System System.out sum String[] args args
import java.util.Scanner;
 
class Main{
 
    public static void main(String[] args){
 
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int k = scanner.nextInt();
        scanner.close();
        
        int sum = 0;
        if(a>=k){
            sum = k;
            System.out.println(sum);
            return;
        }else{
            sum += a;
            if(b>=k-a){
                System.out.println(sum);
                return;
            }else{
                sum -= k-a-b;
                System.out.println(sum);
            }
        }
    }
 
}