#include <bits/stdc++.h>
using namespace std;
int ans(long n, long m) {
  if (n > m || m % n != 0) return -1;
  if (n == m) return 0;
  m /= n;
  int steps = 0;
  while (m % 6 == 0) {
    m /= 6;
    steps += 2;
  }
  while (m % 2 == 0) {
    m >>= 1;
    steps++;
  }
  while (m % 3 == 0) {
    m /= 3;
    steps++;
  }
  return (m != 1) ? -1 : steps;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long n, m;
  cin >> n;
  cin >> m;
  cout << ans(n, m) << endl;
}
