import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args)throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    }

    public static void algo(int n1,int n2,int n3,int max)
    {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    }


} import javax.swing.*; swing.* javax import java.io.BufferedReader; BufferedReader java.io import java.io.InputStreamReader; InputStreamReader java.io public class Main {
    public static void main(String[] args)throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    }

    public static void algo(int n1,int n2,int n3,int max)
    {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    }


} Main public static void main(String[] args)throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    } main {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) String[] s=br.readLine().split(" "); s br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int n1=Integer.parseInt(s[0]); n1 Integer.parseInt(s[0]) Integer.parseInt Integer s[0] s 0 int n2=Integer.parseInt(s[1]); n2 Integer.parseInt(s[1]) Integer.parseInt Integer s[1] s 1 int n3=Integer.parseInt(s[2]); n3 Integer.parseInt(s[2]) Integer.parseInt Integer s[2] s 2 int max=Integer.parseInt(s[3]); max Integer.parseInt(s[3]) Integer.parseInt Integer s[3] s 3 algo(n1,n2,n3,max) algo n1 n2 n3 max String[] args args public static void algo(int n1,int n2,int n3,int max)
    {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    } algo {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    } int sum=0; sum 0 if(n1>=max)
        {
            System.out.println(max);
            return;
        } n1>=max n1 max {
            System.out.println(max);
            return;
        } System.out.println(max) System.out.println System.out System System.out max return; max=max-n1 max max-n1 max n1 sum+=n1 sum n1 if(n2>=max)
        {
            System.out.println(sum);
            return;
        } n2>=max n2 max {
            System.out.println(sum);
            return;
        } System.out.println(sum) System.out.println System.out System System.out sum return; max-=n2 max n2 sum-=max sum max System.out.println(sum) System.out.println System.out System System.out sum int n1 n1 int n2 n2 int n3 n3 int max max public class Main {
    public static void main(String[] args)throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    }

    public static void algo(int n1,int n2,int n3,int max)
    {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    }


} public class Main {
    public static void main(String[] args)throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    }

    public static void algo(int n1,int n2,int n3,int max)
    {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    }


} Main
import javax.swing.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args)throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String[] s=br.readLine().split(" ");

        int n1=Integer.parseInt(s[0]);
        int n2=Integer.parseInt(s[1]);
        int n3=Integer.parseInt(s[2]);
        int max=Integer.parseInt(s[3]);
        algo(n1,n2,n3,max);
    }

    public static void algo(int n1,int n2,int n3,int max)
    {
        int sum=0;
        if(n1>=max)
        {
            System.out.println(max);
            return;
        }
        max=max-n1;
        sum+=n1;
        if(n2>=max)
        {
            System.out.println(sum);
            return;
        }
        max-=n2;
        sum-=max;
        System.out.println(sum);
    }


}
