#include <bits/stdc++.h>

using namespace std;

int id, PQR[3];
string s;

int NOT(int a) { return 2 - a; }
int AND(int a, int b) { return min(a, b); }
int OR(int a, int b) { return max(a, b); }

int formula() {
	if (isdigit(s[id]))	return s[id++] - '0';
	if (isalpha(s[id])) return PQR[s[id++] - 'P'];
	if (s[id] == '-') {
		id++;
		return NOT(formula());
	}
	if (s[id] == '(') {
		id++;
		int res, tmp = formula();
		if (s[id++] == '*') res = AND(tmp, formula());
		else res = OR(tmp, formula());
		id++;
		return res;
	}
}

int main() {
	while (cin >> s, s != ".") {
		int ans = 0;
		for (PQR[0] = 0; PQR[0] < 3; ++PQR[0])
			for (PQR[1] = 0; PQR[1] < 3; ++PQR[1])
				for (PQR[2] = 0; PQR[2] < 3; ++PQR[2], id = 0)
					if (formula() == 2) ans++;
		cout << ans << endl;
	}

	return 0;
}
