#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <array>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <stack>
#include <queue>
#include <bitset>
#include <tuple>
#include <cmath>
#include <complex>
#include <algorithm>
#include <utility>
#include <regex>
#include <cstdint>
#include <numeric>
#include <functional>
#include <cassert>

using namespace std;
using ll = int64_t;
using ull = uint64_t;

constexpr ll INF = 1000000000;/* 1e+9a */
namespace utils{

    class Repeater{
        int st;
        int ed;
        int d;
    public:
        class rep_iterator{
            int i; int d;
        public:
            rep_iterator(int x, int pd) :i(x), d(pd) {}

            int& operator*(){ return i; }
            bool operator==(const rep_iterator& k) const { return i == k.i; }
            bool operator!=(const rep_iterator& k) const { return i != k.i; }
            rep_iterator& operator++(){ i+=d; return *this; }
        };

        explicit Repeater(int N) :st(0), ed(N), d(1) {}
        Repeater(int b, int e) :st(b), ed(e), d(1) {if(st > ed){ st--; ed--; d = -1; } }

        rep_iterator begin() const{ return rep_iterator(st, d); }
        rep_iterator end() const{ return rep_iterator(ed, d); }

    };

    template<class T, class Compare> using p_queue = priority_queue<T, vector<T>, Compare>;
    template<class T> using min_queue = p_queue<T, greater<T>>;
    template<class T> using max_queue = p_queue<T, less<T>>;

    template<class T> bool min_update(T& X, const T& A){ if(X > A) {X = A; return true;} return false; }
    template<class T> bool max_update(T& X, const T& A){ if(X < A) {X = A; return true;} return false; }
#define ALL(x) begin(x), end(x)
#define rALL(x) rbegin(x), rend(x)
}
using namespace utils;

int not3(int x){
    return x == 0 ? 2
                  : x == 1 ? 1
                           : 0;
}

int and3(int x, int y){
    return min(x, y);
}

int or3(int x, int y){
    return max(x, y);
}

pair<int, int> eval(const string& F, int k, int P, int Q, int R){
    switch (F[k]) {
        case '0' : return {0, k+1};
        case '1' : return {1, k+1};
        case '2' : return {2, k+1};
        case 'P' : return {P, k+1};
        case 'Q' : return {Q, k+1};
        case 'R' : return {R, k+1};
        case '-' : {
            auto X = eval(F, k + 1, P, Q, R);
            return {not3(X.first), X.second};
        }
        case '(' :
            auto X = eval(F, k+1, P, Q, R);
            int x = X.first;
            char op = F[X.second];
            auto Y = eval(F, X.second+1, P, Q, R);
            int end = Y.second;
            assert(F[end] == ')');
            int val = op == '+' ? or3(x, Y.first) : and3(x, Y.first);
            return {val, end+1};
    }
    assert(0);
}


void sub_main(istream &is) {
    string S;
    is >> S;
    if(!is or S[0] == '.') return;
    int cnt = 0;
    for (int P = 0; P < 3; ++P) {
        for (int Q = 0; Q < 3; ++Q) {
            for (int R = 0; R < 3; ++R) {
                auto ev = eval(S, 0, P, Q, R);
                assert(ev.second == S.size());
                cnt += ev.first == 2 ? 1 : 0;
            } // end R
        } // end Q
    } // end P
    cout << cnt << endl;
}

int main(int argc, char *argv[]) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << setprecision(16) << scientific;
#ifdef DEBUG
    string test_cases = "test_B.txt";
    cerr << "DEBUG MODE" <<  endl;
    cerr << test_cases << " -->" << endl;
    auto fs = fstream(test_cases, fstream::in);
    int lp = 0;
    while(fs) {
        lp++;
        cout << lp <<  "#------\n";
        sub_main(fs);
    }
    cout << "------#" << endl;
    if(lp <= 1) sub_main(cin);
#else
    while(cin) sub_main(cin);
#endif
    return 0;
}



Measured memory_bytes: 3108