#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e9 + 55;
const int inf = 1e9 + 77;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
const double eps = 1e-7;
vector<int> primes;
bitset<int(1e7) + 2> isprime;
void seive() {
  isprime[0] = isprime[1] = 1;
  for (int i = 2; i < int(1e6); ++i) {
    if (!isprime.test(i)) {
      primes.push_back(i);
      for (int j = i * 2; j < int(1e6) + 2; j += i) {
        isprime.set(j);
      }
    }
  }
}
const int n = 1e6 + 55;
int p[2][n];
bool check(int n) {
  if (n < int(1e6)) return !isprime.test(n);
  for (auto i : primes)
    if (n % i == 0) return false;
  return true;
}
void factor(int a, int f) {
  long long id = 0, pf = primes[0];
  while (pf * pf <= a) {
    while (a % pf == 0) {
      a /= pf;
      p[f][pf]++;
    }
    pf = primes[++id];
  }
  if (a != 1) p[f][a]++;
}
int main() {
  seive();
  int a, b;
  scanf("%d", &a);
  scanf("%d", &b);
  bool t1 = true;
  if (!check(a)) {
    factor(a, 0);
    t1 = false;
  }
  bool t2 = true;
  if (!check(b)) {
    factor(b, 1);
    t2 = false;
  }
  if (t1 && t2) {
    printf("-1");
    return 0;
  }
  if (t1 || t2) {
    if (t1) {
      if (t1 > int(1e6)) {
        printf("-1");
        return 0;
      }
    } else {
      if (t2 > int(1e6)) {
        printf("-1");
        return 0;
      }
    }
  }
  if (t1) factor(a, 0);
  if (t2) factor(b, 1);
  int res1 = 0;
  for (int i = 7; i < n; ++i) {
    if (abs(p[1][i] - p[0][i]) > 0) {
      res1 = -1;
      break;
    }
  }
  if (res1 == -1) {
    printf("-1");
    return 0;
  }
  for (int i = 2; i <= 5; ++i) {
    res1 += abs(p[0][i] - p[1][i]);
  }
  printf("%d", res1);
  return 0;
}
