#include<cstdio>
#include<deque>
#include<stack>
#include<algorithm>
#include<cstring>
#include<cctype>
using namespace std;
int T(char*s)
{
	char*p,a[2]="@",c;
	stack<char> o;
	deque<char> d;
	for(p=s;*p;++p)
	{
		a[0]=*p;
		if(isdigit(*p))
		{
			d.push_back(*p);
			while(o.size()&&o.top()=='-')o.pop(),d.push_back('-');
		}
		if(strpbrk(a,"(+*-"))o.push(*p);
		if(*p==')')
		{
			while(c=o.top(),o.pop(),c-'(')d.push_back(c);
			while(o.size()&&o.top()=='-')o.pop(),d.push_back('-');
		}
	}
	while(o.size())d.push_back(o.top()),o.pop();
	for(int i=1;i<d.size();)
	{
		if(d[i]=='-')d[i-1]=-(d[i-1]-'1')+'1',d.erase(d.begin()+i);
		else if(d[i]=='*')d[i-2]=min(d[i-2],d[i-1]),d.erase(d.begin()+i-1,d.begin()+i+1),--i;
		else if(d[i]=='+')d[i-2]=max(d[i-2],d[i-1]),d.erase(d.begin()+i-1,d.begin()+i+1),--i;
		else ++i;
	}
	return d[0]=='2';
}
int main()
{
	char s[84],t[84],*p,i[3];
	while(scanf("%s",s),s[0]-'.')
	{
		int r=0;
		for(i[0]='0';i[0]<'3';++i[0])for(i[1]='0';i[1]<'3';++i[1])for(i[2]='0';i[2]<'3';++i[2])
		{
			strcpy(t,s);
			while(p=strpbrk(t,"PQR"))*p=i[*p-'P'];
			r+=T(t);
		}
		printf("%d\n",r);
	}
	return 0;
}