table = {I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000, nil => 0}

count = ->(roman, number = 0) {
  return number if roman.empty?
  a, b = table[roman.first], table[roman[1]]
  number += if a >= b
    r = roman.drop(1)
    a
  else
    r = roman.drop(2)
    b - a
  end
  count.(r, number)
}

$<.readlines.map(&:chomp).each {|roman| puts count.(roman.chars.map(&:to_sym))}
