#include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
using namespace std;

int main(){
	char s[105];

	while(scanf("%s",s)!=EOF){
		int len,n[105]={},ans=0;
		len=strlen(s);
		rep(i,0,len){
			if(s[i]=='I') n[i]=1;
			else if(s[i]=='V') n[i]=5;
			else if(s[i]=='X') n[i]=10;
			else if(s[i]=='L') n[i]=50;
			else if(s[i]=='C') n[i]=100;
			else if(s[i]=='D') n[i]=500;
			else if(s[i]=='M') n[i]=1000;
		}
		rep(i,0,len-1){
			if(n[i]<n[i+1]) ans-=n[i];
			else ans+=n[i];
		}
		ans+=n[len-1];
		cout<<ans<<endl;
	}
}
#include <iostream>
#include <cmath>

using namespace std;

int main() {
	int hyou[102];
	for (int i = 0; i < 102; i++) {
		hyou[i] = i*i*i;
	}
	int e;
	while (cin >> e) {
		if (e == 0) {
			break;
		}
		int a, b, c;
		for (int i = 1; ; i++) {
			if (hyou[i] > e) {
				c = i-1;
				break;
			}
		}
		for (int i = 1; ;i++) {
			if (i*i > e-hyou[c]) {
				b = i-1;
				break;
			}
		}
		for (int i = 1; ;i++) {
			if (i > e-hyou[c]-b*b) {
				a = i-1;
				break;
			}
		}
		int MIN = a+b+c;
		for (int i = c-1; i >= 0; i--) {
			for (int j = b+1; ; j++) {
				if (j*j > e-hyou[i]) {
					b = j-1;
					break;
				}
			}
			int q = e-hyou[i]-b*b;
			MIN = min(MIN, b+q+i);
		}
		cout << MIN << endl;
	}
	return 0;
}