//package codeforces;

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Stack;

public class Main {
	// Integer.bitCount(i); count no of 1 bit in a given number
	static Scanner scn = new Scanner(System.in);
//	static class FastReader {
//	BufferedReader br;
//	StringTokenizer st;
//
//	public FastReader() {
//		br = new BufferedReader(new InputStreamReader(System.in));
//	}
//
//	String next() {
//		while (st == null || !st.hasMoreElements()) {
//			try {
//				st = new StringTokenizer(br.readLine());
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return st.nextToken();
//	}
//
//	int nextInt() {
//		return Integer.parseInt(next());
//	}
//
//	long nextLong() {
//		return Long.parseLong(next());
//	}
//
//	double nextDouble() {
//		return Double.parseDouble(next());
//	}
//
//	String nextLine() {
//		String str = "";
//		try {
//			str = br.readLine();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return str;
//	}
//}
//
//static FastReader s = new FastReader();
	

	
	public static void main(String[] args) {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
}
}
	class pair
	{
		int x;
		int y;
		
		pair(int a,int b)
		{
			this.x=a;
			this.y=b;
			
		}
		public int hashCode()
		 {
		  return x*31+y*31;
		 }
		 
		 public boolean equals(Object other)
		 {
		  if(this==other) return true;
		  
		  if(other instanceof pair)
		  {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  }
		  else return false;
		 }
	}
	class sorting implements Comparator<pair>
	{

		@Override
		public int compare(pair o1, pair o2) {
			// TODO Auto-generated method stub
			int a=Math.max(o1.x,o2.x),b=o1.y-o2.y;
			return o1.x-o2.x;
		}
		
	}
 import java.util.Scanner; Scanner java.util import java.io.BufferedReader; BufferedReader java.io import java.io.IOException; IOException java.io import java.io.InputStreamReader; InputStreamReader java.io import java.util.StringTokenizer; StringTokenizer java.util import java.util.HashMap; HashMap java.util import java.util.HashSet; HashSet java.util import java.util.Iterator; Iterator java.util import java.util.TreeMap; TreeMap java.util import java.util.TreeSet; TreeSet java.util import java.math.BigInteger; BigInteger java.math import java.util.ArrayList; ArrayList java.util import java.util.Arrays; Arrays java.util import java.util.Collections; Collections java.util import java.util.Comparator; Comparator java.util import java.util.Stack; Stack java.util public class Main {
	// Integer.bitCount(i); count no of 1 bit in a given number
	static Scanner scn = new Scanner(System.in);
//	static class FastReader {
//	BufferedReader br;
//	StringTokenizer st;
//
//	public FastReader() {
//		br = new BufferedReader(new InputStreamReader(System.in));
//	}
//
//	String next() {
//		while (st == null || !st.hasMoreElements()) {
//			try {
//				st = new StringTokenizer(br.readLine());
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return st.nextToken();
//	}
//
//	int nextInt() {
//		return Integer.parseInt(next());
//	}
//
//	long nextLong() {
//		return Long.parseLong(next());
//	}
//
//	double nextDouble() {
//		return Double.parseDouble(next());
//	}
//
//	String nextLine() {
//		String str = "";
//		try {
//			str = br.readLine();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return str;
//	}
//}
//
//static FastReader s = new FastReader();
	

	
	public static void main(String[] args) {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
}
} Main // Integer.bitCount(i); count no of 1 bit in a given number
	static Scanner scn = new Scanner(System.in); scn new Scanner(System.in) public static void main(String[] args) {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
} main {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
} long a=scn.nextLong() a scn.nextLong() scn.nextLong scn b=scn.nextLong() b scn.nextLong() scn.nextLong scn c=scn.nextLong() c scn.nextLong() scn.nextLong scn k=scn.nextLong() k scn.nextLong() scn.nextLong scn t=0; t 0 t=k t k k-=(a+b) k (a+b) a b if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		} k<=0 k 0 System.out.println(Math.min(a,t)) System.out.println System.out System System.out Math.min(a,t) Math.min Math a t if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		} k>0 k 0 {
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		} k-=c k c if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			} k==0 k 0 System.out.println(a-c) System.out.println System.out System System.out a-c a c if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			} k<0 k 0 {	t-=(a+b);
			
				System.out.println(a-t);
			} t-=(a+b) t (a+b) a b System.out.println(a-t) System.out.println System.out System System.out a-t a t if(k>0)
			{
				System.out.println(a-c);
			} k>0 k 0 {
				System.out.println(a-c);
			} System.out.println(a-c) System.out.println System.out System System.out a-c a c String[] args args class pair
	{
		int x;
		int y;
		
		pair(int a,int b)
		{
			this.x=a;
			this.y=b;
			
		}
		public int hashCode()
		 {
		  return x*31+y*31;
		 }
		 
		 public boolean equals(Object other)
		 {
		  if(this==other) return true;
		  
		  if(other instanceof pair)
		  {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  }
		  else return false;
		 }
	} pair int x; x int y; y pair(int a,int b)
		{
			this.x=a;
			this.y=b;
			
		} pair {
			this.x=a;
			this.y=b;
			
		} this.x=a this.x this this.x a this.y=b this.y this this.y b int a a int b b public int hashCode()
		 {
		  return x*31+y*31;
		 } hashCode {
		  return x*31+y*31;
		 } return x*31+y*31; x*31+y*31 x*31 x 31 y*31 y 31 public boolean equals(Object other)
		 {
		  if(this==other) return true;
		  
		  if(other instanceof pair)
		  {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  }
		  else return false;
		 } equals {
		  if(this==other) return true;
		  
		  if(other instanceof pair)
		  {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  }
		  else return false;
		 } if(this==other) return true; this==other this other return true; true if(other instanceof pair)
		  {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  }
		  else return false; other instanceof pair other pair {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  } pair pt = (pair)other; pt (pair)other return pt.x==this.x && pt.y==this.y; pt.x==this.x && pt.y==this.y pt.x==this.x pt.x pt pt.x this.x this this.x pt.y==this.y pt.y pt pt.y this.y this this.y return false; false Object other other class sorting implements Comparator<pair>
	{

		@Override
		public int compare(pair o1, pair o2) {
			// TODO Auto-generated method stub
			int a=Math.max(o1.x,o2.x),b=o1.y-o2.y;
			return o1.x-o2.x;
		}
		
	} sorting @Override
		public int compare(pair o1, pair o2) {
			// TODO Auto-generated method stub
			int a=Math.max(o1.x,o2.x),b=o1.y-o2.y;
			return o1.x-o2.x;
		} compare {
			// TODO Auto-generated method stub
			int a=Math.max(o1.x,o2.x),b=o1.y-o2.y;
			return o1.x-o2.x;
		} int a=Math.max(o1.x,o2.x) a Math.max(o1.x,o2.x) Math.max Math o1.x o1 o1.x o2.x o2 o2.x b=o1.y-o2.y; b o1.y-o2.y o1.y o1 o1.y o2.y o2 o2.y return o1.x-o2.x; o1.x-o2.x o1.x o1 o1.x o2.x o2 o2.x pair o1 o1 pair o2 o2 Comparator public class Main {
	// Integer.bitCount(i); count no of 1 bit in a given number
	static Scanner scn = new Scanner(System.in);
//	static class FastReader {
//	BufferedReader br;
//	StringTokenizer st;
//
//	public FastReader() {
//		br = new BufferedReader(new InputStreamReader(System.in));
//	}
//
//	String next() {
//		while (st == null || !st.hasMoreElements()) {
//			try {
//				st = new StringTokenizer(br.readLine());
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return st.nextToken();
//	}
//
//	int nextInt() {
//		return Integer.parseInt(next());
//	}
//
//	long nextLong() {
//		return Long.parseLong(next());
//	}
//
//	double nextDouble() {
//		return Double.parseDouble(next());
//	}
//
//	String nextLine() {
//		String str = "";
//		try {
//			str = br.readLine();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return str;
//	}
//}
//
//static FastReader s = new FastReader();
	

	
	public static void main(String[] args) {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
}
} public class Main {
	// Integer.bitCount(i); count no of 1 bit in a given number
	static Scanner scn = new Scanner(System.in);
//	static class FastReader {
//	BufferedReader br;
//	StringTokenizer st;
//
//	public FastReader() {
//		br = new BufferedReader(new InputStreamReader(System.in));
//	}
//
//	String next() {
//		while (st == null || !st.hasMoreElements()) {
//			try {
//				st = new StringTokenizer(br.readLine());
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return st.nextToken();
//	}
//
//	int nextInt() {
//		return Integer.parseInt(next());
//	}
//
//	long nextLong() {
//		return Long.parseLong(next());
//	}
//
//	double nextDouble() {
//		return Double.parseDouble(next());
//	}
//
//	String nextLine() {
//		String str = "";
//		try {
//			str = br.readLine();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return str;
//	}
//}
//
//static FastReader s = new FastReader();
	

	
	public static void main(String[] args) {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
}
} Main
//package codeforces;

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.TreeSet;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Stack;

