#include <fstream>
#include <cstring>
using namespace std;
int b1,b2,nr1,nr2,val,i;
char s1[25],s2[25],ch;
int main()
{
ifstream f("baze1.in");
ofstream g("baze1.out");
f>>b1>>b2;
f>>s1>>s2;
for(i=0;i<strlen(s1);i++)
{
val=s1[i]-'0';
nr1=nr1*b1+val;
}
for(i=0;i<strlen(s2);i++)
{
val=s2[i]-'0';
nr2=nr2*b2+val;
}
g<<nr1+nr2;
f.close();
g.close();
return 0;
}