let f = function
    'I' -> 1
    | 'V' -> 5
    | 'X' -> 10
    | 'L' -> 50
    | 'C' -> 100
    | 'D' -> 500
    | 'M' -> 1000
    | _ -> 0
;;

let get_num s =
    let len = String.length s in
    let rec loop sum prev = function
        i when len = i -> sum
      | i -> let n = f s.[i] in
            if n > prev then
              loop (sum + (f s.[i]) - (2*prev)) n (i+1)
            else
                loop (sum + (f s.[i])) n (i+1)
    in loop 0 0 0
;;

let () =
    let rec read () =
        try let line = read_line () |> String.trim in
            get_num line |> print_int
            ;print_newline ()
            ;read ()
        with End_of_file -> ()
    in read ()
;;