#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
#define D10 fixed<<setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
const int INF = 114514810;
const int MOD = 1000000007;
const double EPS = 1e-10;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
string s;
int p, P, Q, R;

int solve()
{
	if (isdigit(s[p]))
	{
		int n = s[p] - '0';
		p++;
		return n;
	}
	else if (s[p] == '(')
	{
		p++;
		int a = solve();
		char c = s[p];
		p++;
		int b = solve();
		p++;
		if (c == '+') return max(a, b);
		else if(c=='*') return min(a, b);
		return 0;
	}
	else if (s[p] == '-')
	{
		p++;
		int t = solve();
		return 2 - t;
	}
	else if (s[p] == 'P')
	{
		p++;
		return P;
	}
	else if (s[p] == 'Q')
	{
		p++;
		return Q;
	}
	else if (s[p] == 'R')
	{
		p++;
		return R;
	}
	return 0;
}

int main()
{
	while (cin >> s&&s != ".")
	{
		int cnt = 0;
		REP(i, 3)REP(j, 3)REP(k, 3)
		{
			P = i; Q = j; R = k;
			p = 0;
			if (solve() == 2) cnt++;
		}
		cout << cnt << endl;
	}
	return 0;
}