#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, ans = 0;
string a;
cin >> n >> a;
//字符串反转
reverse(a.begin(), a.end());
for (int i = 0; i < a.size(); i ++ )
if (a[i] >= '0' and a[i] <= '9') ans += (a[i] - '0') * pow(n, i);
else ans += (a[i] - 'A' + 10) * pow(n, i);
cout << ans;
return 0;
}