#include <bits/stdc++.h>
using namespace std;
int a[] = {2, 1, 0}, b[3][3] = {{0, 0, 0}, {0, 1, 1}, {0, 1, 2}},
    c[3][3] = {{0, 1, 2}, {1, 1, 2}, {2, 2, 2}};
signed main(void) {
  while (true) {
    string s;
    cin >> s;
    if (s == ".") break;
    int ans = 0;
    for (long long p = (long long)0; p < (long long)3; ++p)
      for (long long q = (long long)0; q < (long long)3; ++q)
        for (long long r = (long long)0; r < (long long)3; ++r) {
          string t = s;
          for (long long i = (long long)0; i < (long long)t.size(); ++i) {
            if (t[i] == 'P') t[i] = p + '0';
            if (t[i] == 'Q') t[i] = q + '0';
            if (t[i] == 'R') t[i] = r + '0';
          }
          while (true) {
            int cnt = 0;
            for (long long i = (long long)0; i < (long long)t.size(); ++i) {
              if (t[i] == '-')
                cnt++;
              else {
                if (cnt > 0) {
                  if ('0' <= t[i] && t[i] < '3') {
                    int tmp = t[i] - '0';
                    for (long long j = (long long)0; j < (long long)cnt; ++j) {
                      tmp = a[tmp];
                    }
                    t = t.substr(0, i - cnt) + (char)(tmp + '0') +
                        t.substr(i + 1);
                    i -= cnt;
                  }
                }
                cnt = 0;
              }
            }
            for (long long i = (long long)0; i < (long long)t.size(); ++i) {
              if (t[i] == '+') {
                if (('0' <= t[i - 1] && t[i - 1] < '3') &&
                    ('0' <= t[i + 1] && t[i + 1] < '3')) {
                  int tmp = c[t[i - 1] - '0'][t[i + 1] - '0'];
                  t = t.substr(0, i - 2) + (char)(tmp + '0') + t.substr(i + 3);
                  i -= 3;
                }
              } else if (t[i] == '*') {
                if (('0' <= t[i - 1] && t[i - 1] < '3') &&
                    ('0' <= t[i + 1] && t[i + 1] < '3') && t[i - 2] == '(' &&
                    t[i + 2] == ')') {
                  int tmp = b[t[i - 1] - '0'][t[i + 1] - '0'];
                  t = t.substr(0, i - 2) + (char)(tmp + '0') + t.substr(i + 3);
                  i -= 3;
                }
              }
            }
            if (t.size() == 1) break;
          }
          if (t == "2") ans++;
        }
    cout << ans << endl;
  }
  return 0;
}
