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

#SP0000244. Đường đi giữa các số nguyên tố (Prime Path)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đường đi giữa các số nguyên tố (Prime Path)

Prime Path

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given two four-digit primes aa and bb, a move changes exactly one digit of the current number to a different digit. The resulting number must still be a four-digit prime (its thousands digit cannot become zero). Each move costs 11.

Find the minimum number of moves required to transform aa into bb. If no transformation is possible, print exactly Impossible. If a=ba=b, the answer is 00.

Input

A single line contains two integers a,ba,b, each a prime between 10001000 and 99999999.

Output

Print an integer equal to the minimum number of digit changes. If no valid transformation exists, print exactly Impossible (case-sensitive).

Subtasks

  • Subtask 1 (20 points): 1000≤a,b≤99991000\le a,b\le9999; both are four-digit primes; a=ba=b.

  • Subtask 2 (30 points): 1000≤a,b≤99991000\le a,b\le9999; both are four-digit primes; a≠ba\ne b and their decimal representations differ in at most two positions.

  • Subtask 3 (50 points): 1000≤a,b≤99991000\le a,b\le9999; both are four-digit primes; the numbers of differing decimal digit positions may be 0,1,2,30,1,2,3, or 44.

Examples

Input

1033 8179

Output

6

Explanation

A valid transformation is 1033→1733→3733→3739→3779→8779→81791033\to1733\to3733\to3739\to3779\to8779\to8179. Each arrow replaces one digit, and every intermediate number is a four-digit prime. These six arrows give a cost of 66. Breadth-first search checks all shorter move counts first and confirms there is no valid path with fewer than six moves, so the output is 6.