import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int a = sc.nextInt();
int b = sc.nextInt();
int m = String.valueOf(a).length();
int n = String.valueOf(b).length();
List<String> tmpA = cal(a);
List<String> tmpB = cal(b);
int res = 100;
for(String p: tmpA){
// System.out.print(p + " p ");
int x = Integer.valueOf(p);
int curLenX = p.length();
for(String q: tmpB){
// System.out.print(q + " q ");
int y = Integer.valueOf(q);
int curLenY = q.length();
if(x==0 || y == 0){
res = Math.min(res, m-curLenX+n-curLenY);
}else if(x%y==0 || y%x==0){
res = Math.min(res, m-curLenX+n-curLenY);
}
}
}
if(res == 100){
System.out.println(-1);
return ;
}
System.out.println(res);
}
public static List<String> cal(int num) {
String s = String.valueOf(num);
int n = s.length();
List<String> res = new ArrayList<>();
for (int i = 1; i < (1 << n); i++) {
StringBuilder ans = new StringBuilder();
for (int j = 0; j < n; j++) {
if ((i & (1 << j)) != 0) {
ans.append(s.charAt(j) + "");
}
}
if(!ans.toString().equals("")){
res.add(ans.toString());
}
}
return res;
}
}