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();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  }
} 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();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  }
} 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();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  } main {
    Scanner sc = new Scanner(System.in);

    long a = sc.nextLong();
    long b = sc.nextLong();
    long c = sc.nextLong();
    long k = sc.nextLong();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  } 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 long ans = 0; ans 0 if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    } k < a k a {
      ans = k;
    } ans = k ans k if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    } k <= a + b k a + b a b {
      ans = a;
    } ans = a ans a {
      ans = 2 * a + b - k;
    } ans = 2 * a + b - k ans 2 * a + b - k 2 * a + b 2 * a 2 a b k System.out.println(ans) System.out.println System.out System System.out ans 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();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  }
} 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();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  }
} 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();
    long ans = 0;

    if(k < a){
      ans = k;
    } else if(k <= a + b){
      ans = a;
    } else {
      ans = 2 * a + b - k;
    }

    System.out.println(ans);
  }
}
