#include <bits/stdc++.h>
using namespace std;
int v[3], n, c;
string s;
int f() {
  n++;
  int r;
  if (s[n - 1] == '(')
    r = f(), n++, r = s[n - 1] == '+' ? max(r, f()) : min(r, f()), n++;
  if (s[n - 1] == '-') r = 2 - f();
  if (isdigit(s[n - 1]))
    r = s[n - 1] - '0';
  else
    r = v[s[n - 1] - 'P'];
  return r;
}
int main() {
  cin >> s;
  if (s != ".") {
    c = 0;
    for ((v[0]) = 0; (v[0]) < (3); v[0]++)
      for ((v[1]) = 0; (v[1]) < (3); v[1]++)
        for ((v[2]) = 0; (v[2]) < (3); v[2]++) n = 0, c += f() / 2;
    cout << c << endl;
    main();
  }
}
