#include <iostream>
#include <algorithm>
using namespace std;


#ifndef HELLO_HPP_
#define HELLO_HPP_

// This file was automatically generated by Caper.
// (http://jonigata.github.io/caper/caper.html)

#include <cstdlib>
#include <cassert>
#include <vector>

namespace calc {

enum Token {
    token_eof,
    token_Add,
    token_Cons,
    token_LBranket,
    token_Mul,
    token_Not,
    token_RBranket,
    token_Var,
};

inline const char* token_label(Token t) {
    static const char* labels[] = {
        "token_eof",
        "token_Add",
        "token_Cons",
        "token_LBranket",
        "token_Mul",
        "token_Not",
        "token_RBranket",
        "token_Var",
    };
    return labels[t];
}

template <class T, unsigned int StackSize>
class Stack {
public:
    Stack() { gap_ = 0; }

    void rollback_tmp() {
        gap_ = stack_.size();
        tmp_.clear();
    }

    void commit_tmp() {
        // may throw
        stack_.reserve(gap_ + tmp_.size());
       
        // expect not to throw
        stack_.erase(stack_.begin()+ gap_, stack_.end());
        stack_.insert(stack_.end(), tmp_.begin(), tmp_.end());
        tmp_.clear();
    }
    bool push(const T& f) {
        if (StackSize != 0 &&
            int(StackSize) <= int(stack_.size() + tmp_.size())) {
            return false;
        }
        tmp_.push_back(f);
        return true;
    }
       
    void pop(size_t n) {
        if (tmp_.size() < n) {
            n -= tmp_.size();
            tmp_.clear();
            gap_ -= n;
        } else {
            tmp_.erase(tmp_.end() - n, tmp_.end());
        }
    }

    T& top() {
        assert(0 < depth());
        if (!tmp_.empty()) {
            return tmp_.back();
        } else {
            return stack_[gap_ - 1];
        }
    }
       
    const T& get_arg(size_t base, size_t index) {
        size_t n = tmp_.size();
        if (base - index <= n) {
            return tmp_[n - (base - index)];
        } else {
            return stack_[gap_ - (base - n) + index];
        }
    }
       
    void clear() {
        stack_.clear();
        tmp_.clear();
        gap_ = 0; 
    }
       
    bool empty() const {
        if (!tmp_.empty()) {
            return false;
        } else {
            return gap_ == 0;
        }
    }
       
    size_t depth() const {
        return gap_ + tmp_.size();
    }
       
    T& nth(size_t index) {
        if (gap_ <= index) {
            return tmp_[index - gap_];
        } else {
            return stack_[index];
        }
    }

    void swap_top_and_second() {
        int d = depth();
        assert(2 <= d);
        T x = nth(d - 1);
        nth(d - 1) = nth(d - 2);
        nth(d - 2) = x;
    }

private:
    std::vector<T> stack_;
    std::vector<T> tmp_;
    size_t gap_;
       
};

template <class _Value, class _SemanticAction,
          unsigned int _StackSize = 0>
class Parser {
public:
    typedef Token token_type;
    typedef _Value value_type;

    enum Nonterminal {
        Nonterminal_Expr,
    };

public:
    Parser(_SemanticAction& sa) : sa_(sa) { reset(); }

    void reset() {
        error_ = false;
        accepted_ = false;
        clear_stack();
        rollback_tmp_stack();
        if (push_stack(0, value_type())) {
            commit_tmp_stack();
        } else {
            sa_.stack_overflow();
            error_ = true;
        }
    }

    bool post(token_type token, const value_type& value) {
        rollback_tmp_stack();
        error_ = false;
        while ((this->*(stack_top()->entry->state))(token, value))
            ; // may throw
        if (!error_) {
            commit_tmp_stack();
        } else {
            recover(token, value);
        }
        return accepted_ || error_;
    }

    bool accept(value_type& v) {
        assert(accepted_);
        if (error_) { return false; }
        v = accepted_value_;
        return true;
    }

    bool error() { return error_; }

private:
    typedef Parser<_Value, _SemanticAction, _StackSize> self_type;

