#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int maxn = 1;
const double EPS = 1e-8;
class AGame23 {
 public:
  void solve(std::istream& in, std::ostream& out) {
    int n, m;
    in >> n >> m;
    if (m % n != 0) {
      out << -1 << '\n';
      return;
    }
    m /= n;
    int res = 0;
    while (m && m % 2 == 0) m /= 2, res++;
    while (m && m % 3 == 0) m /= 3, res++;
    if (m != 1) {
      out << -1 << '\n';
      return;
    } else
      out << res << '\n';
  }
};
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  AGame23 solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
