#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
#include <cassert>

using namespace std;

typedef string::const_iterator Iter;

// -X == 2 - X
// X * Y == min(X, Y)
// X + Y == max(X, Y)

int express(Iter &p);

int express(Iter &p) {
	if(*p == '-') {
		p++;
		int r = express(p);
		return 2 - r;
	}
	else if(*p == '(') {
		p++; // skip '('
		int r = express(p);
		if(*p == '*') {
			p++;
			int rs = express(p);
			r = min(r, rs);
		}
		else if(*p == '+') {
			p++;
			int rs = express(p);
			r = max(r, rs);
		}
		else {
			assert(false);
		}

		p++; // skip ')'
		return r;
	}
	else if(isdigit(*p)) {
		int r = (*p) - '0';
		p++;
		return r;
	}
	else
		assert(false);
}

int main() {
	for(;;) {
		string line;
		getline(cin, line);

		if(line == ".") break;

		int ans = 0;
		int var[3];
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				for(int k = 0; k < 3; k++) {
					var[0] = i;
					var[1] = j;
					var[2] = k;

					string s = line;
					for(string::iterator it = s.begin(); it != s.end(); ++it) {
						if(isalpha(*it)) {
							*it = var[(*it) - 'P'] + '0';
						}
					}
					Iter begin = s.begin();
					int r = express(begin);
					if(r == 2) ans++;
				}
			}
		}
		cout << ans << endl;
	}
}