A reversible prime in any number system is a prime whose “reverse” in that number system is also a prime. For example in the decimal system 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_i++ is a reversible prime because its reverse 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_数字_02 is also a prime.

Now given any two positive integers 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_03 and 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_i++_04, you are supposed to tell if 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_05 is a reversible prime with radix 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_06.

Input Specification:
The input file consists of several test cases. Each case occupies a line which contains two integers 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_05 and 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_06. The input is finished by a negative 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_05.

Output Specification:
For each test case, print in one line Yes if 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_05 is a reversible prime with radix 【PAT (Advanced Level) Practice】1015 Reversible Primes (20 分)_ios_06, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

#include<iostream>

using namespace std;

bool is_prime(int n){

if(n < 2) return false;
for(int i = 2; i <= n / i; i++)
if(n % i == 0) return false;

return true;
}


bool check(int x, int d){

if(!is_prime(x)) return false;

int res = 0;

while(x){
res = res * d + x % d;
x /= d;
}

return is_prime(res);
}

int main(){

int n, d;

while(cin >> n >> d, n > 0){

if(check(n, d)) puts("Yes");
else puts("No");
}

return 0;
}