#include <stdio.h>

int main( void )
{
  int A, B, C, K;
  
  scanf( "%d %d %d %d", &A, &B, &C, &K );
  
  if ( K <= A ) {
     printf ( K );
  } 
  if ( K > A, K <= A + B ) {
     printf ( A );
  }
  if ( K > A + B, K <= A + B + C ) {
     printf ( A - (K - A - B ) );
  }  
}  