import java.util.Scanner;
class Main{
	public static void main(String[] args){
    Scanner sr = new Scanner(System.in);
      int A = sr.nextInt();
      int B = sr.nextInt();
      int C = sr.nextInt();
      int K = sr.nextInt();
      //System.out.print(A +""+ B +""+C+ ""+K );
      if ( K <= A)
        System.out.print(K);
      else
        if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); 
    }
} import java.util.Scanner; Scanner java.util class Main{
	public static void main(String[] args){
    Scanner sr = new Scanner(System.in);
      int A = sr.nextInt();
      int B = sr.nextInt();
      int C = sr.nextInt();
      int K = sr.nextInt();
      //System.out.print(A +""+ B +""+C+ ""+K );
      if ( K <= A)
        System.out.print(K);
      else
        if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); 
    }
} Main public static void main(String[] args){
    Scanner sr = new Scanner(System.in);
      int A = sr.nextInt();
      int B = sr.nextInt();
      int C = sr.nextInt();
      int K = sr.nextInt();
      //System.out.print(A +""+ B +""+C+ ""+K );
      if ( K <= A)
        System.out.print(K);
      else
        if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); 
    } main {
    Scanner sr = new Scanner(System.in);
      int A = sr.nextInt();
      int B = sr.nextInt();
      int C = sr.nextInt();
      int K = sr.nextInt();
      //System.out.print(A +""+ B +""+C+ ""+K );
      if ( K <= A)
        System.out.print(K);
      else
        if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); 
    } Scanner sr = new Scanner(System.in); sr new Scanner(System.in) int A = sr.nextInt(); A sr.nextInt() sr.nextInt sr int B = sr.nextInt(); B sr.nextInt() sr.nextInt sr int C = sr.nextInt(); C sr.nextInt() sr.nextInt sr int K = sr.nextInt(); K sr.nextInt() sr.nextInt sr if ( K <= A)
        System.out.print(K);
      else
        if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); K <= A K A System.out.print(K) System.out.print System.out System System.out K if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); K <= A+B K A+B A B System.out.print(A) System.out.print System.out System System.out A System.out.print(2*A+B-K) System.out.print System.out System System.out 2*A+B-K 2*A+B 2*A 2 A B K String[] args args
import java.util.Scanner;
class Main{
	public static void main(String[] args){
    Scanner sr = new Scanner(System.in);
      int A = sr.nextInt();
      int B = sr.nextInt();
      int C = sr.nextInt();
      int K = sr.nextInt();
      //System.out.print(A +""+ B +""+C+ ""+K );
      if ( K <= A)
        System.out.print(K);
      else
        if(K <= A+B)
          System.out.print(A);
        else
          System.out.print(2*A+B-K); 
    }
}