#include <bits/stdc++.h>
using namespace std;
static const int INF = 1e9;
static const double PI = acos(-1.0);
static const double EPS = 1e-10;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
inline int BITS_COUNT(unsigned long long int b) {
  int c = 0;
  while (b != 0) {
    c += (b & 1);
    b >>= 1;
  }
  return c;
}
inline int MSB(unsigned long long int b) {
  int c = 0;
  while (b != 0) {
    ++c;
    b >>= 1;
  }
  return c - 1;
}
inline int MAKE_MASK(unsigned long long int upper,
                     unsigned long long int lower) {
  assert(lower < 64 && upper < 64 && lower <= upper);
  return ((assert((!numeric_limits<typeof(upper)>::is_signed)),
           assert(upper < 64), (1ULL << (upper))) -
          1) ^
         ((assert((!numeric_limits<typeof(lower)>::is_signed)),
           assert(lower < 64), (1ULL << (lower))) -
          1);
}
template <typename T>
ostream& operator<<(ostream& s, const complex<T>& d) {
  return s << "(" << d.real() << ", " << d.imag() << ")";
}
template <typename T1, typename T2>
ostream& operator<<(ostream& s, const pair<T1, T2>& d) {
  return s << "(" << d.first << ", " << d.second << ")";
}
template <typename T>
ostream& operator<<(ostream& s, const vector<T>& d) {
  int len = d.size();
  for (typeof(len) i = (0); i < (len); ++i) {
    s << d[i];
    if (i < len - 1) s << "\t";
  }
  return s;
}
template <typename T>
ostream& operator<<(ostream& s, const vector<vector<T> >& d) {
  int len = d.size();
  for (typeof(len) i = (0); i < (len); ++i) {
    s << d[i] << endl;
  }
  return s;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& s, const map<T1, T2>& m) {
  s << "{" << endl;
  for (typeof(m.begin()) itr = m.begin(); itr != m.end(); ++itr) {
    s << "\t" << (*itr).first << " : " << (*itr).second << endl;
  }
  s << "}" << endl;
  return s;
}
inline double now() {
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return (static_cast<double>(tv.tv_sec) +
          static_cast<double>(tv.tv_usec) * 1e-6);
}
inline vector<string> split(string s, char delimiter) {
  vector<string> v;
  string t;
  for (typeof(s.length()) i = (0); i < (s.length()); ++i) {
    if (s[i] == delimiter)
      v.push_back(t), t = "";
    else
      t += s[i];
  }
  v.push_back(t);
  return v;
}
inline string join(vector<string> s, string j) {
  string t;
  for (typeof(s.size()) i = (0); i < (s.size()); ++i) {
    t += s[i] + j;
  }
  return t;
}
enum { UP, RIGHT, DOWN, LEFT, UP_RIGHT, DOWN_RIGHT, DOWN_LEFT, UP_LEFT };
complex<int> dydx4[4] = {complex<int>(-1, 0), complex<int>(0, 1),
                         complex<int>(1, 0), complex<int>(0, -1)};
complex<int> dydx8[8] = {complex<int>(-1, 0), complex<int>(0, 1),
                         complex<int>(1, 0),  complex<int>(0, -1),
                         complex<int>(-1, 1), complex<int>(1, 1),
                         complex<int>(1, -1), complex<int>(-1, -1)};
bool in_field(int H, int W, complex<int> p) {
  return (0 <= p.real() && p.real() < H) && (0 <= p.imag() && p.imag() < W);
}
inline void input(string filename) { freopen(filename.c_str(), "r", stdin); }
inline void output(string filename) { freopen(filename.c_str(), "w", stdout); }
bool opt_debug = false;
int neg(int v) {
  int ret = v;
  if (v == 0) {
    ret = 2;
  } else if (v == 2) {
    ret = 0;
  }
  return ret;
}
int conj(int lhs, int rhs) {
  if (lhs == 0 || rhs == 0) {
    return 0;
  }
  if (lhs == 2 && rhs == 2) {
    return 2;
  }
  return 1;
}
int disj(int lhs, int rhs) {
  if (lhs == 2 || rhs == 2) {
    return 2;
  }
  if (lhs == 0 && rhs == 0) {
    return 0;
  }
  return 1;
}
class Parser {
 private:
  string line;
  int pos;

 public:
  Parser(string _line) {
    line = _line;
    pos = 0;
  }
  void assign(char var, int val) {
    for (typeof(line.length()) i = (0); i < (line.length()); ++i) {
      if (line[i] == var) {
        line[i] = val + '0';
      }
    }
  }
  int formula() {
    if (line[pos] == '0' || line[pos] == '1' || line[pos] == '2') {
      return constant();
    }
    if (line[pos] == '-') {
      pos++;
      return neg(formula());
    }
    if (line[pos] == '(') {
      pos++;
      int lhs = formula();
      char op = line[pos];
      pos++;
      int rhs = formula();
      assert(line[pos] == ')');
      pos++;
      if (op == '*') {
        return conj(lhs, rhs);
      } else {
        return disj(lhs, rhs);
      }
    }
    assert(false);
    return -1;
  }
  int constant() {
    char c = line[pos];
    pos++;
    return c - '0';
  }
};
int main(int argc, char** argv) {
  std::ios_base::sync_with_stdio(false);
  int __c;
  while ((__c = getopt(argc, argv, "d")) != -1) {
    switch (__c) {
      case 'd':
        opt_debug = true;
        break;
      default:
        abort();
    }
  }
  opt_debug = true;
  input("./inputs/0.txt");
  string s;
  while (cin >> s, s != ".") {
    int ans = 0;
    for (typeof(3) p = (0); p < (3); ++p) {
      for (typeof(3) q = (0); q < (3); ++q) {
        for (typeof(3) r = (0); r < (3); ++r) {
          Parser parser(s);
          parser.assign('P', p);
          parser.assign('Q', q);
          parser.assign('R', r);
          if (parser.formula() == 2) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
