#include <string>
#include <vector>
using namespace std;
int Min(int x, int y, int z, int w)
{
if(x<=y&&x<=z&&x<=w)
return x;
if(y<=x&&y<=z&&y<=w)
return y;
if(z<=x&&z<=y&&z<=w)
return z;
return w;
}
int check(int x)
{
int cnt=0;
int p=0;
int temp=x%10;
while(x!=0)
{
if(x%10!=temp){
p=-1;
break;
}
temp=x%10;
x/=10;
cnt++;
}
if(p==-1)
return 0;
return cnt;
}
int hee(int x, int y, int cnt)
{
if(cnt>=9||y<0)
return 99999;
if(check(y)!=0)
return check(y);
int k= Min(hee(x, y-x, cnt+1)+1, hee(x, y+x, cnt+1)+1, hee(x, y/x, cnt+1)+1, hee(x, y*x, cnt+1)+1);
if(k==99999)
return -1;
return k;
}
int solution(int N, int number) {
int answer = 0;
answer=hee(N, number, 0);
return answer;
}