import java.util.Scanner;

class Main{
  public static void main(String[] args){
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    int b=scan.nextInt();
    int c=scan.nextInt();
    int num=scan.nextInt();
    int ans=0;
    if(num<=a){
      ans=num;
    }else{
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    }
    System.out.println(ans);
  }
} import java.util.Scanner; Scanner java.util class Main{
  public static void main(String[] args){
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    int b=scan.nextInt();
    int c=scan.nextInt();
    int num=scan.nextInt();
    int ans=0;
    if(num<=a){
      ans=num;
    }else{
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    }
    System.out.println(ans);
  }
} Main public static void main(String[] args){
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    int b=scan.nextInt();
    int c=scan.nextInt();
    int num=scan.nextInt();
    int ans=0;
    if(num<=a){
      ans=num;
    }else{
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    }
    System.out.println(ans);
  } main {
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    int b=scan.nextInt();
    int c=scan.nextInt();
    int num=scan.nextInt();
    int ans=0;
    if(num<=a){
      ans=num;
    }else{
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    }
    System.out.println(ans);
  } Scanner scan=new Scanner(System.in); scan new Scanner(System.in) int a=scan.nextInt(); a scan.nextInt() scan.nextInt scan int b=scan.nextInt(); b scan.nextInt() scan.nextInt scan int c=scan.nextInt(); c scan.nextInt() scan.nextInt scan int num=scan.nextInt(); num scan.nextInt() scan.nextInt scan int ans=0; ans 0 if(num<=a){
      ans=num;
    }else{
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    } num<=a num a {
      ans=num;
    } ans=num ans num {
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    } if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      } num<=a+b num a+b a b {
        ans=num-b;
      } ans=num-b ans num-b num b {
        ans=a-(num-(a+b));
      } ans=a-(num-(a+b)) ans a-(num-(a+b)) a (num-(a+b)) num (a+b) a b System.out.println(ans) System.out.println System.out System System.out ans String[] args args
import java.util.Scanner;

class Main{
  public static void main(String[] args){
    Scanner scan=new Scanner(System.in);
    int a=scan.nextInt();
    int b=scan.nextInt();
    int c=scan.nextInt();
    int num=scan.nextInt();
    int ans=0;
    if(num<=a){
      ans=num;
    }else{
      if(num<=a+b){
        ans=num-b;
      }else{
        ans=a-(num-(a+b));
      }
    }
    System.out.println(ans);
  }
}