#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <stack>
#include <queue>
#include <cassert>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
typedef pair<int,int> P;
typedef pair<int,P>Q;
#define fi first
typedef long long ll;
#define sc second

string s;
int n;
int rec(int L,int R,int a,int b,int c){
	int xx[3]={a,b,c};
	if('0'<=s[L] && s[L] <= '2') return s[L]-'0';
	if('P'<=s[L] && s[L] <= 'R') return xx[s[L]-'P'];
	if(s[L] == '-') return 2 - rec(L+1,R,a,b,c);
	int dep = 0;
	for(int i=L+1;i<R;i++){
		if(s[i] == '(') dep++;
		else if(s[i] == ')') dep--;
		else if(dep == 0 && s[i] == '*'){
			int ll = rec(L+1,i-1,a,b,c);
			int rr = rec(i+1,R-1,a,b,c);
			if(ll == 0  || rr == 0) return 0;
			else if(ll==2&&rr==2) return 2;
			else return 1;
		}
		else if(dep == 0 && s[i] == '+'){
			int ll = rec(L+1,i-1,a,b,c);
			int rr = rec(i+1,R-1,a,b,c);
			if(ll==0&&rr==0) return 0;
			else if(ll<=1&&rr<=1) return 1;
			else return 2;
		}
	}
	assert(0);
}
int main(){
	while(1){
		cin>>s;n=s.size();if(s==".")return 0;
		int ans =0;
		rep(i,3)rep(j,3)rep(k,3) ans += rec(0,n-1,i,j,k) == 2;
		cout << ans << endl;
	}
}

