import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  }
} import java.util.*; util.* java public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  }
} Main public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  } main {
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  } Scanner sc =new Scanner(System.in); sc new Scanner(System.in) long A =sc.nextLong(); A sc.nextLong() sc.nextLong sc long B =sc.nextLong(); B sc.nextLong() sc.nextLong sc long C =sc.nextLong(); C sc.nextLong() sc.nextLong sc long K =sc.nextLong(); K sc.nextLong() sc.nextLong sc if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);} K<A K A {System.out.println(K);} System.out.println(K) System.out.println System.out System System.out K if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);} K<A+B K A+B A B {System.out.println(A);} System.out.println(A) System.out.println System.out System System.out A {long D =A*2+B-K;
         System.out.println(D);} long D =A*2+B-K; D A*2+B-K A*2+B A*2 A 2 B K System.out.println(D) System.out.println System.out System System.out D String[] args args public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  }
} public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  }
} Main
import java.util.*;
public class Main{
  public static void main(String[] args){
    Scanner sc =new Scanner(System.in);
    long A =sc.nextLong();
    long B =sc.nextLong();
    long C =sc.nextLong();
    long K =sc.nextLong();
    if(K<A){System.out.println(K);}
    else if(K<A+B){System.out.println(A);}
    else{long D =A*2+B-K;
         System.out.println(D);}
  }
}