#include <bits/stdc++.h>
using namespace std;
int p, q, r, pos;
string in;
int Formula();
int Seki(int, int);
int Wa(int, int);
int Not(int);
int Num();
int main() {
  while (1) {
    cin >> in;
    if (in == ".") break;
    int ans = 0;
    for (p = 0; p < 3; p++) {
      for (q = 0; q < 3; q++) {
        for (r = 0; r < 3; r++) {
          pos = 0;
          int tmp = Formula();
          if (tmp == 2) ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
int Formula() {
  int x;
  if (in[pos] == '(') {
    pos++;
    x = Formula();
    pos++;
  } else if (in[pos] == '-') {
    int count = 0;
    while (in[pos] == '-') {
      count++;
      pos++;
    }
    x = Num();
    for (int i = 0; i < count; i++) {
      x = (Not(x));
    }
  } else {
    x = Num();
  }
  if (pos < in.size() - 1) {
    if (in[pos + 1] == '*') {
      pos += 2;
      x = Seki(x, Formula());
    } else if (in[pos + 1] == '+') {
      pos += 2;
      x = Wa(x, Formula());
    }
  }
  return x;
}
int Seki(int a, int b) {
  if (a == 0) {
    return 0;
  } else if (a == 1) {
    if (b == 0)
      return 0;
    else
      return 1;
  } else {
    return b;
  }
}
int Wa(int a, int b) {
  if (a == 0) {
    return b;
  } else if (a == 1) {
    if (b == 2)
      return 2;
    else
      return 1;
  } else {
    return 2;
  }
}
int Not(int a) {
  if (a == 0) return 2;
  if (a == 2) return 0;
  return 1;
}
int Num() {
  int x;
  if (in[pos] == 'P') {
    x = p;
  } else if (in[pos] == 'Q') {
    x = q;
  } else if (in[pos] == 'R') {
    x = r;
  } else if (in[pos] == '0') {
    x = 0;
  } else if (in[pos] == '1') {
    x = 1;
  } else if (in[pos] == '2') {
    x = 2;
  }
  return x;
}
