#include <iostream>
#include <string>
using namespace std;

int p, q, r, cursor;
string s;

int not(int a) {
	return 2 - a;
}

int and(int a, int b) {
	if(a == 0 || b == 0) return 0;
	if(a == 1 || b == 1) return 1;
	return 2;
}

int or(int a, int b) {
	if(a == 2 || b == 2) return 2;
	if(a == 1 || b == 1) return 1;
	return 0;
}

int f() {
	char c = s[cursor];
	cursor++;
	if(c == '0') return 0;
	if(c == '1') return 1;
	if(c == '2') return 2;
	if(c == 'P') return p;
	if(c == 'Q') return q;
	if(c == 'R') return r;
	if(c == '-') return not(f());
	if(c == '(') {
		int a = f();
		char op = s[cursor];
		cursor++;
		int b = f();
		cursor++;
		if(op == '*') {
			return and(a, b);
		}
		else {
			return or(a, b);
		}
	}
	return -1;
}

int main() {
	while(cin >> s, s[0] != '.') {
		int ans = 0;
		for(p = 0; p <= 2; p++) {
			for(q = 0; q <= 2; q++) {
				for(r = 0; r <= 2; r++) {
					cursor = 0;
					if(f() == 2) ans++;
				}
			}
		}
		cout << ans << endl;
	}
}