#include<iostream>
#include<vector>
using namespace std;

int main() {
	long long m, n;
	string str;
	while (cin >> m >> n) {
		cin >> str;
		long long input = 0;
		long long temp = 1;
		for (int i = str.size()-1; i >=0 ; i--) {
			if (str[i] >= '0' && str[i] <= '9') {
				input = input + (str[i] - '0') * temp;
				temp = temp * m;
			}
			else {
				input = input + (str[i] -'A'+10) * temp;
				temp = temp * m;
			}
			

		}
		vector<long long> vec;
		while (input != 0) {
			vec.push_back(input % n);
			input = input / n;


		}
		for (int i = vec.size() - 1; i >= 0; i--) {
			cout << vec[i];
		}
		cout << endl;

	}




}