#include<iostream>
#include<cstdio>
#include<string>
#include <cctype>
#include <cassert>
using namespace std;

typedef int value;

string s;
size_t cur = 0;
value P, Q, R;
int ans = 0;

value Formula();

value Not(){
	assert(s[cur] == '-');
	cur++;
	return 2 - Formula();
}
value And(value a, value b){
	return (a<b) ? a : b;
}
value Or(value a, value b){
	return (a<b) ? b : a;
}
value Formula(){
	value a, b, f;
	char op;
	switch (s[cur]){
	case ('0') :
	case ('1') :
	case ('2') :
			   f = s[cur] - '0';
		cur++;
		break;
	case ('P') :
		f = P;
		cur++;
		break;
	case ('Q') :
		f = Q;
		cur++;
		break;
	case ('R') :
		f = R;
		cur++;
		break;
	case ('-') :
		f = Not();
		break;
	case ('(') :
		cur++;
		a = Formula();
		assert(s[cur] == '*' || s[cur] == '+');
		op = s[cur];
		cur++;
		b = Formula();
		f = (op == '*') ? And(a, b) : Or(a, b);
		assert(s[cur] == ')');
		cur++;
		break;
	default:
		assert(false);
		break;
	}
	return f;
}
int main(){
	cin >> s;
	while (s != "."){
		for (P = 0; P <= 2; P++)
			for (Q = 0; Q <= 2; Q++)
				for (R = 0; R <= 2; R++){
					if (Formula() == 2)ans++;
					cur = 0;
				}
		cout << ans << endl;
		ans = 0;
		cin >> s;
	}
	return 0;
}