#include <bits/stdc++.h>
using namespace std;
static const double PI = 6 * asin(0.5);
static const int INF = 1 << 24;
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
string s, tmp;
char aa[] = {'P', 'Q', 'R'};
string schange(string s, char a, char b) {
  for (int(i) = (0); (i) < ((s.size())); ++(i)) {
    if (s[i] == a) {
      s[i] = b;
    }
  }
  return s;
}
string NOT(string t) {
  if (t[1] == '1')
    return "1";
  else if (t[1] == '2')
    return "0";
  return "2";
}
string AND(string t) {
  int a, b;
  char r;
  stringstream ss1;
  ss1 << t;
  ss1 >> a;
  ss1 >> r;
  ss1 >> b;
  if (a == 0 || b == 0)
    return "0";
  else if (a == 1 || b == 1)
    return "1";
  return "2";
}
string OR(string t) {
  int a, b;
  char r;
  stringstream ss1;
  ss1 << t;
  ss1 >> a;
  ss1 >> r;
  ss1 >> b;
  if (a == 2 || b == 2)
    return "2";
  else if (a == 1 || b == 1)
    return "1";
  return "0";
}
bool isnum(char t) {
  if (t >= '0' && t <= '2') return true;
  return false;
}
int calc(string tmp) {
  if (tmp.size() == 1 && tmp[0] == '2')
    return 2;
  else if (tmp.size() == 1)
    return 0;
  for (int(i) = (0); (i) < ((tmp.size())); ++(i)) {
    if (tmp[i] == '(') {
      if (tmp[i + 2] == ')') {
        tmp = tmp.substr(0, i) + tmp.substr(i + 1, 1) + tmp.substr(i + 3);
      }
    } else if (tmp[i] == '-') {
      if (isnum(tmp[i + 1])) {
        tmp = tmp.substr(0, i) + NOT(tmp.substr(i, 2)) + tmp.substr(i + 2);
      }
    } else if (tmp[i] == '*') {
      if (isnum(tmp[i - 1]) && isnum(tmp[i + 1])) {
        tmp = tmp.substr(0, i - 1) + AND(tmp.substr(i - 1, 3)) +
              tmp.substr(i + 2);
      }
    } else if (tmp[i] == '+') {
      if (isnum(tmp[i - 1]) && isnum(tmp[i + 1])) {
        tmp =
            tmp.substr(0, i - 1) + OR(tmp.substr(i - 1, 3)) + tmp.substr(i + 2);
      }
    }
  }
  return calc(tmp);
}
int solve() {
  int ans = 0;
  int c = 0;
  for (int(i) = (0); (i) < ((3)); ++(i)) {
    if (s.find(aa[i])) {
      s = schange(s, aa[i], 'a' + c);
      c++;
    }
  }
  for (int(i) = (0); (i) < ((3)); ++(i)) {
    for (int(j) = (0); (j) < ((3)); ++(j)) {
      for (int(k) = (0); (k) < ((3)); ++(k)) {
        tmp = s;
        tmp = schange(tmp, 'a', i + '0');
        tmp = schange(tmp, 'b', j + '0');
        tmp = schange(tmp, 'c', k + '0');
        if (calc(tmp) == 2) {
          ans++;
        }
      }
    }
  }
  return ans;
}
int main() {
  while (cin >> s, s != ".") {
    cout << solve() << endl;
  }
  return 0;
}
