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

char c[128];
string l;
int p;

int no[3]={2,1,0};
int mu[3][3] = {{0,0,0},{0,1,1},{0,1,2}};
int ad[3][3] = {{0,1,2},{1,1,2},{2,2,2}};

int G();

int F(){
	int le = G();
	while(l[p] == '*' || l[p] == '+'){
		if( l[p] == '*' ){
			p++;
			int ri = G();
			le = mu[le][ri];
		}else{
			p++;
			int ri = G();
			le = ad[le][ri];
		}
	}
	return le;
}
int G(){
	if( l[p] == '-' ){
		p++;
		return no[G()];
	}else if( l[p] == '(' ){
		p++;
		int ri = F();
		p++;
		return ri;
	}else{
		if( '0' <= l[p] && l[p] <= '9' ){
			int ri = l[p]-'0';
			p++;
			return ri;
		}else{
			int ri = c[l[p]];
			p++;
			return ri;
		}
	}
	return -1;
}
int main(){
	while(cin >> l && l != "." ){
		int ans = 0;
		for(int i = 0 ; i < 3 ; i++){
			for(int j = 0 ; j < 3 ; j++){
				for(int k = 0 ; k < 3 ; k++){
					c['P'] = i;
					c['Q'] = j;
					c['R'] = k;
					p = 0;
					if( F() == 2 ){
						ans++;
					}
				}
			}
		}
		cout << ans << endl;
		
	}
}