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

     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 total = 0;
        if(a<=k){
         total += a;
      }else{
         total += k;
      }
      k = k-a-b;
      if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      }
      System.out.println(total);
	}
} 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 total = 0;
        if(a<=k){
         total += a;
      }else{
         total += k;
      }
      k = k-a-b;
      if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      }
      System.out.println(total);
	} main {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		int c = sc.nextInt();
		int k = sc.nextInt();
      int total = 0;
        if(a<=k){
         total += a;
      }else{
         total += k;
      }
      k = k-a-b;
      if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      }
      System.out.println(total);
	} 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 total = 0; total 0 if(a<=k){
         total += a;
      }else{
         total += k;
      } a<=k a k {
         total += a;
      } total += a total a {
         total += k;
      } total += k total k k = k-a-b k k-a-b k-a-b k a b if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      } c<=k c k {
         total -=c;
      } total -=c total c {
         if(k>0){
            total -= k;
         }else{
         }
      } if(k>0){
            total -= k;
         }else{
         } k>0 k 0 {
            total -= k;
         } total -= k total k {
         } System.out.println(total) System.out.println System.out System System.out total 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 total = 0;
        if(a<=k){
         total += a;
      }else{
         total += k;
      }
      k = k-a-b;
      if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      }
      System.out.println(total);
	}
} 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 total = 0;
        if(a<=k){
         total += a;
      }else{
         total += k;
      }
      k = k-a-b;
      if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      }
      System.out.println(total);
	}
} 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 total = 0;
        if(a<=k){
         total += a;
      }else{
         total += k;
      }
      k = k-a-b;
      if(c<=k){
         total -=c;
      }else{
         if(k>0){
            total -= k;
         }else{
         }
      }
      System.out.println(total);
	}
}

    