#include <bits/stdc++.h>
using namespace std;
const int _and[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
const int _or[3][3] = {{0, 1, 2}, {0, 1, 2}, {2, 2, 2}};
const int _not[3] = {2, 1, 0};
string s;
int idx;
int term1(void);
int term2(void);
int expr(void);
int term1(void) {
  if (s[idx] == '-') {
    idx++;
    return _not[term1()];
  } else
    return term2();
}
int term2(void) {
  int res = 0;
  if (s[idx] == '(') {
    idx++;
    res = expr();
    idx++;
  } else {
    return (s[idx] - '0');
  }
  return res;
}
int expr(void) {
  int a = term1();
  while (idx < s.size() && s[idx] != ')') {
    char op = s[idx++];
    int b = term1();
    switch (op) {
      case '*':
        a = _and[a][b];
        break;
      case '+':
        a = _or[a][b];
        break;
    }
  }
  return a;
}
int parse(void) {
  idx = 0;
  int res = expr();
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  string str = "";
  while (cin >> str, str != ".") {
    int res = 0;
    int n = str.length();
    for (int p = 0; p < (3); p++) {
      string s1 = str;
      for (int i = 0; i < (n); i++)
        if (s1[i] == 'P') s1[i] = (char)('0' + p);
      for (int q = 0; q < (3); q++) {
        string t1 = s1;
        for (int i = 0; i < (n); i++)
          if (t1[i] == 'Q') t1[i] = (char)('0' + q);
        for (int r = 0; r < (3); r++) {
          s = t1;
          for (int i = 0; i < (n); i++)
            if (s[i] == 'R') s[i] = (char)('0' + r);
          int ans = parse();
          if (ans == 2) res++;
        }
      }
    }
    cout << res << endl;
  }
  return 0;
}
