import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    }
} Main public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    } main {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    } Scanner cin = new Scanner(System.in); cin new Scanner(System.in) int a = cin.nextInt(); a cin.nextInt() cin.nextInt cin int b = cin.nextInt(); b cin.nextInt() cin.nextInt cin int c = cin.nextInt(); c cin.nextInt() cin.nextInt cin int k = cin.nextInt(); k cin.nextInt() cin.nextInt cin int sum = 0; sum 0 int m = Math.min(a, k); m Math.min(a, k) Math.min Math a k sum+=m sum m k-=m k m m = Math.min(b, k) m Math.min(b, k) Math.min Math b k k-=m k m if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k); k==0 k 0 System.out.println(sum) System.out.println System.out System System.out sum System.out.println(sum-k) System.out.println System.out System System.out sum-k sum k String[] args args public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    }
} public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    }
} Main
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int a = cin.nextInt();
        int b = cin.nextInt();
        int c = cin.nextInt();
        int k = cin.nextInt();
        int sum = 0;
        int m = Math.min(a, k);
        sum+=m;
        k-=m;
        m = Math.min(b, k);
        k-=m;
        if(k==0)
            System.out.println(sum);
        else
            System.out.println(sum-k);

    }
}
