#include <iostream>
#include <vector>
#include <string>

using Int = long long int;
using String = std::string;
template <class T>
using Vector = std::vector<T>;

struct {
    Vector<Int> var;
    String s;
    Int i;

    Int expr() {
        Int ret;
        if (s[i] == '(') {
            ++i;
            Int a = expr();
            char c = s[i];
            ++i;
            Int b = expr();
            ++i;

            if (c == '*') {
                ret = std::min(a, b);
            } else if (c == '+') {
                ret = std::max(a, b);
            }
        } else if (s[i] == '-') {
            ++i;
            Int a = expr();
            ret = 2 - a;
        } else if (isdigit(s[i])) {
            ret = s[i] - '0';
            ++i;
        } else if ('P' <= s[i] && s[i] <= 'R') {
            ret = var[s[i] - 'P'];
            ++i;
        }
        return ret;
    }

    Int operator()() {
        i = 0;
        return expr();
    }
} parser;

bool solve() {
    std::cin >> parser.s;
    if (parser.s == ".") return false;

    Int ans = 0;
    Vector<Int> v(3);
    for (v[0] = 0; v[0] <= 2; ++v[0]) {
        for (v[1] = 0; v[1] <= 2; ++v[1]) {
            for (v[2] = 0; v[2] <= 2; ++v[2]) {
                parser.var = v;
                if (parser() == 2) ++ans;
            }
        }
    }

    std::cout << ans << std::endl;
    return true;
}

int main() {
    while (solve()) {}
    return 0;
}

