#include <algorithm>
#include <string>
#include <queue>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <vector>
 
using namespace std;
#define reps(i,f,n) for(int i=f; i<int(n); ++i)
#define rep(i,n) reps(i,0,n)
 
typedef vector<int> vi;
typedef vector<vi> vvi;
 
const int INF = 1001001001;
 
int formula(const string& str, int& cur)
{
//  puts(str.substr(cur).c_str());
    if(isdigit(str[cur])){
        ++cur;
        return str[cur-1] - '0';
    }
    if(str[cur] == '-'){
        ++cur;
        return 2 - formula(str, cur);
    }
 
    ++cur;
    int a = formula(str, cur);
    char op = str[cur];
    ++cur;
    int b = formula(str, cur);
    ++cur;
    if(op == '+')
        return max(a, b);
    else
        return min(a, b);
}
 
int main()
{
    char str[128];
    while(scanf("%s", str), str[0]!='.'){
        int n = strlen(str);
        int cnt = 0;
        rep(p, 3) rep(q, 3) rep(r, 3){
            string tmp = str;
            rep(i, n){
                if(tmp[i] == 'P') tmp[i] = p + '0';
                if(tmp[i] == 'Q') tmp[i] = q + '0';
                if(tmp[i] == 'R') tmp[i] = r + '0';
            }
            int cur = 0;
            cnt += formula(tmp, cur) == 2;
        }
 
        printf("%d\n", cnt);
    }
    return 0;
}