import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        }
        if(b>=k){
            k -= k;
        }else{
            k-=b;
        }
        if(k>0){
            sum -= k;
        }
        System.out.println(sum);
    }
} import java.util.Scanner; Scanner java.util public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        }
        if(b>=k){
            k -= k;
        }else{
            k-=b;
        }
        if(k>0){
            sum -= k;
        }
        System.out.println(sum);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        }
        if(b>=k){
            k -= k;
        }else{
            k-=b;
        }
        if(k>0){
            sum -= k;
        }
        System.out.println(sum);
    } main {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        }
        if(b>=k){
            k -= k;
        }else{
            k-=b;
        }
        if(k>0){
            sum -= k;
        }
        System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int a = sc.nextInt(); a sc.nextInt() sc.nextInt sc int b = sc.nextInt(); b sc.nextInt() sc.nextInt sc int c = sc.nextInt(); c sc.nextInt() sc.nextInt sc int k = sc.nextInt(); k sc.nextInt() sc.nextInt sc int sum = 0; sum 0 if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        } a>=k a k {
            sum = k;
            k=0;
        } sum = k sum k k=0 k 0 {
            sum = a;
            k -= a;
        } sum = a sum a k -= a k a if(b>=k){
            k -= k;
        }else{
            k-=b;
        } b>=k b k {
            k -= k;
        } k -= k k k {
            k-=b;
        } k-=b k b if(k>0){
            sum -= k;
        } k>0 k 0 {
            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 sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        }
        if(b>=k){
            k -= k;
        }else{
            k-=b;
        }
        if(k>0){
            sum -= k;
        }
        System.out.println(sum);
    }
} public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int k = sc.nextInt();
        int sum = 0;
        if(a>=k) {
            sum = k;
            k=0;
        }else{
            sum = a;
            k -= a;
        }
        if(b>=k){
            k -= k;
        }else{
            k-=b;
        }
        if(k>0){
            sum -= k;
        }
        System.out.println(sum);
    }
} Main
import java.util.Scanner;

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