#include <iostream>
using namespace std;
int main()
{
int a, S=0;
cin >> a;
while (a!=0)
if (a % 3 == 0 && a % 10 == 9) S=S+a;
}
cout<<S;
return 0;
#include <iostream>
using namespace std;
int main()
{
int a, S=0;
cin >> a;
while (a!=0)
{
if (a % 3 == 0 && a % 10 == 9) S=S+a;
cin >> a;
}
cout<<S;
return 0;
}