### constant

UOPS = {'-' => [2, 1, 0]}

BOPS = {
  '*' => [[0, 0, 0], [0, 1, 1], [0, 1, 2]],
  '+' => [[0, 1, 2], [1, 1, 2], [2, 2, 2]]
}

VARNAME = 'PQR'

### subroutines
#<fml> := '0'-'2' | '-'<fml> | '('<fml>'*'<fml>')' | '('<fml>'+'<fml>')'

def formula(fml)
  chpos = fml[$pos]
  $pos += 1
  val = nil

  case chpos
  when '0'..'2'
    val = chpos.to_i
  when '-'
    v0 = formula(fml)
    val = UOPS[chpos][v0]
  else
    v0 = formula(fml)
    op = fml[$pos]
    $pos += 1
    v1 = formula(fml)
    $pos += 1
    #p [v0, op, v1]
    val = BOPS[op][v0][v1]
  end

  val
end

### main

loop do
  fml = gets.strip
  break if fml == '.'

  count = 0
  vvals = [0, 0, 0]

  loop do
    fml0 = fml.clone.tr(VARNAME, vvals.join(''))
    $pos = 0
    val = formula(fml0)
    count += 1 if val == 2

    k = 0
    while k < 3
      vvals[k] += 1
      break if vvals[k] <= 2
      vvals[k] = 0
      k += 1
    end
    break if k >= 3
  end

  puts count
end