#include <bits/stdc++.h>
using namespace std;
int mod = 1e9 + 7;
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = (res % mod) * (a % mod), res %= mod;
    a = (a % mod) * (a % mod);
    a %= mod;
    b >>= 1;
  }
  return res;
}
long long divide(long long a, int b) {
  return ((a % mod) * binpow(b, mod - 2) % mod) % mod;
}
long long nCr(long long n, long long k) {
  long long res = 1;
  if (k > n - k) k = n - k;
  for (int i = 0; i < k; ++i) {
    res = ((res % mod) * ((n - i) % mod)) % mod;
    res = divide(res, i + 1);
  }
  return res;
}
int a, b;
bool check(int n) { return n >= 7; }
int solve(int x, int y) {
  if (x == y) return 0;
  int ret = 1000000;
  if (x % 3 == 0) ret = min(ret, 1 + solve(x / 3, y));
  if (x % 2 == 0) ret = min(ret, 1 + solve(x / 2, y));
  if (x % 5 == 0) ret = min(ret, 1 + solve(x / 5, y));
  if (y % 3 == 0) ret = min(ret, 1 + solve(x, y / 3));
  if (y % 2 == 0) ret = min(ret, 1 + solve(x, y / 2));
  if (y % 5 == 0) ret = min(ret, 1 + solve(x, y / 5));
  return ret;
}
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int a, b;
  cin >> a >> b;
  map<int, int> cnt_a, cnt_b;
  set<int> st;
  for (int i = 2; i * i <= a; i++) {
    while (a % i == 0) {
      cnt_a[i]++;
      a /= i;
      st.insert(i);
    }
  }
  if (a > 1) cnt_a[a]++, st.insert(a);
  for (int i = 2; i * i <= b; i++) {
    while (b % i == 0) {
      cnt_b[i]++;
      b /= i;
      st.insert(i);
    }
  }
  if (b > 1) cnt_b[b]++, st.insert(b);
  int cnt = 0;
  for (auto i : st) {
    if (i >= 7 && (!cnt_a[i] || !cnt_b[i])) {
      return cout << "-1", 0;
    }
    cnt += abs(cnt_a[i] - cnt_b[i]);
  }
  cout << cnt;
}
