import java.util.Scanner;

public class Main{

	public static void main(String[] args){

		Scanner scan = new Scanner(System.in);
		int a = scan.nextInt();
		int b = scan.nextInt();
		int c = scan.nextInt();
		int k = scan.nextInt();

		// System.out.println(a);
		// System.out.println(b);
		// System.out.println(c);

		// int one[]=new int[a];
		// int zero[]=new int[b];
		// int minus[]=new int[c];
		int anser = 0;

		int k1 = 0;
		int k2 = 0;
		//int k3 = 0;

		if(k>a || k==a){
		for(int i=0; i<a; i++){
			anser++;
		}
		k1 = k-a;
	}else if(a>k){
		for(int i=0; i<k; i++){
			anser++;
		}
		k1 = 0;
	}

		if(k1>b || k1==b){
		for(int i=0; i<b; i++){
			anser = anser + 0;
		}
		k2 = k1-b;
	}
		for(int i=0; i<k2; i++){
			anser =anser -1;
	}
		System.out.println(anser);
	}
}
/tmp/hf-datasets-cache/medium/datasets/42704881117311-config-parquet-and-info-qiankunmu-Project_CodeNet-896c9d36/hub/datasets--qiankunmu--Project_CodeNet_Python800_and_Java250/snapshots/c899fde44a77ba1cc14973ed3de064e4ff3aadc7/Project_CodeNet_Java250.tar.gz