#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int formula(string& s, int& i) {
	if(s[i] == '(') {
		i++;
		int val = formula(s, i);

		char op = s[i];

		i++;
		int val2 = formula(s, i);

		int ret = 0;
		if(op == '+') {
			if(val == 2 || val2 == 2) ret = 2;
			else if(val == 1 || val2 == 1) ret = 1;
			else ret = 0;
		}
		if(op == '*') {
			if(val == 2 && val2 == 2) ret = 2;
			else if((val == 1 || val == 2) && (val2 == 1 || val2 == 2)) ret = 1;
			else ret = 0;
		}

		i++;
		return ret;
	} else if(isdigit(s[i])) {
		int ret = (s[i] - '0');
		i++;
		return ret;
	} else if(s[i] == '-') {
		i++;
		int val = formula(s, i);

		int ret = 0;
		if(val == 0) ret = 2;
		if(val == 1) ret = 1;
		if(val == 2) ret = 0;

		return ret;
	}
}


int main() {
	string s;
	while(cin >> s) {
		if(s == ".") break;

		int ans = 0;
		rep(i, 3) {
			rep(j, 3) {
				rep(k, 3) {
					string t = s;
					rep(l, s.size()) {
						if(t[l] == 'P') t[l] = ('0' + i);
						else if(t[l] == 'Q') t[l] = ('0' + j);
						else if(t[l] == 'R') t[l] = ('0' + k);
					}

					int p = 0;
					if(formula(t, p) == 2) {
						ans++;
					}
				}
			}
		}

		cout << ans << endl;
	}

	return 0;
}