class Expression
  def initialize(x)
    @terms = [Term.new(x)]
    @eval = x
  end

  attr_reader :eval

  def mult(x)
    diff = @terms[-1].mult(x)
    @eval += diff
  end

  def add(x)
    @terms << Term.new(x)
    @eval += x
  end

  def shrinkable?
    @terms.size > 1 || @terms[0].shrinkable?
  end

  def shrink
    diff = @terms[0].shrink
    @eval += diff
    @terms.shift if @terms[0].empty?
  end

  def count
    if @terms.size == 1
      @terms[0].count
    else
      @terms[0].count_l * @terms[-1].count_r
    end
  end
end

class Term
  def initialize(x)
    @a = [[x, 1]]
    @eval = x
  end

  attr_reader :eval

  def mult(x)
    old = @eval
    if x == 1
      @a[-1][1] += 1
    else
      @a << [x, 1]
      @eval *= x
    end
    @eval - old
  end

  def shrinkable?
    @a.size > 1 || (@a[0][0] > 1 && @a[0][1] > 1)
  end

  def shrink
    old = @eval
    @eval /= @a[0][0]
    if @a[0][0] > 1 && @a[0][1] > 1
      @a[0][0] = 1
      @a[0][1] -= 1
    else
      @a.shift
    end
    @eval = 0 if empty?
    @eval - old
  end

  def count
    if @a.size == 1
      n = @a[0][1]
      if @a[0][0] == 1
        n * (n + 1) / 2
      else
        n
      end
    else
      count_l * count_r
    end
  end

  def count_l
    @a[0][0] == 1 ? @a[0][1] : 1
  end

  def count_r
    @a[-1][1]
  end

  def empty?
    @a.empty?
  end
end

def calc_noparen_exp(n, a)
  k = 0

  i = 0
  exp = Expression.new(a[i])

  check_mult1 = proc do
    while a[i + 1] == ?* && a[i + 2] == 1
      exp.mult(1)
      i += 2
    end
  end

  expand = proc do
    if a[i + 1] == ?+
      exp.add(a[i + 2])
    else
      exp.mult(a[i + 2])
    end
    i += 2
    check_mult1[]
  end

  check_mult1[]

  loop do
    expand[] while exp.eval < n && i < a.size - 1

    k += exp.count if exp.eval == n

    if exp.shrinkable?
      exp.shrink
    elsif i < a.size - 1
      expand[]
    else
      break
    end
  end

  k
end

def truncated_value(n, a)
  x = r = a[0]
  a[1..-1].each_slice(2) do |op, y|
    if op == ?+
      r = y
      x += r
    else
      old = r
      r *= y
      x += r - old
    end
    break if x > n
  end
  x > n ? n + 1 : x
end

loop do
  n = gets.to_i
  break if n == 0
  s = gets.chomp
  stack = []
  answer = 0
  s.each_char do |c|
    if c == ?)
      a = []
      a.unshift stack.pop until a[0] == ?(
      a.shift
      answer += calc_noparen_exp(n, a)
      stack << truncated_value(n, a)
    elsif c == ?( || c == ?+ || c == ?*
      stack << c
    else
      stack << c.to_i
    end
  end
  answer += calc_noparen_exp(n, stack)
  p answer
end
