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 sum = Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b));
  System.out.println(sum);
  
  sc.close();
}//void
}//class 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 sum = Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b));
  System.out.println(sum);
  
  sc.close();
}//void
}//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 = Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b));
  System.out.println(sum);
  
  sc.close();
}//void 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 = Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b));
  System.out.println(sum);
  
  sc.close();
} 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 = Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b)); sum Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b)) Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b)) Math.min(a,k) Math.min Math a k 0*(Math.min(k-Math.min(a,k),b)) 0 (Math.min(k-Math.min(a,k),b)) Math.min Math k-Math.min(a,k) k Math.min(a,k) Math.min Math a k b (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b)) (-1) 1 (k-Math.min(a,k)-Math.min(k-Math.min(a,k),b)) k-Math.min(a,k)-Math.min(k-Math.min(a,k),b) k Math.min(a,k) Math.min Math a k Math.min(k-Math.min(a,k),b) Math.min Math k-Math.min(a,k) k Math.min(a,k) Math.min Math a k b System.out.println(sum) System.out.println System.out System System.out sum sc.close() sc.close sc 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 sum = Math.min(a,k) + 0*(Math.min(k-Math.min(a,k),b)) + (-1)*(k-Math.min(a,k)-Math.min(k-Math.min(a,k),b));
  System.out.println(sum);
  
  sc.close();
}//void
}//class