#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  std::cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  std::cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long int n, m;
  cin >> n >> m;
  if (m % n != 0) {
    cout << "-1";
    return 0;
  }
  long long int quo = m / n;
  long long int count = 0;
  while (quo != 1) {
    if (quo % 2 == 0) {
      quo = quo / 2;
      count++;
    } else if (quo % 3 == 0) {
      quo = quo / 3;
      count++;
    } else {
      cout << "-1";
      return 0;
    }
  }
  cout << count;
  return 0;
}
