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

int calc_neg(int a) { return 2 - a; }
int calc_plus(int a, int b) { return max(a, b); }
int calc_mult(int a, int b) { return min(a, b); }

int calc_op(int l, int r, char op) {
	if(op == '+') return calc_plus(l, r);
	if(op == '*') return calc_mult(l, r);
	assert(false);
}

int parse(const string &s, int &idx, map<char, int> mp) {
	if(s[idx] == '-') {
		idx++;
		return calc_neg(parse(s, idx, mp));
	} else if(s[idx] == '(') {
		idx++;
		int left = parse(s, idx, mp);
		char op = s[idx];
		idx++;
		int right = parse(s, idx, mp);
		assert(s[idx] == ')');
		idx++;
		return calc_op(left, right, op);
	} else if('0' <= s[idx] && s[idx] <= '2') {
		int n = s[idx] - '0';
		idx++;
		return n;
	} else {
		int n = mp[s[idx]];
		idx++;
		return n;
	}
}

bool solve() {
	string s;
	cin >> s;
	if(s == ".") return false;
	map<char, int> mp;
	int ans = 0;
	for(int i = 0; i < 3 * 3 * 3; i++) {
		int cpy = i;
		for(int j = 0; j < 3; j++) {
			mp['P' + j] = cpy % 3;
			cpy /= 3;
		}
		int idx = 0;
		int res = parse(s, idx, mp);
		ans += res == 2;
	}
	cout << ans << endl;
	return true;
}

int main() {
	while(solve());
	return 0;
}

