import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  }
} import java.util.Scanner; Scanner java.util public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  }
} Main public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  } main {
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int a = scan.nextInt(); a scan.nextInt() scan.nextInt scan int b = scan.nextInt(); b scan.nextInt() scan.nextInt scan int c = scan.nextInt(); c scan.nextInt() scan.nextInt scan int maxIndex = scan.nextInt(); maxIndex scan.nextInt() scan.nextInt scan int index = 0; index 0 int sum = 0; sum 0 for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    } int i = 0; int i = 0; i 0 i < a && index < maxIndex i < a i a index < maxIndex index maxIndex i++,index++ i++ i index++ index {
      sum += 1;
    } {
      sum += 1;
    } sum += 1 sum 1 for(int j = 0;j < b && index < maxIndex;j++,index++){
    } int j = 0; int j = 0; j 0 j < b && index < maxIndex j < b j b index < maxIndex index maxIndex j++,index++ j++ j index++ index {
    } {
    } for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    } int k = 0; int k = 0; k 0 k < c && index < maxIndex k < c k c index < maxIndex index maxIndex k++,index++ k++ k index++ index {
        sum -= 1;
    } {
        sum -= 1;
    } sum -= 1 sum 1 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 scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  }
} public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  }
} Main
import java.util.Scanner;

public class Main{
  public static void main(String[] args){
    Scanner scan = new Scanner(System.in);
    int a = scan.nextInt();
    int b = scan.nextInt();
    int c = scan.nextInt();
    int maxIndex = scan.nextInt();
    
    int index = 0;
    int sum = 0;
    for(int i = 0;i < a && index < maxIndex;i++,index++){
      sum += 1;
    }
    for(int j = 0;j < b && index < maxIndex;j++,index++){
    }
    for(int k = 0;k < c && index < maxIndex;k++,index++){
        sum -= 1;
    }
    System.out.println(sum);
  }
}