#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmax(T &a, T b) {
  a = max(a, b);
}
template <typename T>
void chmin(T &a, T b) {
  a = min(a, b);
}
template <typename T>
void chadd(T &a, T b) {
  a = a + b;
}
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, 0};
constexpr long long INF = 1001001001001001LL;
constexpr long long MOD = 1000000007LL;
long long int vari[3];
string s;
string regenerate(string t) {
  long long int n = t.length();
  string ret = "";
  for (long long int(i) = (0); (i) < (n); (i)++) {
    if (i < n - 1 && t[i] == '-' && t[i + 1] == '-')
      i++;
    else
      ret += t[i];
  }
  return ret;
}
long long int opnot(long long int m) { return 2 - m; }
vector<vector<long long int> > opmul{{0, 0, 0}, {0, 1, 1}, {0, 1, 2}};
vector<vector<long long int> > oppls{{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
long long int retvari(char c) {
  string chk = "PQR";
  if (chk.find(c) != string::npos)
    return vari[chk.find(c)];
  else
    return c - '0';
}
long long int solve(long long int l, long long int r) {
  if (r - l == 1)
    return retvari(s[l]);
  else if (s[l] == '-' && r - l == 2)
    return opnot(retvari(s[l + 1]));
  else {
    long long int cnt = 0, nl = -1, nr = -1;
    long long int vl, vr;
    for (long long int(i) = (l); (i) < (r); (i)++) {
      if (s[i] == '(') {
        cnt++;
      }
      if (s[i] == ')') {
        cnt--;
      }
      if (s[i] == '*' && cnt == 0) {
        vl = solve(l, i);
        vr = solve(i + 1, r);
        return opmul[vl][vr];
      }
      if (s[i] == '+' && cnt == 0) {
        vl = solve(l, i);
        vr = solve(i + 1, r);
        return oppls[vl][vr];
      }
    }
  }
  return solve(l + 1, r - 1);
}
signed main() {
  while (cin >> s, s != ".") {
    s = regenerate(s);
    long long int n = s.length(), cnt = 0;
    for (long long int(i) = (0); (i) < (3); (i)++)
      for (long long int(j) = (0); (j) < (3); (j)++)
        for (long long int(k) = (0); (k) < (3); (k)++) {
          vari[0] = i, vari[1] = j, vari[2] = k;
          if (solve(0, n) == 2) cnt++;
        }
    cout << cnt << endl;
  }
  return 0;
}
