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

fizbuz(N) :- 0 is N mod 3,!,fail.
fizbuz(N) :- 0 is N mod 5,!,fail.
fizbuz(_).

solve(N,N,N) :- fizbuz(N),!.
solve(N,N,0) :- !.
solve(I,N,Sum) :- fizbuz(I),!,Next is I + 1,solve(Next,N,M),Sum is M + I.
solve(I,N,Sum) :- Next is I + 1,solve(Next,N,Sum).

main :-
  get_number(N),
  solve(1,N,Ans),
  writeln(Ans).