import java.util.*;

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 timesCounter=0;
      int maxCounter=0;
      
      for(int i=0; i<A; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      }
      
      for(int i=0; i<B; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      }
      
      for(int i=0; i<C; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      }
      
      System.out.println(maxCounter);
    }
} import java.util.*; util.* java 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 timesCounter=0;
      int maxCounter=0;
      
      for(int i=0; i<A; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      }
      
      for(int i=0; i<B; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      }
      
      for(int i=0; i<C; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      }
      
      System.out.println(maxCounter);
    }
} 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 timesCounter=0;
      int maxCounter=0;
      
      for(int i=0; i<A; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      }
      
      for(int i=0; i<B; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      }
      
      for(int i=0; i<C; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      }
      
      System.out.println(maxCounter);
    } main {
      Scanner sc = new Scanner(System.in);
      int A = sc.nextInt();
      int B = sc.nextInt();
      int C = sc.nextInt();
      int K = sc.nextInt();
      int timesCounter=0;
      int maxCounter=0;
      
      for(int i=0; i<A; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      }
      
      for(int i=0; i<B; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      }
      
      for(int i=0; i<C; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      }
      
      System.out.println(maxCounter);
    } 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 timesCounter=0; timesCounter 0 int maxCounter=0; maxCounter 0 for(int i=0; i<A; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      } int i=0; int i=0; i 0 i<A i A i++ i++ i {
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      } {
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      } if(timesCounter == K){
          break;
        } timesCounter == K timesCounter K {
          break;
        } break; timesCounter++ timesCounter maxCounter++ maxCounter for(int i=0; i<B; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      } int i=0; int i=0; i 0 i<B i B i++ i++ i {
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      } {
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      } if(timesCounter == K){
          break;
        } timesCounter == K timesCounter K {
          break;
        } break; timesCounter++ timesCounter for(int i=0; i<C; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      } int i=0; int i=0; i 0 i<C i C i++ i++ i {
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      } {
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      } if(timesCounter == K){
          break;
        } timesCounter == K timesCounter K {
          break;
        } break; timesCounter++ timesCounter maxCounter-- maxCounter System.out.println(maxCounter) System.out.println System.out System System.out maxCounter String[] args args
import java.util.*;

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 timesCounter=0;
      int maxCounter=0;
      
      for(int i=0; i<A; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter++;
      }
      
      for(int i=0; i<B; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
      }
      
      for(int i=0; i<C; i++){
        if(timesCounter == K){
          break;
        }
        timesCounter++;
        maxCounter--;
      }
      
      System.out.println(maxCounter);
    }
}