    typedef bool (self_type::*state_type)(token_type, const value_type&);
    typedef int (self_type::*gotof_type)(Nonterminal);

    bool            accepted_;
    bool            error_;
    value_type      accepted_value_;
    _SemanticAction& sa_;

    struct table_entry {
        state_type  state;
        gotof_type  gotof;
        bool        handle_error;
    };

    struct stack_frame {
        const table_entry*  entry;
        value_type          value;
        int                 sequence_length;

        stack_frame(const table_entry* e, const value_type& v, int sl)
            : entry(e), value(v), sequence_length(sl) {}
    };

    Stack<stack_frame, _StackSize> stack_;

    bool push_stack(int state_index, const value_type& v, int sl = 0) {
        bool f = stack_.push(stack_frame(entry(state_index), v, sl));
        assert(!error_);
        if (!f) { 
            error_ = true;
            sa_.stack_overflow();
        }
        return f;
    }

    void pop_stack(size_t n) {
        stack_.pop(n);
    }

    stack_frame* stack_top() {
        return &stack_.top();
    }

    const value_type& get_arg(size_t base, size_t index) {
        return stack_.get_arg(base, index).value;
    }

    void clear_stack() {
        stack_.clear();
    }

    void rollback_tmp_stack() {
        stack_.rollback_tmp();
    }

    void commit_tmp_stack() {
        stack_.commit_tmp();
    }

    void recover(token_type, const value_type&) {
    }

    bool call_nothing(Nonterminal nonterminal, int base) {
        pop_stack(base);
        int dest_index = (this->*(stack_top()->entry->gotof))(nonterminal);
        return push_stack(dest_index, value_type());
    }

    bool call_0_Identity(Nonterminal nonterminal, int base, int arg_index0) {
        int arg0; sa_.downcast(arg0, get_arg(base, arg_index0));
        int r = sa_.Identity(arg0);
        value_type v; sa_.upcast(v, r);
        pop_stack(base);
        int dest_index = (this->*(stack_top()->entry->gotof))(nonterminal);
        return push_stack(dest_index, v);
    }

    bool call_0_MakeAdd(Nonterminal nonterminal, int base, int arg_index0, int arg_index1) {
        int arg0; sa_.downcast(arg0, get_arg(base, arg_index0));
        int arg1; sa_.downcast(arg1, get_arg(base, arg_index1));
        int r = sa_.MakeAdd(arg0, arg1);
        value_type v; sa_.upcast(v, r);
        pop_stack(base);
        int dest_index = (this->*(stack_top()->entry->gotof))(nonterminal);
        return push_stack(dest_index, v);
    }

    bool call_0_MakeMul(Nonterminal nonterminal, int base, int arg_index0, int arg_index1) {
        int arg0; sa_.downcast(arg0, get_arg(base, arg_index0));
        int arg1; sa_.downcast(arg1, get_arg(base, arg_index1));
        int r = sa_.MakeMul(arg0, arg1);
        value_type v; sa_.upcast(v, r);
        pop_stack(base);
        int dest_index = (this->*(stack_top()->entry->gotof))(nonterminal);
        return push_stack(dest_index, v);
    }

    bool call_0_MakeNot(Nonterminal nonterminal, int base, int arg_index0) {
        int arg0; sa_.downcast(arg0, get_arg(base, arg_index0));
        int r = sa_.MakeNot(arg0);
        value_type v; sa_.upcast(v, r);
        pop_stack(base);
        int dest_index = (this->*(stack_top()->entry->gotof))(nonterminal);
        return push_stack(dest_index, v);
    }

