/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    }
} import java.util.*; util.* java public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    }
} Main public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    } main {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) long a = sc.nextLong(); a sc.nextLong() sc.nextLong sc long b = sc.nextLong(); b sc.nextLong() sc.nextLong sc long c = sc.nextLong(); c sc.nextLong() sc.nextLong sc long k = sc.nextLong(); k sc.nextLong() sc.nextLong sc long sum = 0; sum 0 if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		} a >= k a k {
		    sum = sum + 1 * k;
		} sum = sum + 1 * k sum sum + 1 * k sum 1 * k 1 k {
		    k = k - a;
		    sum = sum + 1 * a;
		} k = k - a k k - a k a sum = sum + 1 * a sum sum + 1 * a sum 1 * a 1 a k = k - b k k - b k b if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		} k >= 1 k 1 {
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		} if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    } c >= k c k {
		        sum = sum + (-1) * k;
		    } sum = sum + (-1) * k sum sum + (-1) * k sum (-1) * k (-1) 1 k {
		        k = k - c;
		        sum = sum + (-1) * c;
		    } k = k - c k k - c k c sum = sum + (-1) * c sum sum + (-1) * c sum (-1) * c (-1) 1 c 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 sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    }
} public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    }
} Main
/******************************************************************************

                            Online Java Compiler.
                Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.

*******************************************************************************/
import java.util.*;
public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		long c = sc.nextLong();
		long k = sc.nextLong();
		long sum = 0;
		if(a >= k)
		{
		    sum = sum + 1 * k;
		}
		else
		{
		    k = k - a;
		    sum = sum + 1 * a;
		}
		k = k - b;
		if(k >= 1)
		{
		    if(c >= k)
		    {
		        sum = sum + (-1) * k;
		    }
		    else
		    {
		        k = k - c;
		        sum = sum + (-1) * c;
		    }
		}
		System.out.println(sum);
    }
}