import java.util.Scanner; public class C { static Scanner in = new Scanner(System.in); static char[] str; static int pt; static int[] pqr = new int[3]; final static int[] MINUS = new int[] { 2, 1, 0 }; final static int[][] AND = new int[][] { { 0, 0, 0 }, { 0, 1, 1 }, { 0, 1, 2 } }; final static int[][] OR = new int[][] { { 0, 1, 2 }, { 1, 1, 2 }, { 2, 2, 2 } }; static int formula() { char c = next(); switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case 'P': return pqr[0]; case 'Q': return pqr[1]; case 'R': return pqr[2]; case '-': return MINUS[formula()]; case '(': { int a = formula(); char op = next(); int b = formula(); next(); if (op == '*') return AND[a][b]; else if (op == '+') return OR[a][b]; else throw new RuntimeException(); } } throw new RuntimeException(); } static char next() { if (pt == str.length) return 0; else return str[pt++]; } static char peek() { if (pt == str.length) return 0; else return str[pt]; } public static void main(String[] args) { while (true) { str = in.nextLine().toCharArray(); if (str[0] == '.') break; int ans = 0; for (int a = 0; a < 3; a++) for (int b = 0; b < 3; b++) for (int c = 0; c < 3; c++) { pqr[0] = a; pqr[1] = b; pqr[2] = c; pt = 0; if (formula() == 2) ans++; } System.out.println(ans); } } }