#include <bits/stdc++.h>
using namespace std;
string s;
int func(int P, int Q, int R) {
  bool f = true;
  while (f) {
    f = false;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '(' && (s[i + 2] == '+' || s[i + 2] == '*') &&
          s[i + 4] == ')') {
        f = true;
        int a, b, c;
        a = s[i + 1] - '0';
        b = s[i + 3] - '0';
        c = (s[i + 2] == '+' ? max(a, b) : min(a, b));
        s[i + 4] = '0' + c;
        s = s.substr(0, i) + s.substr(i + 4);
      }
    }
  }
  if (s.size() == 3)
    return s[1] - '0';
  else
    return s[0] - '0';
}
int main(void) {
  while (cin >> s && s != ".") {
    for (int i = 0; i < s.size() - 1; i++) {
      if (s[i] == '-' && s[i + 1] == '-') s = s.substr(0, i) + s.substr(i + 2);
    }
    int ans = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          string _s = s;
          for (int l = 0; l < s.size(); l++) {
            if (s[l] == '-') {
              if (s[l + 1] == 'P')
                s = s.substr(0, l) + s.substr(l + 1), s[l] = '0' + abs(i - 2);
              else if (s[l + 1] == 'Q')
                s = s.substr(0, l) + s.substr(l + 1), s[l] = '0' + abs(j - 2);
              else if (s[l + 1] == 'R')
                s = s.substr(0, l) + s.substr(l + 1), s[l] = '0' + abs(k - 2);
              else if (s[l + 1] == '0')
                s = s.substr(0, l) + s.substr(l + 1), s[l] = '2';
              else if (s[l + 1] == '1')
                s = s.substr(0, l) + s.substr(l + 1);
              else if (s[l + 1] == '2')
                s = s.substr(0, l) + s.substr(l + 1), s[l] = '0';
            } else if (s[l] == 'P')
              s[l] = '0' + i;
            else if (s[l] == 'Q')
              s[l] = '0' + j;
            else if (s[l] == 'R')
              s[l] = '0' + k;
          }
          if (func(i, j, k) == 2) ans++;
          s = _s;
        }
      }
    }
    cout << ans << endl;
  }
}
