#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int R;

int toDec(const string& s) {
	int base=1;
	int sign=1;
	int result =0;
	
	for (int i=s.length()-1; i>=0; i--) {
		result+=base*sign*(int)(s.at(i)-'0');
		base *= R;
		sign = -sign;
	}
	return result;
}
	
void toR(int x) {
	string s;
	char buf[30];
	int mod;
	int idx = 0; //oznacuje aktualny rad v R-sustave
	//cout << "*****" << endl;
	if (x >= 0){
	while (x != 0){
		mod = x%R;
		if ( idx % 2 != 0 ) {//zaporna
			x+=R;
			mod = R-mod;
		}
		//cout << mod << endl;
		buf[idx] = (char)mod+'0';
		//cout <<  buf[idx];
		x = x/R;
		idx++;
	}
	}
	else {
		x *=(-1);
		while (x != 0){
		mod = x%R;
		if ( (idx % 2 == 0)&&(idx!=0) ) {//zaporna
			x+=R;
			mod = R-mod;
		}
		//cout << mod << endl;
		buf[idx] = (char)mod+'0';
		//cout <<  buf[idx];
		x = x/R;
		idx++;

		}
		//buf[idx++] = '-';
	}
	//cout << "*****";
	for (int i = idx-1; i >= 0; i-- ){
		cout << buf[i];
	}
	cout << endl;
	
	
	
	
}


int main(char argc, char**argv) {
	
	string s;
	istringstream ss;
	
	
	
	//cin>>s;
	getline(cin,s);
	while(s!="end") {
		switch (s.at(0)) {
			case 't': //cout<<"to*"<<endl;
				s.erase(0, 3);			
				ss.str(s);
				ss >> R;
				
				int numb;
				ss >> numb;
				
				toR(numb);
				
				ss.clear();
				break;
			case 'f':// cout<<"from*"<<endl;
				s.erase(0, 5);
				ss.str(s);
			
				ss >> R;
			
				string num;
			
				ss >> num;
			
				cout<<toDec(num)<<endl;
				
				ss.clear();
				break;	
			}
		getline(cin,s);
	}
	
	return(0);
}
