#include<iostream>
#include<string>
#include<cctype>
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long Int;
typedef string::const_iterator Itr;

int P, Q, R;

int expression(Itr &itr);
int term(Itr &itr);
int factor(Itr &itr);
int letter(Itr &itr);

int not(int n)
{
	if (n == 0)return 2;
	else if (n == 1)return 1;
	else return 0;
}

int star(int x, int y)
{
	if (x == 0 || y == 0)return 0;
	else if (x == 1 || y == 1)return 1;
	else return 2;
}

int _plus(int x, int y)
{
	if (x == 0 && y == 0)return 0;
	else if (x == 2 || y == 2)return 2;
	else return 1;
}

int expression(Itr &itr) 
{
	int ret = term(itr);

	while (true)
	{
		if (*itr == '*')
		{
			itr++;
			ret = star(ret, term(itr));

		}
		else if (*itr == '+')
		{
			itr++;
			ret = _plus(ret, term(itr));
		}
		else
		{
			break;
		}
	}
	return ret;
}

int term(Itr &itr)
{
	int ret;

	if (*itr == '-')
	{
		itr++;
		ret = not(term(itr));
	}
	else
	{
		ret = factor(itr);
	}

	return ret;
}

int factor(Itr &itr)
{
	int ret;

	if (*itr == '(')
	{
		itr++;
		ret = expression(itr);
		itr++;
	}
	else
	{
		ret = letter(itr);
	}

	return ret;
}

int letter(Itr &itr)
{
	int ret = 0;

	if (*itr == 'P')
	{
		ret = P;
	}
	else if (*itr == 'Q')
	{
		ret = Q;
	}
	else if (*itr == 'R')
	{
		ret = R;
	}
	else if(isdigit(*itr))
	{
		ret = *itr - '0';
	}
	itr++;
	return ret;
}

int main()
{
	while (true)
	{
		string str;
		getline(cin, str);
		if (str[0] == '.')break;
		str.append("_");
		int count = 0;
		REP(i, 3)REP(j, 3)REP(k, 3)
		{
			P = i;
			Q = j;
			R = k;
			Itr begin = str.begin();
			int ans = expression(begin);
			if (ans == 2)
			{
				count++;
			}
		}
		cout << count << endl;
	}

	return 0;
}
