import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    }
} import java.util.*; util.* java import java.io.*; io.* java public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    }
} Main public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    } main {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    } BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); br new BufferedReader(new InputStreamReader(System.in)) PrintWriter pw=new PrintWriter(System.out); pw new PrintWriter(System.out) String str[]=br.readLine().split(" "); str br.readLine().split(" ") br.readLine().split br.readLine() br.readLine br " " int a=Integer.parseInt(str[0]); a Integer.parseInt(str[0]) Integer.parseInt Integer str[0] str 0 int b=Integer.parseInt(str[1]); b Integer.parseInt(str[1]) Integer.parseInt Integer str[1] str 1 int c=Integer.parseInt(str[2]); c Integer.parseInt(str[2]) Integer.parseInt Integer str[2] str 2 int k=Integer.parseInt(str[3]); k Integer.parseInt(str[3]) Integer.parseInt Integer str[3] str 3 int ans=0; ans 0 if(a>=k)
        {
            ans=k;
            k=0;
        } a>=k a k {
            ans=k;
            k=0;
        } ans=k ans k k=0 k 0 if(a<k)
        {
            ans+=a;
            k-=a;
        } a<k a k {
            ans+=a;
            k-=a;
        } ans+=a ans a k-=a k a if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        } k>0 k 0 {
            if(b>k)
            k=0;
            else
            k-=b;
        } if(b>k)
            k=0;
            else
            k-=b; b>k b k k=0 k 0 k-=b k b if(k>0)
        ans-=k; k>0 k 0 ans-=k ans k pw.println(ans) pw.println pw ans pw.flush() pw.flush pw pw.close() pw.close pw String args[] args public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    }
} public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    }
} Main
import java.util.*;
import java.io.*;
public class Main
{
    public static void main(String args[])throws Exception
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        String str[]=br.readLine().split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);
        int c=Integer.parseInt(str[2]);
        int k=Integer.parseInt(str[3]);
        int ans=0;
        if(a>=k)
        {
            ans=k;
            k=0;
        }
        if(a<k)
        {
            ans+=a;
            k-=a;
        }
        if(k>0)
        {
            if(b>k)
            k=0;
            else
            k-=b;
        }
        if(k>0)
        ans-=k;
        pw.println(ans);
        pw.flush();
        pw.close();
    }
}