#include <bits/stdc++.h>
using namespace std;

string s;
int it, n;
int vals;

int formula() {
	int res;
	if (s[it] == '-') {
		it++;
		res = 2 - formula();
	}
	else if ('0' <= s[it] && s[it] <= '2') {
		res = s[it] - '0';
		it++;
	}
	else if (s[it] == 'P') {
		it++;
		res = vals % 3;
	}
	else if (s[it] == 'Q') {
		it++;
		res = vals / 3 % 3;
	}
	else if (s[it] == 'R') {
		it++;
		res = vals / 9;
	}
	else {
		assert(s[it++] == '(');
		res = formula();
		if (s[it] == '*') {
			it++;
			res = min(res, formula());
		}
		else {
			assert(s[it++] == '+');
			res = max(res, formula());
		}
		assert(s[it++] == ')');
	}
	return res;
}

int main()
{
	while (cin >> s, s != ".") {
		n = s.size();
		int res = 0;
		for (vals = 0; vals < 27; vals++) {
			it = 0;
			if (formula() == 2) res++;
		}
		cout << res << endl;
	}
	return 0;
}