#include <bits/stdc++.h>

using namespace std;

string str;
int pos;
int P, Q, R;

int formula() {
	int res = -1;
	if (str[pos] == '-') {
		++pos;
		int a = formula();
		res = 2 - a;
	}
	else if (str[pos] == '(') {
		++pos;
		int a = formula();
		char c = str[pos];
		++pos;
		int b = formula();
		++pos;

		if (c == '*') {
			res = min(a, b);
		}
		else {
			res = max(a, b);
		}
	}
	else if (str[pos] == 'P') {
		res = P;
		++pos;
	}
	else if (str[pos] == 'Q') {
		res = Q;
		++pos;
	}
	else if (str[pos] == 'R') {
		res = R;
		++pos;
	}
	else if (isdigit(str[pos])) {
		res = str[pos] - '0';
		++pos;
	}
	else {
		assert(false);
	}
	return res;
}

int parse(string s) {
	str = s;
	pos = 0;
	return formula();
}

int main() {
	string s;
	while ( cin >> s, s != "." ) {
		int ans = 0;
		for (int p = 0; p < 3; ++p) for (int q = 0; q < 3; ++q) for (int r = 0; r < 3; ++r) {
			P = p, Q = q, R = r;
			if ( parse(s) == 2 ) {
				++ans;
			}
		}
		cout << ans << endl;
	}
}