#include <bits/stdc++.h>
using namespace std;
int n;
int dot(int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  else if (a == 2 && b == 2)
    return 2;
  else
    return 1;
}
int add(int a, int b) {
  if (a == 2 || b == 2)
    return 2;
  else if (a == 0 && b == 0)
    return 0;
  else
    return 1;
}
int rec(int P, int Q, int R, string s, int l, int r) {
  int resp;
  if (s[l] == '(') {
    int a, b;
    int d = 0;
    int enzan_idx;
    for (int i = l + 1; i < n; i++) {
      if (s[i] == '(')
        d++;
      else if (s[i] == ')')
        d--;
      if ((s[i] == '+' || s[i] == '*') && d == 0) {
        enzan_idx = i;
        break;
      }
    }
    a = rec(P, Q, R, s, l + 1, enzan_idx);
    b = rec(P, Q, R, s, enzan_idx + 1, r - 1);
    if (s[enzan_idx] == '+')
      resp = add(a, b);
    else
      resp = dot(a, b);
  } else {
    int mainus = 0;
    int idx;
    for (int i = l; i < n; i++) {
      if (s[i] == '-')
        mainus = (mainus + 1) % 2;
      else {
        idx = i;
        break;
      }
    }
    int res;
    if (s[idx] == 'P')
      res = P;
    else if (s[idx] == 'Q')
      res = Q;
    else if (s[idx] == 'R')
      res = R;
    else
      res = (int)(s[idx] - '0');
    if (mainus) {
      if (res == 2)
        res = 0;
      else if (res == 0)
        res = 2;
    }
    resp = res;
  }
  return resp;
}
int main() {
  while (1) {
    string s;
    cin >> s;
    if (s == ".") break;
    n = (int)s.size();
    int ans = 0;
    for (int P = 0; P < 3; P++) {
      for (int Q = 0; Q < 3; Q++) {
        for (int R = 0; R < 3; R++) {
          ans += (rec(P, Q, R, s, 0, n) == 2);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
