#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for(int (i)=0;(i)<(n);++(i))

const int mul[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
const int add[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
const int neg[3] = {2, 1, 0};

int eval(string::iterator& c) {
	if(*c == '-') {
		return neg[eval(++c)];
	}
	if(*c == '(') {
		int left = eval(++c);
		c++;
		if(*c == '*') {
			int right = eval(++c);
			c++;
			return mul[left][right];
		}
		if(*c == '+') {
			int right = eval(++c);
			c++;
			return add[left][right];
		}
	}
	return *c-'0';
}

int main() {
	string S;
	while(cin >> S && S != ".") {
		int res = 0;
		rep(P,3) rep(Q,3) rep(R,3) {
			string s = S;
			int pos;
			pos = s.find("P");
			while(pos != string::npos) {
				s.replace(pos, 1, to_string(P));
				pos = s.find("P");
			}
			pos = s.find("Q");
			while(pos != string::npos) {
				s.replace(pos, 1, to_string(Q));
				pos = s.find("Q");
			}
			pos = s.find("R");
			while(pos != string::npos) {
				s.replace(pos, 1, to_string(R));
				pos = s.find("R");
			}
			string::iterator c = s.begin();
			if(eval(c) == 2) {
				res++;
			}
		}
		cout << res << endl;
	}
	return 0;
}