#include<bits/stdc++.h>
using namespace std;
#define zd 1000005
typedef long long ll;
int main()
{
string c="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int a,d=0;
char b[37];
cin>>a>>b;
for(int i=1;i<=strlen(b);i++){
for(int j=0;j<=a;j++){
if(c[j]==b[i]){
d+=j*pow(a,i-1);
}
}
}
cout<<d;
return 0;
}