#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <cassert>
using namespace std;

#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REP(i,n) FOR(i,0,n)
typedef vector<int> vi;
typedef vector<vi> vii;
#define ALL(a) a.begin(),a.end()

int mynot(int n){
	switch(n){
	case 0:return 2;
	case 1:return 1;
	case 2:return 0;
	default:assert(0);
	}
}
int myand(int x,int y){
	switch(x){
	case 0:
		return 0;
	case 1:
		return (y==0?0:1);
	case 2:
		return y;
	default:
		assert(0);
	}
}
int myor(int x,int y){
	switch(x){
	case 0:
		return y;
	case 1:
		return (y==2?2:1);
	case 2:
		return 2;
	default:
		assert(0);
	}
}
int n;
int go(string s,int p,int q,int r){
	int a,b,sign,v;
	switch(s[n]){
	case '0':
	case '1':
	case '2':
		v=s[n];
		n++;
		return v-'0';
	case 'P':
		n++;
		return p;
	case 'Q':
		n++;
		return q;
	case 'R':
		n++;
		return r;
	case '-':
		n++;
		return mynot(go(s,p,q,r));
	case '(':
		n++;
		a=go(s,p,q,r);
		sign=s[n];
		n++;
		b=go(s,p,q,r);
		n++;
		switch(sign){
		case '*':
			return myand(a,b);
		case '+':
			return myor(a,b);
		}
	case ')':
	default:
		assert(0);
	}
}

int main(){
	string s;
	while(cin>>s,s!="."){
		int ans=0;
		REP(p,3)REP(q,3)REP(r,3){
			n=0;
			if(go(s,p,q,r)==2){
				ans++;
			}
		}
		cout<<ans<<endl;
	}
}