#include<cstdio> #include<cstring> char a[21]; int n; int pd(int i) { if(a[i]=='0')return 0; else if(a[i]=='1')return 1; else if(a[i]=='2')return 2; else if(a[i]=='3')return 3; else if(a[i]=='4')return 4; else if(a[i]=='5')return 5; else if(a[i]=='6')return 6; else if(a[i]=='7')return 7; else if(a[i]=='8')return 8; else if(a[i]=='9')return 9; else if(a[i]=='A')return 10; else if(a[i]=='B')return 11; else if(a[i]=='C')return 12; else if(a[i]=='D')return 13; else if(a[i]=='E')return 14; else if(a[i]=='F')return 15; } int main() { int ans=0,t=1;scanf("%s %d",a+1,&n); for(int i=strlen(a+1);i>=1;i--) { ans+=pd(i)*t;t*=n; } printf("%d",ans); }