import java.io.*;
import java.util.*;
class Main {
  public static int result = 0;
  public static int a = 0;
  public static int b = 0;
  public static int c = 0;
  public static int k = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt(); //1のカード枚数
        b = sc.nextInt(); //0のカード枚数
        c = sc.nextInt(); //-1のカード枚数
        k = sc.nextInt(); //引くカードの枚数
        k = calc(a,k, 1);
        k = calc(b,k, 0);
        k = calc(c,k, -1);
        System.out.println(result);
    }
    public static int calc(int num, int k , int card){
      if(k==0) return k;
      if(num >= k){
        result += k*card;
        k=0;
      }else {
        result += num*card;
        k-=num;
      }
      return k;
    }
} import java.io.*; io.* java import java.util.*; util.* java class Main {
  public static int result = 0;
  public static int a = 0;
  public static int b = 0;
  public static int c = 0;
  public static int k = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt(); //1のカード枚数
        b = sc.nextInt(); //0のカード枚数
        c = sc.nextInt(); //-1のカード枚数
        k = sc.nextInt(); //引くカードの枚数
        k = calc(a,k, 1);
        k = calc(b,k, 0);
        k = calc(c,k, -1);
        System.out.println(result);
    }
    public static int calc(int num, int k , int card){
      if(k==0) return k;
      if(num >= k){
        result += k*card;
        k=0;
      }else {
        result += num*card;
        k-=num;
      }
      return k;
    }
} Main public static int result = 0; result 0 public static int a = 0; a 0 public static int b = 0; b 0 public static int c = 0; c 0 public static int k = 0; k 0 public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt(); //1のカード枚数
        b = sc.nextInt(); //0のカード枚数
        c = sc.nextInt(); //-1のカード枚数
        k = sc.nextInt(); //引くカードの枚数
        k = calc(a,k, 1);
        k = calc(b,k, 0);
        k = calc(c,k, -1);
        System.out.println(result);
    } main {
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt(); //1のカード枚数
        b = sc.nextInt(); //0のカード枚数
        c = sc.nextInt(); //-1のカード枚数
        k = sc.nextInt(); //引くカードの枚数
        k = calc(a,k, 1);
        k = calc(b,k, 0);
        k = calc(c,k, -1);
        System.out.println(result);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) a = sc.nextInt() a sc.nextInt() sc.nextInt sc b = sc.nextInt() b sc.nextInt() sc.nextInt sc c = sc.nextInt() c sc.nextInt() sc.nextInt sc k = sc.nextInt() k sc.nextInt() sc.nextInt sc k = calc(a,k, 1) k calc(a,k, 1) calc a k 1 k = calc(b,k, 0) k calc(b,k, 0) calc b k 0 k = calc(c,k, -1) k calc(c,k, -1) calc c k -1 1 System.out.println(result) System.out.println System.out System System.out result String[] args args public static int calc(int num, int k , int card){
      if(k==0) return k;
      if(num >= k){
        result += k*card;
        k=0;
      }else {
        result += num*card;
        k-=num;
      }
      return k;
    } calc {
      if(k==0) return k;
      if(num >= k){
        result += k*card;
        k=0;
      }else {
        result += num*card;
        k-=num;
      }
      return k;
    } if(k==0) return k; k==0 k 0 return k; k if(num >= k){
        result += k*card;
        k=0;
      }else {
        result += num*card;
        k-=num;
      } num >= k num k {
        result += k*card;
        k=0;
      } result += k*card result k*card k card k=0 k 0 {
        result += num*card;
        k-=num;
      } result += num*card result num*card num card k-=num k num return k; k int num num int k k int card card
import java.io.*;
import java.util.*;
class Main {
  public static int result = 0;
  public static int a = 0;
  public static int b = 0;
  public static int c = 0;
  public static int k = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        a = sc.nextInt(); //1のカード枚数
        b = sc.nextInt(); //0のカード枚数
        c = sc.nextInt(); //-1のカード枚数
        k = sc.nextInt(); //引くカードの枚数
        k = calc(a,k, 1);
        k = calc(b,k, 0);
        k = calc(c,k, -1);
        System.out.println(result);
    }
    public static int calc(int num, int k , int card){
      if(k==0) return k;
      if(num >= k){
        result += k*card;
        k=0;
      }else {
        result += num*card;
        k-=num;
      }
      return k;
    }
}
