var
x , sum : integer;
begin
read(x);
while( x <> 0) do begin
if( x mod 3 = 0) and (x mod 10 = 9) then
sum := sum + x;
end;
write(sum);
end.
var
x , sum : integer;
begin
read(x);
while( x <> 0) do begin
if( x mod 3 = 0) and (x mod 10 = 9) then
sum := sum + x;
read(x);
end;
write(sum);
end.