import java.util.Scanner;
public 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();

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

        sum -= k;
        System.out.println(sum);

    }
} import java.util.Scanner; Scanner java.util public 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();

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

        sum -= k;
        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();

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

        sum -= k;
        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();

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

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

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

        sum -= k;
        System.out.println(sum);

    }
} public 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();

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

        sum -= k;
        System.out.println(sum);

    }
} Main
import java.util.Scanner;
public 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();

        int sum = 0;

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

        if(b <= k){
            k = k - b;
            if(k == 0){
                System.out.println(sum);
                return;
            }
        }else{
            System.out.println(sum);
            return;
        }

        sum -= k;
        System.out.println(sum);

    }
}
