#include <iostream>
#include <cstdio>
#include <set>
#include <map>
#include <vector>
#include <utility>
#include <algorithm>
#include <complex>
#include <queue>
#include <string>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef pair<int, int> PI;
#define REP(i,n) for (int i=0; i<int(n); i++)
#define FOR(i,a,n) for (int i=a; i<int(n); i++)
#define FORD(i,n) for (int i=int(n)-1; i>=0; i--)
#define FOREACH(it,a) for (__typeof(a.begin()) it=a.begin(); it!=a.end(); it++)
#define ll long long

string zaporne(int base, ll x);
string kladne(int base,ll x) {
    string ret="";
    if (x==0) return "";
    if (abs(x)<base) {
        if (x>0) {
            return ret+char('0'+x);
        } else {
            return ret+"1"+char('0'+base+x);
        }
    } else {
        ll zv = abs(x)%base;
        ret+=('0'+zv);
        x-=zv;
        return zaporne(base,- (x/base))+ret;    
    }    
}

string zaporne(int base, ll x) {
    string ret="";
    if (x==0) return "";
    if (abs(x)<base) {
        if (x>0) {
            return ret+char('0'+abs(x));
        } else {
            return ret+"1"+char('0'+base-abs(x));
        }
    } else {
        ll zv=abs(x)%base;
        x-=zv;
        ret+=('0'+zv);
        return kladne(base,- (x/base))+ret;    
    }    
}

ll from(ll base,ll nasob,ll x) {        
    if (x==0) return 0;
    return (x%10)*nasob+from(base,-nasob*base,x/10);
}


int main() {
    char oper[15];
    string op;
    int base;
    ll cis;
    
    while (scanf("%[^-]-%d %lld\n",oper,&base,&cis)==3) {
//        cout<<": "<<oper<<" "<<base<<" "<<cis<<endl;
        op=oper;        
        if (op=="to") {
            if (cis==0) printf("0\n");
            else printf("%s\n",kladne(base,cis).c_str());
        } else if (op=="from") {
            if (cis==0) printf("0\n");
            else printf("%lld\n",from(base,1,cis));
        }
    }


} 