public class Main {
	// Integer.bitCount(i); count no of 1 bit in a given number
	static Scanner scn = new Scanner(System.in);
//	static class FastReader {
//	BufferedReader br;
//	StringTokenizer st;
//
//	public FastReader() {
//		br = new BufferedReader(new InputStreamReader(System.in));
//	}
//
//	String next() {
//		while (st == null || !st.hasMoreElements()) {
//			try {
//				st = new StringTokenizer(br.readLine());
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
//		}
//		return st.nextToken();
//	}
//
//	int nextInt() {
//		return Integer.parseInt(next());
//	}
//
//	long nextLong() {
//		return Long.parseLong(next());
//	}
//
//	double nextDouble() {
//		return Double.parseDouble(next());
//	}
//
//	String nextLine() {
//		String str = "";
//		try {
//			str = br.readLine();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		return str;
//	}
//}
//
//static FastReader s = new FastReader();
	

	
	public static void main(String[] args) {
		/* TODO Auto-generated method stub */
		// System.out.format("%.10f", ans);char c = sc.next().charAt(0);
		
		
		long a=scn.nextLong(),b=scn.nextLong(),c=scn.nextLong(),k=scn.nextLong(),t=0;
		t=k;
		k-=(a+b);
		if(k<=0)
			System.out.println(Math.min(a,t));
		else if(k>0)
		{
			k-=c;
			if(k==0)
				System.out.println(a-c);
			else if(k<0)
			{	t-=(a+b);
			
				System.out.println(a-t);
			}
			else if(k>0)
			{
				System.out.println(a-c);
			}
		}
		
}
}
	class pair
	{
		int x;
		int y;
		
		pair(int a,int b)
		{
			this.x=a;
			this.y=b;
			
		}
		public int hashCode()
		 {
		  return x*31+y*31;
		 }
		 
		 public boolean equals(Object other)
		 {
		  if(this==other) return true;
		  
		  if(other instanceof pair)
		  {
		   pair pt = (pair)other;
		   
		   return pt.x==this.x && pt.y==this.y;
		  }
		  else return false;
		 }
	}
	class sorting implements Comparator<pair>
	{

		@Override
		public int compare(pair o1, pair o2) {
			// TODO Auto-generated method stub
			int a=Math.max(o1.x,o2.x),b=o1.y-o2.y;
			return o1.x-o2.x;
		}
		
	}

