#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)

int parse(string x, int &i)
{
	switch (x[i])
	{
	case '0': i++; return 0;
	case '1': i++; return 1;
	case '2': i++; return 2;
	case '-': i++; return (2 - parse(x, i));
	}

	i++;
	int l = parse(x, i);
	int flag = -1;
	if (x[i] == '*')
		flag = 0;
	else
		flag = 1;
	i++;
	int r = parse(x, i);
	i++;

	if (flag == 0)
	{
		if (l == 2 && r == 2) return 2;
		if (l == 0 || r == 0) return 0;
		return 1;
	}

	if (l == 0 && r == 0) return 0;
	if (l == 2 || r == 2) return 2;
	return 1;
}

string replace(string src, string from, string to)
{
	string::size_type Pos(src.find(from));
	while (Pos != string::npos)
	{
		src.replace(Pos, from.length(), to);
		Pos = src.find(from, Pos + to.length());
	}
	return src;
}

int main()
{
	while (1)
	{
		string s; cin >> s;
		if (s[0] == '.') return 0;

		int ans = 0;
		rep(p, 0, 3) rep(q, 0, 3) rep(r, 0, 3)
		{
			string ss = s;
			ss = replace(ss, "P", to_string(p));
			ss = replace(ss, "Q", to_string(q));
			ss = replace(ss, "R", to_string(r));

			int i = 0;
			if (parse(ss, i) == 2) ans++;
		}
		cout << ans << endl;
	}
}