#include <map>
#include <iostream>

using namespace std;

/** Problem0039 : Roman Figure **/
int main()
{
	char num[110]; int now, old, ans;
	map<char, int> list;
	list.insert(map<char, int>::value_type('I', 1));
	list.insert(map<char, int>::value_type('V', 5));
	list.insert(map<char, int>::value_type('X', 10));
	list.insert(map<char, int>::value_type('L', 50));
	list.insert(map<char, int>::value_type('C', 100));
	list.insert(map<char, int>::value_type('D', 500));
	list.insert(map<char, int>::value_type('M', 1000));
	
	while (cin >> num) {
		ans = 0;
		old = list[num[0]];
		for (int i=1; i<110 && num[i]!='\0'; i++) {
			now = list[num[i]];
			if (old == 0) {
				old = now;
			} else if (old<now) {
				ans += (now-old);
				old = 0;
			} else {
				ans += old;
				old = now;
			}
		}
		ans += old;
		cout << ans << endl;
	}
	
	return 0;
}
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {
	for (;;) {
		int e; cin >> e;
		if (e == 0) break;
		int m = INT_MAX;
		for (int z = 0; z * z * z <= e; z++) {
			int _e = e - z * z * z;
			int y = sqrt((double)_e), x = _e - y * y;
			m = min(m, x + y + z);
		}
		cout << m << endl;
	}
}