import java.util.*;
public class Main {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    int sum;

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

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

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

	System.out.println(sum);
  }
} Main public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    int sum;

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

	System.out.println(sum);
  } main {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    int sum;

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

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

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

	System.out.println(sum);
  }
} public class Main {
  public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int c = sc.nextInt();
    int k = sc.nextInt();
    int sum;

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

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

    if (a<=k) {
      if (b<=k-a) {
        c = k-a-b;
        sum = a-c;
      } else {
        sum = a;
      }
    } else {
      sum = k;
    }

	System.out.println(sum);
  }
}
