using
System;
using
System.Collections.Generic;
class
GFG{
class
pair
{
public
int
first, second;
public
pair(
int
first,
int
second)
{
this
.first = first;
this
.second = second;
}
}
static
int
[]spf =
new
int
[100009];
static
void
sieve()
{
for
(
int
i = 0; i < 100005; i++)
spf[i] = -1;
for
(
int
i = 2; i * i <= 100005; i++)
{
for
(
int
j = i; j <= 100005; j += i)
{
if
(spf[j] == -1)
{
spf[j] = i;
}
}
}
}
static
HashSet<
int
> findPrimeFactors(HashSet<
int
> s,
int
n)
{
while
(n > 1)
{
s.Add(spf[n]);
n /= spf[n];
}
return
s;
}
static
int
MinimumSteps(
int
n,
int
m)
{
Queue<pair> q =
new
Queue<pair>();
HashSet<
int
> s =
new
HashSet<
int
>();
q.Enqueue(
new
pair(n, 0));
while
(q.Count != 0)
{
int
newNum = q.Peek().first;
int
distance = q.Peek().second;
q.Dequeue();
HashSet<
int
> k = findPrimeFactors(s,
newNum);
foreach
(
int
i
in
k)
{
if
(newNum == m)
{
return
distance;
}
else
if
(newNum > m)
{
break
;
}
else
{
q.Enqueue(
new
pair(newNum + i,
distance + 1));
}
}
}
return
-1;
}
public
static
void
Main(String[] args)
{
int
N = 7, M = 16;
sieve();
Console.Write(MinimumSteps(N, M));
}
}