import java.util.*;
class Main{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
  int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),k=sc.nextInt();
  if(a>k){
    System.out.println(k);
  }else{
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  }
}
} import java.util.*; util.* java class Main{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
  int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),k=sc.nextInt();
  if(a>k){
    System.out.println(k);
  }else{
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  }
}
} Main public static void main(String[]args){
Scanner sc=new Scanner(System.in);
  int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),k=sc.nextInt();
  if(a>k){
    System.out.println(k);
  }else{
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  }
} main {
Scanner sc=new Scanner(System.in);
  int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),k=sc.nextInt();
  if(a>k){
    System.out.println(k);
  }else{
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  }
} Scanner sc=new Scanner(System.in); sc new Scanner(System.in) int a=sc.nextInt() a sc.nextInt() sc.nextInt sc b=sc.nextInt() b sc.nextInt() sc.nextInt sc c=sc.nextInt() c sc.nextInt() sc.nextInt sc k=sc.nextInt(); k sc.nextInt() sc.nextInt sc if(a>k){
    System.out.println(k);
  }else{
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  } a>k a k {
    System.out.println(k);
  } System.out.println(k) System.out.println System.out System System.out k {
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  } k-=a k a if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      } b>k b k {
          System.out.println(a);
      } System.out.println(a) System.out.println System.out System System.out a {
          k-=b;
          System.out.println(a-k);
      } k-=b k b System.out.println(a-k) System.out.println System.out System System.out a-k a k String[]args args
import java.util.*;
class Main{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
  int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt(),k=sc.nextInt();
  if(a>k){
    System.out.println(k);
  }else{
      k-=a;
      if(b>k){
          System.out.println(a);
      }else{
          k-=b;
          System.out.println(a-k);
      }
  }
}
}