class Bit
  def initialize(b)
    @b = b
  end
  attr_reader :b
  def -@
    @b == 0 ? I : O
  end
  def *(other)
    self.b * other.b == 0 ? O : I
  end
  def ^(other)
    self.b ^ other.b == 0 ? O : I
  end
  def to_s
    @b.to_s
  end
end

O = Bit.new(0)
I = Bit.new(1)

class String
  def output
    ((0b0000..0b1111).map {|b|
      (eval tr('abcd', sprintf("%04b", b)).tr('10', 'IO')).to_s
    } * "").to_i(2)
  end

  def terminated?
    self !~ /e/
  end
end

queue = ['e']
min = Array.new(2**16, 16)
min[0x0000] = 1
min[0xffff] = 1

until queue.empty?
  e = queue.shift
  if e.terminated?
    out = e.output
    min[out] = e.size if e.size < min[out]
  else
    replaced = [
      e.sub(?e, ?a),
      e.sub(?e, ?b),
      e.sub(?e, ?c),
      e.sub(?e, ?d),
      e.sub(?e, "-e"),
      e.sub(?e, "(e*e)"),
      e.sub(?e, "(e^e)"),
    ]
    replaced.reject! {|r|
       r =~ /--/ or
       r =~ /a[*^]-?a/ or
       r =~ /b[*^]-?[ab]/ or
       r =~ /c[*^]-?[abc]/ or
       r =~ /d[*^]-?[abcd]/ or
       r =~ /\)[*^]-?[abcd]/
    }
    replaced.each do |r|
      if r.size < 16
        queue << r
      end
    end
  end
end

loop do
  s = gets.chomp
  break if s == ?.
  puts min[s.output]
end

