#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

const int64 mod = 1e9+7;

int32 add(int32 a, int32 b){
	if(a == 1 && b == 1) return 1;
	return min(2, a+b);
}

int32 mul(int32 a, int32 b){
	if(a*b == 0) return 0;
	if(a == 1 || b == 1) return 1;
	return 2;
}

int32 nnot(int32 a){
	return 2-a;
}

int32 val[3];

int32 eval(string s){
	if(s.size() == 1 && '0' <= s[0] && s[0] <= '3') return s[0]-'0';
	if(s.size() == 1) return val[s[0]-'P'];
	if(s[0] == '-') return nnot(eval(s.substr(1, s.size()-1)));
	int32 now = 0;
	REP(i, s.size()){
		if(s[i] == '(') now++;
		if(s[i] == ')') now--;
		if(now == 1 && s[i] == '+') return add(eval(s.substr(1, i-1)), eval(s.substr(i+1, s.size()-i-2)));
		if(now == 1 && s[i] == '*') return mul(eval(s.substr(1, i-1)), eval(s.substr(i+1, s.size()-i-2)));
	}
}

int main(void){
	string s;
	while(cin >> s && s != "."){
		int32 res = 0;
		REP(i, 3){
			REP(j, 3){
				REP(k, 3){
					val[0] = i; val[1] = j; val[2] = k;
					if(eval(s) == 2) res++;
				}
			}
		}
		cout << res << endl;
	}
}
