import java.util.Scanner;
public class Main{
    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    }
} import java.util.Scanner; Scanner java.util public class Main{
    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    }
} Main public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    } main {
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    } Scanner input=new Scanner(System.in); input new Scanner(System.in) int a=input.nextInt(); a input.nextInt() input.nextInt input int b=input.nextInt(); b input.nextInt() input.nextInt input int c=input.nextInt(); c input.nextInt() input.nextInt input int k=input.nextInt(); k input.nextInt() input.nextInt input int result=0; result 0 if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    } k<=a k a {
    result+=k;
    System.out.print(result);
    } result+=k result k System.out.print(result) System.out.print System.out System System.out result if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    } k<=a+b k a+b a b {
    System.out.print(a);
    } System.out.print(a) System.out.print System.out System System.out a {
    result=2*a+b-k;
    System.out.print(result);
    } result=2*a+b-k result 2*a+b-k 2*a+b 2*a 2 a b k System.out.print(result) System.out.print System.out System System.out result String[] args args public class Main{
    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    }
} public class Main{
    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    }
} Main
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int a=input.nextInt();
    int b=input.nextInt();
    int c=input.nextInt();
    int k=input.nextInt();
    int result=0;
    if(k<=a){
    result+=k;
    System.out.print(result);
    }
    else if(k<=a+b){
    System.out.print(a);
    }
    else {
    result=2*a+b-k;
    System.out.print(result);
    }
    }
}