#include <bits/stdc++.h>
using namespace std;
long long nextInt() {
  long long a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}
void inputVector(vector<long long> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextInt());
  }
}
void inputVector(vector<double> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextDouble());
  }
}
void inputVector(vector<string> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextString());
  }
}
char mns(char c) {
  if (c == '0') return '2';
  if (c == '1') return '1';
  return '0';
}
char multi(char c1, char c2) {
  if (c1 == '0' || c2 == '0') return '0';
  if (c1 == '2' && c2 == '2') return '2';
  return '1';
}
char pls(char c1, char c2) {
  if (c1 == '2' || c2 == '2') return '2';
  if (c1 == '0' && c2 == '0') return '0';
  return '1';
}
char eval(string s) {
  if (s[0] != '(') {
    long long i = 0;
    while (s[i] == '-') i++;
    if (i % 2) return mns(s[i]);
    return s[i];
  }
  s = s.substr(1, (long long)s.size() - 2);
  long long cnt = 0;
  long long pt = 0;
  while (!(cnt == 0 && (s[pt] == '+' || s[pt] == '*'))) {
    if (s[pt] == '(')
      cnt++;
    else if (s[pt] == ')')
      cnt--;
    pt++;
  }
  string a = s.substr(0, pt);
  string b = s.substr(pt + 1);
  if (s[pt] == '+') return pls(eval(a), eval(b));
  return multi(eval(a), eval(b));
}
signed main() {
  while (true) {
    string t;
    cin >> t;
    if (t == ".") break;
    long long ret = 0;
    for (char p = '0'; p <= '2'; p++) {
      for (char q = '0'; q <= '2'; q++) {
        for (char r = '0'; r <= '2'; r++) {
          string s = t;
          for (long long i = 0; i < (s.size()); i++) {
            if (s[i] == 'P')
              s[i] = p;
            else if (s[i] == 'Q')
              s[i] = q;
            else if (s[i] == 'R')
              s[i] = r;
          }
          ret += (eval(s) == '2');
        }
      }
    }
    cout << ret << endl;
  }
  return 0;
}
