#include <bits/stdc++.h>
using namespace std;
char sb[100];
char s[100];
bool ch(int, int, int);
int stsy(char*, int, int, int);
int pr(int, int);
int ka(int, int);
int main() {
  while (1) {
    cin >> sb;
    if (s[0] == '.') break;
    int co = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        for (int k = 0; k < 3; k++) {
          for (int l = 0; l < 100; l++) {
            s[l] = sb[l];
          }
          int ans;
          ans = stsy(s, i, j, k);
          if (ans == 2) co++;
        }
      }
    }
    cout << co << endl;
  }
  return 0;
}
int stsy(char* cp, int p, int q, int r) {
  int d = -1;
  int d2 = -1;
  int mi = 0;
  int t = 0;
  int x = 0;
  while (1) {
    if (*cp == ')' || *cp == '\0')
      return d2;
    else if (*cp == '(') {
      d = stsy(cp + 1, p, q, r);
      for (; *cp != ')'; cp++)
        ;
      *cp = 'x';
      cp++;
    } else if (*cp == '-') {
      if (mi == 0)
        mi = 1;
      else
        mi = 0;
    } else if (*cp == 'P')
      d = p;
    else if (*cp == 'Q')
      d = q;
    else if (*cp == 'R')
      d = r;
    else if (*cp == '0')
      d = 0;
    else if (*cp == '1')
      d = 1;
    else if (*cp == '2')
      d = 2;
    else if (*cp == '+')
      t = 1;
    else if (*cp == '*')
      x = 1;
    if (d != -1 && mi == 1) {
      if (d == 2)
        d = 0;
      else if (d == 0)
        d = 2;
    }
    if (t == 1 && d != -1) {
      d2 = pr(d, d2);
      d = -1;
      t = 0;
    }
    if (x == 1 && d != -1) {
      d2 = ka(d, d2);
      d = -1;
      x = 0;
    }
    if (d != -1) {
      d2 = d;
    }
    d = -1;
    cp++;
  }
}
int pr(int a, int b) {
  if (a == 0) return b;
  if (a == 2 || b == 2) return 2;
  return 1;
}
int ka(int a, int b) {
  if (a == 0 || b == 0) return 0;
  if (a == 2 && b == 2) return 2;
  return 1;
}
