#include <bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {1, 0, -1, 0};
int mul(int X, int Y) {
  if (X == 0 || Y == 0) return 0;
  if (X == 1 || Y == 1) return 1;
  if (X == 2) return Y;
  assert(false);
}
int add(int X, int Y) {
  if (X == 0) return Y;
  if (X == 1 && (Y == 0 || Y == 1)) return 1;
  if (X == 1) return 2;
  if (X == 2) return 2;
  assert(false);
}
int sub(int X) {
  if (X == 0) return 2;
  if (X == 2) return 0;
  return 1;
}
class Parsing {
 private:
  string parse;
  int pos;

 public:
  Parsing(string s) {
    parse = s;
    pos = 0;
  }
  int fact() {
    if (parse[pos] == '(') {
      pos++;
      int p = expression();
      pos++;
      return p;
    } else if (parse[pos] == '-') {
      int p = 0;
      int cnt = 0;
      while (parse[pos] == '-') {
        cnt++, pos++;
      }
      p = parse[pos] - '0';
      pos++;
      for (int i = 0; i < cnt; i++) {
        p = sub(p);
      }
      return p;
    } else {
      int p = 0;
      while ('0' <= parse[pos] && parse[pos] <= '2') {
        p *= 10;
        p += parse[pos] - '0';
        pos++;
      }
      return p;
    }
  }
  int term() {
    int p = fact();
    return p;
  }
  int expression() {
    int p = term();
    while (parse[pos] == '+' || parse[pos] == '*') {
      if (parse[pos] == '*') {
        pos++;
        int q = term();
        p = mul(p, q);
      } else if (parse[pos] == '+') {
        pos++;
        int q = term();
        p = add(p, q);
      }
    }
    return p;
  }
};
int ans, last;
string s;
void draw(char pair<int, int>, char Q, char R) {
  string pre = s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == 'P')
      s[i] = pair<int, int>;
    else if (s[i] == 'Q')
      s[i] = Q;
    else if (s[i] == 'R')
      s[i] = R;
  }
  Parsing par = Parsing(s);
  if (par.expression() == 2) ans++;
  s = pre;
}
void test() {
  for (char i = '0'; i <= '2'; i++) {
    for (char j = '0'; j <= '2'; j++) {
      for (char k = '0'; k <= '2'; k++) {
        draw(i, j, k);
      }
    }
  }
}
int main() {
  while (true) {
    cin >> s;
    if (s == ".") break;
    ans = 0;
    bool nise = true;
    for (int i = 0; i < s.length(); i++)
      if (s[i] == 'P' || s[i] == 'Q' || s[i] == 'R') {
        nise = false;
        last = i;
      }
    if (nise) {
      Parsing par = Parsing(s);
      if (par.expression() == 2)
        cout << 27 << endl;
      else
        cout << 0 << endl;
      continue;
    }
    test();
    cout << ans << endl;
  }
  return 0;
}
