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