#include <stdio.h>
#include <string.h>

int bind[128];
char *i;

int rdp(void)
{
    int a, b;
    char c, op;

    switch (c = *i++) {
        case '0':
        case '1':
        case '2':
            return c - '0';
        case 'P':
        case 'Q':
        case 'R':
            return bind[c];
        case '-':
            return -(rdp() - 1) + 1;
        case '(':
            a = rdp();
            switch (op = *i++) {
                case '*':
                    a = (a * rdp() + 1) / 2;
                    break;
                case '+':
                    b = rdp();
                    a = (a == 1 && b == 1) ? 1 : (a + b >= 2 ? 2 : a + b);
                    break;
            }
            ++i;    // skip ')'
            return a;
    }
    return a;
}

int main(void)
{
    for (;;) {
        char s[81];
        int p, q, r, c = 0;

        scanf("%s", s);
        if (strcmp(s, ".") == 0)
            break;

        for (p = 0; p < 3; ++p) {
            bind['P'] = p;
            for (q = 0; q < 3; ++q) {
                bind['Q'] = q;
                for (r = 0; r < 3; ++r) {
                    bind['R'] = r;
                    i = s;
                    if (rdp() == 2)
                        ++c;
                }
            }
        }

        printf("%d\n", c);
    }

    return 0;
}