    bool state_0(token_type token, const value_type& value) {
        switch(token) {
        case token_Cons:
            // shift
            push_stack(/*state*/ 6, value);
            return false;
        case token_LBranket:
            // shift
            push_stack(/*state*/ 3, value);
            return false;
        case token_Not:
            // shift
            push_stack(/*state*/ 2, value);
            return false;
        case token_Var:
            // shift
            push_stack(/*state*/ 7, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_0(Nonterminal nonterminal) {
        switch(nonterminal) {
        case Nonterminal_Expr: return 1;
        default: assert(0); return false;
        }
    }

    bool state_1(token_type token, const value_type& value) {
        switch(token) {
        case token_eof:
            // accept
            accepted_ = true;
            accepted_value_ = get_arg(1, 0);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_1(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_2(token_type token, const value_type& value) {
        switch(token) {
        case token_Cons:
            // shift
            push_stack(/*state*/ 6, value);
            return false;
        case token_LBranket:
            // shift
            push_stack(/*state*/ 3, value);
            return false;
        case token_Not:
            // shift
            push_stack(/*state*/ 2, value);
            return false;
        case token_Var:
            // shift
            push_stack(/*state*/ 7, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_2(Nonterminal nonterminal) {
        switch(nonterminal) {
        case Nonterminal_Expr: return 8;
        default: assert(0); return false;
        }
    }

    bool state_3(token_type token, const value_type& value) {
        switch(token) {
        case token_Cons:
            // shift
            push_stack(/*state*/ 6, value);
            return false;
        case token_LBranket:
            // shift
            push_stack(/*state*/ 3, value);
            return false;
        case token_Not:
            // shift
            push_stack(/*state*/ 2, value);
            return false;
        case token_Var:
            // shift
            push_stack(/*state*/ 7, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_3(Nonterminal nonterminal) {
        switch(nonterminal) {
        case Nonterminal_Expr: return 9;
        default: assert(0); return false;
        }
    }

    bool state_4(token_type token, const value_type& value) {
        switch(token) {
        case token_Cons:
            // shift
            push_stack(/*state*/ 6, value);
            return false;
        case token_LBranket:
            // shift
            push_stack(/*state*/ 3, value);
            return false;
        case token_Not:
            // shift
            push_stack(/*state*/ 2, value);
            return false;
        case token_Var:
            // shift
            push_stack(/*state*/ 7, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_4(Nonterminal nonterminal) {
        switch(nonterminal) {
        case Nonterminal_Expr: return 10;
        default: assert(0); return false;
        }
    }

    bool state_5(token_type token, const value_type& value) {
        switch(token) {
        case token_Cons:
            // shift
            push_stack(/*state*/ 6, value);
            return false;
        case token_LBranket:
            // shift
            push_stack(/*state*/ 3, value);
            return false;
        case token_Not:
            // shift
            push_stack(/*state*/ 2, value);
            return false;
        case token_Var:
            // shift
            push_stack(/*state*/ 7, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_5(Nonterminal nonterminal) {
        switch(nonterminal) {
        case Nonterminal_Expr: return 12;
        default: assert(0); return false;
        }
    }

    bool state_6(token_type token, const value_type& value) {
        switch(token) {
        case token_eof:
        case token_Add:
        case token_Mul:
        case token_RBranket:
            // reduce
            return call_0_Identity(Nonterminal_Expr, /*pop*/ 1, 0);
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_6(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_7(token_type token, const value_type& value) {
        switch(token) {
        case token_eof:
        case token_Add:
        case token_Mul:
        case token_RBranket:
            // reduce
            return call_0_Identity(Nonterminal_Expr, /*pop*/ 1, 0);
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_7(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_8(token_type token, const value_type& value) {
        switch(token) {
        case token_eof:
        case token_Add:
        case token_Mul:
        case token_RBranket:
            // reduce
            return call_0_MakeNot(Nonterminal_Expr, /*pop*/ 2, 1);
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_8(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_9(token_type token, const value_type& value) {
        switch(token) {
        case token_Add:
            // shift
            push_stack(/*state*/ 4, value);
            return false;
        case token_Mul:
            // shift
            push_stack(/*state*/ 5, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_9(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_10(token_type token, const value_type& value) {
        switch(token) {
        case token_RBranket:
            // shift
            push_stack(/*state*/ 11, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_10(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_11(token_type token, const value_type& value) {
        switch(token) {
        case token_eof:
        case token_Add:
        case token_Mul:
        case token_RBranket:
            // reduce
            return call_0_MakeAdd(Nonterminal_Expr, /*pop*/ 5, 1, 3);
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_11(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_12(token_type token, const value_type& value) {
        switch(token) {
        case token_RBranket:
            // shift
            push_stack(/*state*/ 13, value);
            return false;
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_12(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    bool state_13(token_type token, const value_type& value) {
        switch(token) {
        case token_eof:
        case token_Add:
        case token_Mul:
        case token_RBranket:
            // reduce
            return call_0_MakeMul(Nonterminal_Expr, /*pop*/ 5, 1, 3);
        default:
            sa_.syntax_error();
            error_ = true;
            return false;
        }
    }

    int gotof_13(Nonterminal nonterminal) {
        assert(0);
        return true;
    }

    const table_entry* entry(int n) const {
        static const table_entry entries[] = {
            { &Parser::state_0, &Parser::gotof_0, false },
            { &Parser::state_1, &Parser::gotof_1, false },
            { &Parser::state_2, &Parser::gotof_2, false },
            { &Parser::state_3, &Parser::gotof_3, false },
            { &Parser::state_4, &Parser::gotof_4, false },
            { &Parser::state_5, &Parser::gotof_5, false },
            { &Parser::state_6, &Parser::gotof_6, false },
            { &Parser::state_7, &Parser::gotof_7, false },
            { &Parser::state_8, &Parser::gotof_8, false },
            { &Parser::state_9, &Parser::gotof_9, false },
            { &Parser::state_10, &Parser::gotof_10, false },
            { &Parser::state_11, &Parser::gotof_11, false },
            { &Parser::state_12, &Parser::gotof_12, false },
            { &Parser::state_13, &Parser::gotof_13, false },
        };
        return &entries[n];
    }

};

} // namespace calc

#endif // #ifndef HELLO_HPP_



class unexpected_char : public std::exception {};

int var[3];

class scanner {
public:
    using char_type = char;

private:
    string s;
    int pos;
public:
    scanner(string s) : s(s), pos(0) {}

    calc::Token get(int& v) {
        char c;
        do {
            c = getc();
        } while (isspace(c));

        if ('0' <= c and c <= '2') {
            v = c - '0';
            return calc::token_Cons;
        }
        if ('P' <= c and c <= 'R') {
            v = var[c - 'P'];
            return calc::token_Var;
        }
        switch (c) {
        case '+': return calc::token_Add;
//        case '-': return calc::token_Sub;
        case '*': return calc::token_Mul;
//        case '/': return calc::token_Div;
        case '(': return calc::token_LBranket;
        case ')': return calc::token_RBranket;
        case '-': return calc::token_Not;
        case EOF: return calc::token_eof;
        }

        // ??´??°
        // if(isdigit(c)) {
        //     v = c - '0';
        //     return calc::token_Digit;
        // }
        cerr << c << endl;
        throw unexpected_char();
    }

private:
    char_type getc() {
        if (pos == s.size()) return EOF;
        return s[pos++];
    }
};

struct SemanticAction {
    void syntax_error(){}
    void stack_overflow(){}
    void downcast( int& x, int y ) { x = y; }
    void upcast( int& x, int y ) { x = y; }

    int Identity( int n ) { return n; }
    int MakeMul(int x, int y) {
        return min(x, y);
    }
    int MakeAdd(int x, int y) {
        return max(x, y);
    }
    int MakeNot(int x) {
        return 2-x;
    }
};

int main() {
    while (true) {
        string s;
        getline(cin, s);
        if (s == ".") break;
        int ans = 0;
        for (int x = 0; x < 3; x++) {
            for (int y = 0; y < 3; y++) {
                for (int z = 0; z < 3; z++) {
                    var[0] = x; var[1] = y; var[2] = z;
                    scanner scn(s);
                    SemanticAction sa;
                    calc::Parser<int, SemanticAction> parser(sa);
                    calc::Token token;
                    while (true) {
                        int v;
                        token = scn.get(v);
                        if (parser.post(token, v)) { break; }
                    }
                    int v;
                    if (parser.accept(v)) {
                        if (v == 2) ans++;
                    }
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 3208