import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
public class TiaoShiBan {
public static List<Integer> getDivisor(int number){
List<Integer> divisor = new LinkedList<Integer>();
for(int i = 2; i <= Math.sqrt(number); i++){
if(number % i == 0){
divisor.add((int) i);
if(number /i != i ){
divisor.add(number / i);
}
}
}
return divisor;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int start = in.nextInt();
int end = in.nextInt();
int[] seq = new int[end + 1];
seq[start] = 1;
for(int i = start ; i < end + 1;i++){
if(seq[i] > 0){
List<Integer> divisor = getDivisor(i);
for(int j = 0; j < divisor.size(); j++){
if((i + divisor.get(j)) > end){
continue;
}
//当其为零,或者值大于步数
if(seq[i + divisor.get(j)] == 0 || seq[i + divisor.get(j)] > seq[i] + 1 ){
seq[i + divisor.get(j)] = seq[i] + 1;
}
}
}
}
if(seq[end] == 0){
System.out.println(-1);
}else{
System.out.println(seq[end] - 1);
}
}
}
import java.util.List;
import java.util.Scanner;
public class TiaoShiBan {
public static List<Integer> getDivisor(int number){
List<Integer> divisor = new LinkedList<Integer>();
for(int i = 2; i <= Math.sqrt(number); i++){
if(number % i == 0){
divisor.add((int) i);
if(number /i != i ){
divisor.add(number / i);
}
}
}
return divisor;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNext()){
int start = in.nextInt();
int end = in.nextInt();
int[] seq = new int[end + 1];
seq[start] = 1;
for(int i = start ; i < end + 1;i++){
if(seq[i] > 0){
List<Integer> divisor = getDivisor(i);
for(int j = 0; j < divisor.size(); j++){
if((i + divisor.get(j)) > end){
continue;
}
//当其为零,或者值大于步数
if(seq[i + divisor.get(j)] == 0 || seq[i + divisor.get(j)] > seq[i] + 1 ){
seq[i + divisor.get(j)] = seq[i] + 1;
}
}
}
}
if(seq[end] == 0){
System.out.println(-1);
}else{
System.out.println(seq[end] - 1);
}
}
}
}
约数求解时,复杂度太高不能过