#include <bits/stdc++.h>
using namespace std;
string s, s_copy;
int ans = 0;
int point = 0;
char ch(int i) {
  if (i == 0)
    return '0';
  else if (i == 1)
    return '1';
  else
    return '2';
}
void set(int i, int j, int z) {
  s_copy = s;
  int c = s.size();
  for (int k = 0; k < c; k++) {
    if (s_copy[k] == 'P') s_copy[k] = ch(i);
    if (s_copy[k] == 'Q') s_copy[k] = ch(j);
    if (s_copy[k] == 'R') s_copy[k] = ch(z);
  }
}
int mainus(int i) {
  switch (i) {
    case 0:
      return 2;
    case 1:
      return 1;
    case 2:
      return 0;
  }
}
int kakeru(int x, int y) { return x > y ? y : x; }
int tasu(int x, int y) { return x > y ? x : y; }
int calc() {
  switch (s_copy[point++]) {
    case '0':
      return 0;
    case '1':
      return 1;
    case '2':
      return 2;
    case '-':
      return mainus(calc());
    case '(':
      int left = calc();
      char temp = s_copy[point++];
      int right = calc();
      if (temp == '+')
        return tasu(left, right);
      else
        return kakeru(left, right);
  }
}
int main() {
  while (true) {
    cin >> s;
    if (s == ".") break;
    for (int i = 0; i <= 2; i++)
      for (int j = 0; j <= 2; j++)
        for (int z = 0; z <= 2; z++) {
          set(i, j, z);
          point = 0;
          if (calc() == 2) ans++;
        }
    cout << ans << "\n";
    ans = 0;
  }
  return 0;
}
