#include <bits/stdc++.h>
int F(int p, int q, int r, char ft[100], int fc) {
  int fit[100], ic, x, t;
  ic = 1;
  for (t = 0; t < 100; t++) {
    fit[t] = 0;
  }
  while (ic < fc) {
    if (ft[ic] == 'P') {
      fit[ic] = p;
    }
    if (ft[ic] == 'Q') {
      fit[ic] = q;
    }
    if (ft[ic] == 'R') {
      fit[ic] = r;
    }
    if (ft[ic] == '0') {
      fit[ic] = 0;
    }
    if (ft[ic] == '1') {
      fit[ic] = 1;
    }
    if (ft[ic] == '2') {
      fit[ic] = 2;
    }
    if (ft[ic] == '+') {
      fit[ic] = 3;
    }
    if (ft[ic] == '-') {
      fit[ic] = 4;
    }
    if (ft[ic] == '*') {
      fit[ic] = 5;
    }
    ic++;
  }
  ic--;
  x = 1;
  while (x < ic) {
    if (fit[x] == 3) {
      if (fit[x - 1] == 0 && fit[x - 2] == 0) {
        fit[x - 2] = 0;
      } else if (fit[x - 1] == 2 || fit[x - 2] == 2) {
        fit[x - 2] = 2;
      } else {
        fit[x - 2] = 1;
      }
      ic -= 2;
      for (t = x - 1; t <= ic; t++) {
        fit[t] = fit[t + 2];
      }
      x -= 2;
    }
    if (fit[x] == 4) {
      fit[x - 1] = 2 - fit[x - 1];
      ic--;
      for (t = x; t <= ic; t++) {
        fit[t] = fit[t + 1];
      }
      x--;
    }
    if (fit[x] == 5) {
      if (fit[x - 1] == 0 || fit[x - 2] == 0) {
        fit[x - 2] = 0;
      } else if (fit[x - 1] == 2 && fit[x - 2] == 2) {
        fit[x - 2] = 2;
      } else {
        fit[x - 2] = 1;
      }
      ic -= 2;
      for (t = x - 1; t <= ic; t++) {
        fit[t] = fit[t + 2];
      }
      x -= 2;
    }
    x++;
  }
  return fit[1];
}
int main(void) {
  int c, nc, oc, fc;
  char txt[100], ng[100], op[100], ft[100];
  while (1) {
    scanf("%s", &txt);
    if (txt[0] == '.') {
      break;
    }
    nc = oc = fc = 0;
    for (c = 0; c < strlen(txt); c++) {
      if (txt[c] == 0 || txt[c] == 1 || txt[c] == 2 || txt[c] == 'P' ||
          txt[c] == 'Q' || txt[c] == 'R') {
        fc++;
        ft[fc] = txt[c];
        if (nc != 0) {
          while (nc != 0) {
            fc++;
            ft[fc] = ng[nc];
            nc--;
          }
        }
      }
      if (txt[c] == '+') {
        if (oc == 0 || op[oc] == '(') {
          oc++;
          op[oc] = txt[c];
        } else {
          while (oc != 0) {
            fc++;
            ft[fc] = op[oc];
            oc--;
          }
          oc++;
          op[oc] = txt[c];
        }
      }
      if (txt[c] == '*') {
        if (oc == 0 || op[oc] == '+' || op[oc] == '(') {
          oc++;
          op[oc] = txt[c];
        } else {
          while (oc != 0) {
            fc++;
            ft[fc] = op[oc];
            oc--;
          }
          oc++;
          op[oc] = txt[c];
        }
      }
      if (txt[c] == '(') {
        oc++;
        op[oc] = txt[c];
      }
      if (txt[c] == ')') {
        while (op[oc] != '(') {
          fc++;
          ft[fc] = op[oc];
          oc--;
        }
        op[oc] = 0;
        oc--;
      }
      if (txt[c] == '-') {
        nc++;
        ng[nc] = txt[c];
      }
    }
    if (oc != 0) {
      while (oc != 0) {
        fc++;
        ft[fc] = op[oc];
        oc--;
      }
    }
    for (c = 1; c <= fc; c++) {
      printf("%c", ft[c]);
    }
    printf("\n");
    for (c = 0; c < 100; c++) {
      ng[c] = op[c] = 0;
    }
    int p, q, r, ans;
    ans = 0;
    for (p = 0; p < 3; p++) {
      for (q = 0; q < 3; q++) {
        for (r = 0; r < 3; r++) {
          if (F(p, q, r, ft, fc) == 2) {
            ans++;
          }
        }
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
