/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    }
} import java.util.*; util.* java import java.lang.*; lang.* java import java.io.*; io.* java public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    }
} Main public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    } main {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    } Scanner s = new Scanner(System.in); s new Scanner(System.in) int a = s.nextInt(); a s.nextInt() s.nextInt s int b = s.nextInt(); b s.nextInt() s.nextInt s int c = s.nextInt(); c s.nextInt() s.nextInt s int k = s.nextInt(); k s.nextInt() s.nextInt s int x = a+b; x a+b a b int y = x+c; y x+c x c if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k); k<a k a System.out.println(k) System.out.println System.out System System.out k if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k); k < x k x System.out.println(a) System.out.println System.out System System.out a if(k<=y)
            System.out.println(a+x-k); k<=y k y System.out.println(a+x-k) System.out.println System.out System System.out a+x-k a+x a x k String[] args args public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    }
} public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    }
} Main
/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
        Scanner s = new Scanner(System.in);
        int a = s.nextInt();
        int b = s.nextInt();
        int c = s.nextInt();
        int k = s.nextInt();
        int x = a+b;
        int y = x+c;
        if(k<a)
            System.out.println(k);
        else if(k < x)
            System.out.println(a);
        else if(k<=y)
            System.out.println(a+x-k);
    }
}
