#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char *argv[] )
{
    int a, b, c, k;

    scanf( "%d %d %d %d", &a, &b, &c, &k );

    if( a >= k ) {
        printf( "%d\n", k );
        exit( 0 );
    }
    if( a+b >= k ) {
        printf( "%d\n", a );
        exit( 0 );
    }

    k -= a+b;
    printf( "%d\n", a-k );
}
