import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    }
} import java.util.*; util.* java public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    }
} Main public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    } main {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    } Scanner input = new Scanner(System.in); input new Scanner(System.in) int a = input.nextInt(); a input.nextInt() input.nextInt input int b = input.nextInt(); b input.nextInt() input.nextInt input int c = input.nextInt(); c input.nextInt() input.nextInt input int k = input.nextInt(); k input.nextInt() input.nextInt input int ans = 0; ans 0 int count = 0; count 0 int tmp; tmp tmp = Math.min(a,k) tmp Math.min(a,k) Math.min Math a k count += tmp * 1 count tmp * 1 tmp 1 ans += tmp * 1 ans tmp * 1 tmp 1 k = Math.max(k-tmp,0) k Math.max(k-tmp,0) Math.max Math k-tmp k tmp 0 tmp = Math.min(b,k) tmp Math.min(b,k) Math.min Math b k count += tmp * 1 count tmp * 1 tmp 1 ans += tmp * 0 ans tmp * 0 tmp 0 k = Math.max(k-tmp,0) k Math.max(k-tmp,0) Math.max Math k-tmp k tmp 0 tmp = Math.min(c,k) tmp Math.min(c,k) Math.min Math c k count += tmp * 1 count tmp * 1 tmp 1 ans -= tmp * 1 ans tmp * 1 tmp 1 k = Math.max(k-tmp,0) k Math.max(k-tmp,0) Math.max Math k-tmp k tmp 0 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 input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    }
} Main
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        int b = input.nextInt();
        int c = input.nextInt();
        int k = input.nextInt();
        int ans = 0;
        int count = 0;
        int tmp;
        tmp = Math.min(a,k);
        count += tmp * 1;
        ans += tmp * 1;
        k = Math.max(k-tmp,0);
        tmp = Math.min(b,k);
        count += tmp * 1;
        ans += tmp * 0;
        k = Math.max(k-tmp,0);
        tmp = Math.min(c,k);
        count += tmp * 1;
        ans -= tmp * 1;
        k = Math.max(k-tmp,0);
        System.out.println(ans);
    }
}