import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    }
} Main public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    } main {
        Scanner sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int card1 = sc.nextInt(); card1 sc.nextInt() sc.nextInt sc int card2 = sc.nextInt(); card2 sc.nextInt() sc.nextInt sc int card3 = sc.nextInt(); card3 sc.nextInt() sc.nextInt sc int card = sc.nextInt(); card sc.nextInt() sc.nextInt sc sc.close() sc.close sc int i = 0; i 0 int j = 0; j 0 int k = 0; k 0 if(card>card1){
            i=card1;
        }else{
            i=card;
        } card>card1 card card1 {
            i=card1;
        } i=card1 i card1 {
            i=card;
        } i=card i card card=card-card1 card card-card1 card card1 if(card<0){
            card=0;
        } card<0 card 0 {
            card=0;
        } card=0 card 0 if(card>card2){
            j=card2;
        }else{
            j=card;
        } card>card2 card card2 {
            j=card2;
        } j=card2 j card2 {
            j=card;
        } j=card j card card=card-card2 card card-card2 card card2 if(card<0){
            card=0;
        } card<0 card 0 {
            card=0;
        } card=0 card 0 if(card>card3){
            k=card3;
        }else{
            k=card;
        } card>card3 card card3 {
            k=card3;
        } k=card3 k card3 {
            k=card;
        } k=card k card int ans=0; ans 0 for(int num1=0;num1<i; num1++){
            ans++;
        } int num1=0; int num1=0; num1 0 num1<i num1 i num1++ num1++ num1 {
            ans++;
        } {
            ans++;
        } ans++ ans for(int num2=0; num2<k; num2++){
            ans --;
        } int num2=0; int num2=0; num2 0 num2<k num2 k num2++ num2++ num2 {
            ans --;
        } {
            ans --;
        } ans -- ans 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 sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    }
} public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    }
} Main
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int card1 = sc.nextInt();
        int card2 = sc.nextInt();
        int card3 = sc.nextInt();
        int card = sc.nextInt();
        sc.close();

        int i = 0;
        int j = 0;
        int k = 0;
        if(card>card1){
            i=card1;
        }else{
            i=card;
        }
        card=card-card1;
        if(card<0){
            card=0;
        }
        if(card>card2){
            j=card2;
        }else{
            j=card;
        }
        card=card-card2;
        if(card<0){
            card=0;
        }
        if(card>card3){
            k=card3;
        }else{
            k=card;
        }
        int ans=0;
        for(int num1=0;num1<i; num1++){
            ans++;
        }
        for(int num2=0; num2<k; num2++){
            ans --;
        }
        System.out.println(ans);
    }
}