let [n, b] = require("fs").readFileSync(0, "utf8").toString().split(' ');
let res=0;
b = +b;
for(let i=0; i<n.length; i++){
    let val = (n[i]>='A' && n[i]<='Z') ? n[i].charCodeAt() - 'A'.charCodeAt() + 10 : n[i].charCodeAt() - '0'.charCodeAt();
    res = res*b + val;
}
console.log(res);