Imports System.Collections.Generic
Imports System.Console
Module Program

    Sub Main()
        Dim StrInPut As String = StrConv(Console.ReadLine(), 1)
        If StrInPut.Length < 1 Or StrInPut.Length > 15 Then
            Exit Sub
        End If
        Dim ans As Integer = NumeralsRoman(StrInPut)
        If ans < 0 Or ans >= 4000 Then
            Exit Sub
        End If
        Console.WriteLine(ans)
    End Sub

    Function NumeralsRoman(Str As String) As Integer
        NumeralsRoman = 0
        Dim StrNumber() As String, StrRoma() As String, i As Integer
        Dim Strtmp As String = Str
        StrNumber = Split("900,400,90,40,9,4,1000,500,100,50,10,5,1", ",")
        StrRoma = Split("CM,CD,XC,XL,IX,IV,M,D,C,L,X,V,I", ",")
        For i = 0 To UBound(StrRoma)
            Strtmp = Replace(UCase(Strtmp), StrRoma(i), "")
            Str = Replace(UCase(Str), StrRoma(i), StrNumber(i) & "|")
        Next i
        If Strtmp <> "" Then
            Return -1
        End If
        StrRoma = Split(Str, "|")
        For i = 0 To UBound(StrRoma)
            NumeralsRoman = NumeralsRoman + Val(StrRoma(i))
        Next i
    End Function

End Module
