#include <iostream>
#include <cstring>
using namespace std;
int main() {
char s[25], s2[25];
int b1, b2, n1, n2, v;
ifstream f("baze1.in");
ofstream g("baze1.out");
f >> b1 >> b2 >> s >> s2;
for (int i = 0; i < strlen(s); i++) {
v = s[i] - '0';
n1 = n1 * b1 + v;
}
for (int i = 0; i < strlen(s2); i++) {
v = s2[i] - '0';
n2 = n2 * b2 + v;
}
g << n1 + n2;
}