import java.util.Scanner;
import java.util.ArrayList;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    }
  
  public static void maxCal(int A , int B, int C, int K){   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  }
} import java.util.Scanner; Scanner java.util import java.util.ArrayList; ArrayList java.util public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    }
  
  public static void maxCal(int A , int B, int C, int K){   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  }
} Main public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    } main {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    } Scanner scan = new Scanner(System.in); scan new Scanner(System.in) int i = 0; i 0 int a a b b c c k; k String an = scan.nextLine(); an scan.nextLine() scan.nextLine scan String atai[] = an.split(" ",0); atai an.split(" ",0) an.split an " " 0 int num[] = new int[4]; num new int[4] 4 while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      } i < 4 i 4 {
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      } num[i] = Integer.parseInt(atai[i]) num[i] num i Integer.parseInt(atai[i]) Integer.parseInt Integer atai[i] atai i i++ i a = num[0] a num[0] num 0 b = num[1] b num[1] num 1 c = num[2] c num[2] num 2 k = num[3] k num[3] num 3 maxCal(a,b,c,k) maxCal a b c k scan.close() scan.close scan String[] args args public static void maxCal(int A , int B, int C, int K){   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  } maxCal {   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  } int S S T; T if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    } A >= K A K {
       System.out.println(K);
      return;
    } System.out.println(K) System.out.println System.out System System.out K return; if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    } (A+B) >= K (A+B) A B K {
       System.out.println(A);
      return;
    } System.out.println(A) System.out.println System.out System System.out A return; {
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    } S = K-(A+B) S K-(A+B) K (A+B) A B T = A - S T A - S A S System.out.println(T) System.out.println System.out System System.out T return; int A A int B B int C C int K K public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    }
  
  public static void maxCal(int A , int B, int C, int K){   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  }
} public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    }
  
  public static void maxCal(int A , int B, int C, int K){   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  }
} Main
import java.util.Scanner;
import java.util.ArrayList;
 
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
      int i = 0;
      int a,b,c,k;
      String an = scan.nextLine();
      String atai[] = an.split(" ",0);
      int num[] = new int[4];
      while(i < 4){
     //   System.out.println(atai[i]);
        num[i] = Integer.parseInt(atai[i]);
        i++;
      }
      a = num[0];
      b = num[1];
      c = num[2];
      k = num[3];
        maxCal(a,b,c,k);
        scan.close();
    }
  
  public static void maxCal(int A , int B, int C, int K){   
    int S,T;
    if(A >= K){
       System.out.println(K);
      return;
    }else if((A+B) >= K){
       System.out.println(A);
      return;
    }else{
       S = K-(A+B);
       T = A - S;
       System.out.println(T);
      return;
    }
  }
}