#include <iostream>
#include <string>
#include <cctype>
#include <map>
#include <algorithm>

using namespace std;

string str;
int ps;
map<char, int> mpp;

int formula()
{
  int v, u;
  if(isalpha(str[ps])){
    return mpp[str[ps++]];
  } else if(isdigit(str[ps])){
    return (int)(str[ps++]-'0');
  } else if(str[ps] == '-'){
    ++ps;
    return (2-formula());
  } if(str[ps] == '('){
    ++ps;

    v = formula();
    char ch = str[ps++];
    u = formula();
    ++ps;

    if(ch == '*') v = min(v, u);
    else v = max(v, u);
  }

  return v;
}

int solve(int p, int q, int r)
{
  int res;
  ps = 0;
  mpp['P'] = p;
  mpp['Q'] = q;
  mpp['R'] = r;

  res = formula();

  mpp.clear();
  return res;
}

int main()
{
  int cnt;

  while(cin>>str && str != "."){
    cnt = 0;

    for(int i = 0; i < 3; ++i)
      for(int j = 0; j < 3; ++j)
        for(int k = 0; k < 3; ++k)
          if(solve(i, j, k) == 2) ++cnt;

    cout << cnt << endl;
  }

  return 0;
}