#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//判断回文数的函数(字符串)/
int is_palindrome(long long int x){
char str[1000];
sprintf(str,"%lld",x);
int len = strlen(str);
long long int number1 = atoi(str);
char data[len];
for(int i = 1;i <=len;i++){
data[i-1]= str[len - i];
}//创建一个逆序的字符串
long long int number2 = atoi(data);
if(number1 == number2){
return 1;
}else{
return 0;
}
}
long long int convert_decimal(int x,int decimal){
// long long int origin = x;
long long int sum=0;
int quotient=x / decimal;
int i=1;
sum = x % decimal;
while(quotient !=0){
x = quotient;
if(quotient == 0)
break;
quotient = x / decimal;
sum = sum + (x % decimal) * (long long int)(pow(10,i));
i++;
}
//printf("%d的%d进制数是%d\n",origin,decimal,sum);
return sum;
}
int main()
{
int n,k;
scanf("%d %d",&n,&k);
int sum =0;
for(int i =1;i<=n;i++){
if(is_palindrome(i)){
if(is_palindrome( convert_decimal(i,k))){
sum += i;
//printf("双基回文数:%d 它的%d进制数是%lld\n",i,k,convert_decimal(i,k));
}
}
}
printf("%d",sum);
return 0;
}