#include <bits/stdc++.h>
using namespace std;
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
void change(string &s, char x, char b) {
  for (int i = (0); i < (s.size()); i++) {
    if (s[i] == x) {
      s[i] = b;
    }
  }
}
vector<vector<int>> AND({{0, 0, 0}, {0, 1, 1}, {0, 1, 2}}),
    OR({{0, 1, 2}, {1, 1, 2}, {2, 2, 2}});
vector<int> NOT({2, 1, 0});
vector<char> vc{'P', 'Q', 'R'};
int eval(string &s, int &k) {
  if (1) {
    cerr << "s.substr(k)"
         << " " << s.substr(k) << endl;
  };
  if (1) {
    cerr << "k"
         << " " << k << endl;
  };
  if (isdigit(s[k])) {
    return s[k] - '0';
  } else if (s[k] == '-') {
    return NOT[eval(s, ++k)];
  } else if (s[k] == '(') {
    k++;
    int a = eval(s, k);
    if (1) {
      cerr << "a"
           << " " << a << endl;
    };
    k++;
    char op = s[k];
    if (1) {
      cerr << "op"
           << " " << op << endl;
    };
    k++;
    int b = eval(s, k);
    k++;
    if (1) {
      cerr << "b"
           << " " << b << endl;
    };
    if (op == '*')
      return AND[a][b];
    else if (op == '+')
      return OR[a][b];
  }
}
signed main() {
  string s;
  while (cin >> s, s != ".") {
    int cnt(0);
    for (char P = '0'; P <= '2'; P++) {
      for (char Q = '0'; Q <= '2'; Q++) {
        for (char R = '0'; R <= '2'; R++) {
          string ex(s);
          change(ex, 'P', P);
          change(ex, 'Q', Q);
          change(ex, 'R', R);
          int k(0);
          if (eval(ex, k) == 2) cnt++;
        }
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
