#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 42;
template <typename T>
void pop_front(std::vector<T> &vec) {
  assert(!vec.empty());
  vec.erase(vec.begin());
}
bool compPairF(pair<long long, long long> v1, pair<long long, long long> v2) {
  return v1.first < v2.first;
}
bool compPairS(pair<long long, long long> v1, pair<long long, long long> v2) {
  return v1.second < v2.second;
}
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}
long long power(long long x, long long y) {
  long long temp;
  if (y == 0) return 1;
  temp = power(x, y / 2);
  if (y % 2 == 0)
    return temp * temp;
  else
    return x * temp * temp;
}
vector<long long> primefactorisation(long long n) {
  vector<long long> ans;
  long long t = 2;
  while (n > 1) {
    if (n % t == 0) {
      ans.push_back(t);
      n = n / t;
    } else {
      t++;
    }
  }
  return ans;
}
void sol() {
  long long n, m;
  cin >> n >> m;
  long long ans = 0;
  long long t = 2;
  while (n > 1 || m > 1) {
    if (t > 5 && m != n) {
      cout << -1 << endl;
      return;
    }
    if (n == m) {
      break;
    }
    if (n % t == 0 && m % t == 0) {
      n = n / t;
      m = m / t;
    } else {
      if (n % t != 0 && m % t != 0)
        t++;
      else if (t > 5) {
        cout << -1 << endl;
        return;
      } else if (n % t == 0) {
        n = n / t;
        ans++;
      } else if (m % t == 0) {
        m = m / t;
        ans++;
      } else {
        t++;
      }
    }
  }
  if (n != m) {
    cout << -1 << endl;
    return;
  }
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int a = 1;
  while (a--) {
    sol();
  }
}
