#include<iostream>
#include<algorithm>
using namespace std;
string s;
int i,j,k,p,ans;

int calc(){
  char c=s[p++];
  if('0'<=c&&c<='9')return (c-'0');
  else if(c=='P')return i;
  else if(c=='Q')return j;
  else if(c=='R')return k;
  else if(c=='-'){
    return 2-calc();
  }else if(c=='('){
    int a=calc();
    char ch=s[p++];
    int b=calc();
    p++;
    if(ch=='*')return min(a,b);
    else if(ch=='+')return max(a,b);
  }
  return 0;
}


void solve(){
  p=0;
  if(calc()==2)ans++;
}
int main(){
  while(1){
    cin>>s;
    if(s==".")break;
    ans=0;
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
	for(k=0;k<3;k++)
	  solve();
    cout<<ans<<endl;
  }
  return 0;
}