#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const ll LINF = 1ll<<50;
template<typename T>
void printv(const vector<T>& s) {
  for(int i=0;i<(int)(s.size());++i) {
    cout << s[i];
    if(i == (int)(s.size())-1) cout << endl;
    else cout << " ";
  }
}
class Exp {
public:
  char op, val;
  Exp *lch, *rch;
  Exp() {}
};
Exp *pl(const string &s, int l, int r);
Exp *mult(const string &s, int l, int r);
Exp *mi(const string &s, int l, int r);
Exp *term(const string &s, int l, int r);
Exp *pl(const string &s, int l, int r) {
  if(l >= r) return nullptr;
  int cnt = 0;
  for(int i=l;i<r;++i) {
    if(s[i] == '(') {
      cnt++;
    } else if(s[i] == ')') {
      cnt--;
    } else if(s[i] == '+' && cnt == 0) {
      Exp *res = new Exp();
      res->op = '+';
      res->lch = pl(s, l, i);
      res->rch = pl(s, i+1, r);
      return res;
    }
  }
  return mult(s, l, r);
}
Exp *mult(const string &s, int l, int r) {
  if(l >= r) return nullptr;
  int cnt = 0;
  for(int i=l;i<r;++i) {
    if(s[i] == '(') {
      cnt++;
    } else if(s[i] == ')') {
      cnt--;
    } else if(s[i] == '*' && cnt == 0) {
      Exp *res = new Exp();
      res->op = '*';
      res->lch = mult(s, l, i);
      res->rch = mult(s, i+1, r);
      return res;
    }
  }
  return mi(s, l, r);
}
Exp *mi(const string &s, int l, int r) {
  if(l >= r) return nullptr;
  if(s[l] == '-') {
    Exp *res = new Exp();
    res->op = '-';
    res->rch = mi(s, l+1, r);
    return res;
  } else {
    return term(s, l, r);
  }
}
Exp *term(const string &s, int l, int r) {
  if(s[l] == '(' && s[r-1] == ')') {
    return pl(s, l+1, r-1);
  }
  Exp *res = new Exp();
  res->val = s[l];
  return res;
}
int solve(Exp *now, int p, int q, int r) {
  if(now->op == '*') {
    int lch = solve(now->lch, p, q, r);
    int rch = solve(now->rch, p, q, r);
    if(lch == 0) {
      return 0;
    } else if(lch == 1) {
      if(rch == 0) return 0;
      else return 1;
    } else {
      return rch;
    }
  } else if(now->op == '+') {
    int lch = solve(now->lch, p, q, r);
    int rch = solve(now->rch, p, q, r);
    if(lch == 0) {
      return rch;
    } else if(lch == 1) {
      if(rch == 2) return 2;
      else return 1;
    } else {
      return 2;
    }
  } else if(now->op == '-') {
    int rch = solve(now->rch, p, q, r);
    if(rch == 0) {
      return 2;
    } else if(rch == 1) {
      return 1;
    } else if(rch == 2) {
      return 0;
    }
  } else if(now->val == '0') {
    return 0;
  } else if(now->val == '1') {
    return 1;
  } else if(now->val == '2') {
    return 2;
  } else if(now->val == 'P') {
    return p;
  } else if(now->val == 'Q') {
    return q;
  } else if(now->val == 'R') {
    return r;
  }
  return 0;
}
int main() {
  cin.tie(0);
  cout << fixed << setprecision(10);
  while(1) {
    string s; cin >> s;
    if(s == ".") break;
    Exp *res = pl(s, 0, (int)(s.size()));
    int cnt = 0;
    for(int i=0;i<=2;++i) {
      for(int j=0;j<=2;++j) {
        for(int k=0;k<=2;++k) {
          cnt += solve(res, i, j, k) == 2;
        }
      }
    }
    cout << cnt << endl;
  }
}



Measured memory_bytes: 3072