#include <bits/stdc++.h>
using namespace std;
void reader(int& a) { scanf("%d", &a); }
void reader(double& a) { scanf("%lf", &a); }
void reader(char a[]) { scanf("%s", a); }
void reader(char& a) { scanf(" %c", &a); }
void reader(long long& a) { scanf("%lld", &a); }
void reader(unsigned long long& a) { scanf("%llu", &a); }
template <class T, class U>
void reader(T& t, U& u) {
  reader(t);
  reader(u);
}
template <class T, class U, class V>
void reader(T& t, U& u, V& v) {
  reader(t);
  reader(u);
  reader(v);
}
void writer(const int a, char c) {
  printf("%d", a);
  putchar(c);
}
void writer(const long long a, char c) {
  printf("%lld", a);
  putchar(c);
}
void writer(const unsigned long long a, char c) {
  printf("%llu", a);
  putchar(c);
}
void writer(const double a, char c) {
  printf("%.20lf", a);
  putchar(c);
}
void writer(const char a[]) { printf("%s", a); };
void writer(const char a[], char c) {
  printf("%s", a);
  putchar(c);
};
void writer(const char a, char c) {
  putchar(a);
  putchar(c);
};
template <class T>
void writerLn(T t) {
  writer(t, '\n');
}
template <class T, class U>
void writerLn(T t, U u) {
  writer(t, ' ');
  writer(u, '\n');
}
template <class T, class U, class V>
void writerLn(T t, U u, V v) {
  writer(t, ' ');
  writer(u, ' ');
  writer(v, '\n');
}
template <class T>
void writerArr(T x[], int n) {
  int i;
  if (!n) {
    putchar('\n');
    return;
  }
  for (i = 0; i < n - 1; ++i) writer(x[i], ' ');
  writer(x[n - 1], '\n');
}
template <class T>
void writerVec(vector<T> x) {
  int n = x.size();
  int i;
  if (!n) {
    putchar('\n');
    return;
  }
  for (i = 0; i < n - 1; ++i) writer(x[i], ' ');
  writer(x[n - 1], '\n');
}
vector<string> split(const string& str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}
bool test = 0;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
unsigned long long mod = (int)1e9 + 7;
int N, M;
int dp[200005];
bool taked[200005];
int P, Q, R;
int number(string& s, int& i);
int factor(string& s, int& i);
int not_calc(string& s, int& i);
int term(string& s, int& i);
int expr(string& s, int& i);
int number(string& s, int& i) {
  int res;
  switch (s[i]) {
    case 'P':
      res = P;
      break;
    case 'Q':
      res = Q;
      break;
    case 'R':
      res = R;
      break;
    default:
      res = s[i] - '0';
  }
  i++;
  if (test) {
    cerr << "i"
         << " = " << (i) << endl;
    fprintf(stderr, "number ");
    cerr << "res"
         << " = " << (res) << endl;
  }
  return res;
}
int factor(string& s, int& i) {
  if (s[i] != '(') return number(s, i);
  i++;
  int ret = expr(s, i);
  i++;
  if (test) {
    fprintf(stderr, "factor : ");
    cerr << "ret"
         << " = " << (ret) << endl;
  }
  return ret;
}
int not_calc(string& s, int& i) {
  int val;
  int cnt = 0;
  while (s[i] == '-') {
    cnt++;
    i++;
  }
  val = factor(s, i);
  if (cnt % 2) {
    val = 2 - val;
  }
  if (test) {
    fprintf(stderr, "not_calc ");
    cerr << "val"
         << " = " << (val) << endl;
  }
  return val;
}
int term(string& s, int& i) {
  int val = not_calc(s, i);
  while (s[i] == '*') {
    i++;
    int val2 = not_calc(s, i);
    if (val == 0 || val2 == 0)
      val = 0;
    else if (val == 0 || val2 == 1)
      val = 1;
  }
  if (test) {
    fprintf(stderr, "term ");
    cerr << "val"
         << " = " << (val) << endl;
  }
  return val;
}
int expr(string& s, int& i) {
  int val = term(s, i);
  while (s[i] == '+') {
    i++;
    int val2 = term(s, i);
    if (val == 2 || val2 == 2)
      val = 2;
    else if (val == 1 || val == 1)
      val = 1;
  }
  if (test) {
    fprintf(stderr, "sum ");
    cerr << "val"
         << " = " << (val) << endl;
  }
  return val;
}
signed main(void) {
  int i, j, k;
  while (1) {
    string s;
    int cnt = 0;
    cin >> s;
    if (s == ".") return 0;
    for (P = 0; P < 3; ++P)
      for (Q = 0; Q < 3; ++Q)
        for (R = 0; R < 3; ++R) {
          i = 0;
          if (expr(s, i) == 2) cnt++;
        }
    cout << cnt << endl;
  }
  return 0;
}
