get_string(String) :-
  current_input(Input),
  read_string(Input, ' \n', '', _, String).
 
get_number(Number) :-
  get_string(String),
  number_string(Number, String).

comp([],[_]).
comp([H1|S],[H1|T]) :- comp(S,T).
    
main :-
  get_number(A),
  get_number(B),
  get_number(C),
  get_number(K),
  (A >= K -> writeln(K);(D is A + B, D >= K)->writeln(A);Ans is A - (K - A - B), writeln(Ans)).