#include<bits/stdc++.h>
using namespace std;
int main()
{
int i,j;
int t;
int n=0;
int r,R;
int len;
char a[100];
char s[100];
cin >> a;
cin >> r;
cin >> R;
len=strlen(a);//a转换为r进制的数n
for(i=0; a[i]!='\0'; i++)
{
switch(a[i])
{
case 'A':
t=10;
break;
case 'B':
t=11;
break;
case 'C':
t=12;
break;
case 'D':
t=13;
break;
case 'E':
t=14;
break;
case 'F':
t=15;
break;
default:
t=a[i]-'0';
break;
}
n=n+t*pow(r,len-1+-i);
}
cout << n << endl;
j=0;
i=0;
while(n)//n转换为R进制的数s
{
int x=n%R;
s[i++]=x<10?x+'0':x-10+'A';
n/=R;
}
for(j=i-1; j>=0; j--)
{
cout << s[j];
}
}