import std.algorithm, std.array, std.container, std.range, std.bitmanip; import std.numeric, std.math, std.bigint, std.random; import std.string, std.conv, std.stdio, std.typecons; void main() { auto k = readln.chomp.to!int; auto n = readln.chomp.to!int; auto pi = primes(n).assumeSorted.upperBound(k - 1).array; auto hi = pi.map!(calcHash).array; auto memo = new int[](10); memo[] = -1; auto maxLen = 0, maxPos = 0; auto s = 0, i = 0; while (i < hi.length) { auto h = hi[i]; if (memo[h] < 0) { memo[h] = i; i += 1; } else { auto len = i - s; if (len >= maxLen) { maxLen = len; maxPos = s; } s = i = memo[h] + 1; memo[] = -1; } } auto len = i - s; if (len >= maxLen) { maxLen = len; maxPos = s; } writeln(pi[maxPos]); } int calcHash(int n) { auto r = 0; while (n > 0) { r += n % 10; n /= 10; } if (r < 10) return r; else return calcHash(r); } int[] primes(int n) { auto sieve = new bool[](n + 1); sieve[0..2] = false; sieve[2..$] = true; foreach (p; 2..n.to!real.sqrt.to!int + 1) if (sieve[p]) foreach (q; iota(p * 2, n + 1, p)) sieve[q] = false; return sieve.enumerate .filter!("a[1]") .map!("a[0]").map!(to!int).array; }