import java.util.*;

class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int A = sc.nextInt();  //  1
      int B = sc.nextInt();  //  0
      int C = sc.nextInt();  // -1
      int K = sc.nextInt();  // 選ぶ枚数
      int max = -100;
      
      if ( A >= K){
      	System.out.println(K);
      }
      else{
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      }
    }
} import java.util.*; util.* java class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int A = sc.nextInt();  //  1
      int B = sc.nextInt();  //  0
      int C = sc.nextInt();  // -1
      int K = sc.nextInt();  // 選ぶ枚数
      int max = -100;
      
      if ( A >= K){
      	System.out.println(K);
      }
      else{
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      }
    }
} Main public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int A = sc.nextInt();  //  1
      int B = sc.nextInt();  //  0
      int C = sc.nextInt();  // -1
      int K = sc.nextInt();  // 選ぶ枚数
      int max = -100;
      
      if ( A >= K){
      	System.out.println(K);
      }
      else{
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      }
    } main {
    	Scanner sc = new Scanner(System.in);
      int A = sc.nextInt();  //  1
      int B = sc.nextInt();  //  0
      int C = sc.nextInt();  // -1
      int K = sc.nextInt();  // 選ぶ枚数
      int max = -100;
      
      if ( A >= K){
      	System.out.println(K);
      }
      else{
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      }
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) int A = sc.nextInt(); A sc.nextInt() sc.nextInt sc int B = sc.nextInt(); B sc.nextInt() sc.nextInt sc int C = sc.nextInt(); C sc.nextInt() sc.nextInt sc int K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int max = -100; max -100 100 if ( A >= K){
      	System.out.println(K);
      }
      else{
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      } A >= K A K {
      	System.out.println(K);
      } System.out.println(K) System.out.println System.out System System.out K {
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      } int L = K - A; L K - A K A if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	} B >= L B L {
        	System.out.println(A);
      	} System.out.println(A) System.out.println System.out System System.out A {
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	} System.out.println(1*A + 0*B+ (L-B)*(-1) ) System.out.println System.out System System.out 1*A + 0*B+ (L-B)*(-1) 1*A + 0*B+ (L-B)*(-1) 1*A 1 A 0*B 0 B (L-B)*(-1) (L-B) L B (-1) 1 String[] args args
import java.util.*;

class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int A = sc.nextInt();  //  1
      int B = sc.nextInt();  //  0
      int C = sc.nextInt();  // -1
      int K = sc.nextInt();  // 選ぶ枚数
      int max = -100;
      
      if ( A >= K){
      	System.out.println(K);
      }
      else{
        int L = K - A;
     	 if ( B >= L){
        	System.out.println(A);
      	}
      	else
      	{
      		System.out.println(1*A + 0*B+ (L-B)*(-1) );
      	}
      }
    }
}