#include <bits/stdc++.h>
using namespace std;
int main() {
  int64_t m, n, d, count = 0, f = 1;
  cin >> m >> n;
  if (n % m == 0) {
    f = 0;
    d = n / m;
    while (d % 2 == 0) {
      d /= 2;
      count++;
    }
    while (d % 3 == 0) {
      d /= 3;
      count++;
    }
  }
  if (f == 1)
    cout << -1;
  else if (d != 1)
    cout << -1;
  else
    cout << count;
  return 0;
}
