PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SP0000061. Nguyên tố hay số đối xứng? (Primes or Palindromes?)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Nguyên tố hay số đối xứng? (Primes or Palindromes?)

Primes or Palindromes?

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Rikhail compares how many primes and positive decimal palindromes there are. Let π(n)\pi(n) count primes not exceeding nn, and let rub(n)rub(n) count positive integers not exceeding nn whose decimal representations read the same forward and backward, without leading zeros. Given positive integers p,qp,q and A=p/qA=p/q, find the largest positive integer nn such that q⋅π(n)≤p⋅rub(n)q\cdot\pi(n)\le p\cdot rub(n). If a largest such integer does not exist, print the specified sentence.

Input

One line contains positive integers pp and qq, the numerator and denominator of AA.

Output

Print the largest valid integer nn. If there is no maximum, print exactly Palindromic tree is better than splay tree.

Subtasks

  • Subtask 1 (20 points): 1≤p,q≤61\le p,q\le 6.
  • Subtask 2 (30 points): 1≤p,q≤161\le p,q\le 16.
  • Subtask 3 (50 points): 1≤p,q≤421\le p,q\le 42.

Examples

Input

1 1

Output

40

Explanation

For p=q=1p=q=1 we need π(n)≤rub(n)\pi(n)\le rub(n). At n=40n=40, the primes are 2,3,5,7,11,13,17,19,23,29,31,372,3,5,7,11,13,17,19,23,29,31,37, so π(40)=12\pi(40)=12. The palindromes are 11 through 99, plus 11,22,3311,22,33, giving rub(40)=12rub(40)=12. The final valid value is 4040.