import java.util.Scanner;

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

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
        Scanner o = new Scanner(System.in);
        int a = o.nextInt();
        int b = o.nextInt();
        int c = o.nextInt();
        int k = o.nextInt();

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    }
} Main public static void main(String[] args){
        Scanner o = new Scanner(System.in);
        int a = o.nextInt();
        int b = o.nextInt();
        int c = o.nextInt();
        int k = o.nextInt();

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    } main {
        Scanner o = new Scanner(System.in);
        int a = o.nextInt();
        int b = o.nextInt();
        int c = o.nextInt();
        int k = o.nextInt();

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    } Scanner o = new Scanner(System.in); o new Scanner(System.in) int a = o.nextInt(); a o.nextInt() o.nextInt o int b = o.nextInt(); b o.nextInt() o.nextInt o int c = o.nextInt(); c o.nextInt() o.nextInt o int k = o.nextInt(); k o.nextInt() o.nextInt o int ans = 0; ans 0 ans += Math.min(k,a) ans Math.min(k,a) Math.min Math k a k -= ans k ans a -= ans a ans int x = Math.min(k,b); x Math.min(k,b) Math.min Math k b k -= x k x b -= x b x int y = Math.min(k,c); y Math.min(k,c) Math.min Math k c ans -= y ans y System.out.println(ans) System.out.println System.out System System.out ans String[] args args public class Main{
    public static void main(String[] args){
        Scanner o = new Scanner(System.in);
        int a = o.nextInt();
        int b = o.nextInt();
        int c = o.nextInt();
        int k = o.nextInt();

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    }
} public class Main{
    public static void main(String[] args){
        Scanner o = new Scanner(System.in);
        int a = o.nextInt();
        int b = o.nextInt();
        int c = o.nextInt();
        int k = o.nextInt();

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    }
} Main
import java.util.Scanner;

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

        int ans = 0;
        ans += Math.min(k,a);
        k -= ans;
        a -= ans;
        int x = Math.min(k,b);
        k -= x;
        b -= x;
        int y = Math.min(k,c);
        ans -= y;

        System.out.println(ans);
    }
}