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

int term(string&, int&);
int dfs(string&s, int&i) {
	int ret = term(s, i);
	while (s[i] == '+' || s[i] == '*') {
		if (s[i] == '+') {
			i++; int b = dfs(s, i);
			ret = max(ret, b);
		}
		else {
			i++; int b = dfs(s, i);
			ret = min(ret, b);
		}
	}
	return ret;
}
int term(string&s, int&i) {
	if (isdigit(s[i]))return s[i++] - '0';
	if (s[i] == '-') {
		i++; int a = term(s, i);
		if (a == 0)return 2;
		if (a == 2)return 0;
		return 1;
	}
	if (s[i] == '(') {
		i++; int a = dfs(s, i); i++;
		return a;
	}
}
int main() {
	string s;
	while (cin >> s, s != ".") {
		int cnt = 0;
		rep(i, 3)rep(j, 3)rep(k, 3) {
			string t = s;
			for (char&c : t) {
				if (c == 'P')c = '0' + i;
				if (c == 'Q')c = '0' + j;
				if (c == 'R')c = '0' + k;
			}
			int I = 0;
			if (dfs(t, I) == 2)cnt++;
		}
		cout << cnt << endl;
	}
}