#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
using namespace std;
#ifdef DEBUGRUN
#define log(a) (cerr<<#a"="<<a<<endl)
#else
#define log(a) ((void)0)
#endif
#define rep(i, n) for(int i=0; i<(int)(n); i++)

const int tbl_n[3] = {2, 1, 0};
const int tbl_m[3][3] = { {0, 0, 0}, {0, 1, 1}, {0, 1, 2} };
const int tbl_p[3][3] = { {0, 1, 2}, {1, 1, 2}, {2, 2, 2} };

int v[3];
string s;
unsigned ix;

int expr();
int parse(int p, int q, int r) {
    v[0]=p, v[1]=q, v[2]=r;
    ix = 0;
    return expr();
}

int expr() {
    const char ch = s[ix++];
    if('0'<=ch && ch<='2') return ch-'0';
    else if('P'<=ch && ch<='R') return v[ch-'P'];
    else if(ch=='-') return tbl_n[expr()];
    else {
        const int l = expr();
        const char op = s[ix++];
        const int r = expr();
        ix++;
        if(op=='+') return tbl_p[l][r];
        if(op=='*') return tbl_m[l][r];
    }
    log(false);
}

int main() {
    for(;;) {
        getline(cin, s);
        log(s);
        if(s[0]=='.') return 0;
        int ans = 0;
        rep(p, 3) rep(q, 3) rep(r, 3) {
            if(parse(p, q, r)==2) ans++;
        }
        printf("%d\n", ans);
    }
}