#include <vector>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
char int_tochar(int num)
{
char res = 0;
if (num >= 10)
res = num - 10 + 'A';
else res = num + '0';
return res;
}
int char_toint(char c)
{
int res = 0;
if (c >= '0' && c <= '9')
res = c - '0';
if (c >= 'A' && c <= 'F')
res = c - 'A'+10;
if (c >= 'a' && c <= 'f')
res = c - 'a'+10;
return res;
}
void trans(int m, int n, string& s)
{
string res = "";
for (int i = 0; i < s.length();)
{
int yushu = 0;
int t;
for (int j = i; j < s.length(); j++)
{
t = yushu;
yushu = (yushu * m + char_toint(s[j])) % n;
int tt = (t* m + char_toint(s[j])) / n;
s[j] = int_tochar(tt);
}
res += int_tochar(yushu);
while (s[i] == '0')
i++;
}
s = res;
}
int main() {
int a, b;
string s;
while (cin >> a)
{
cin >> s;
cin >> b;
trans(a, b, s);
reverse(s.begin(), s.end());
cout << s << endl;
}
/*cout<<int_tochar(10);
cout << char_toint('a');*/
}
// 64 位输出请用 printf("%lld")