#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, n;
  cin >> i;
  cin >> n;
  if (n == i) {
    cout << 0 << endl;
    return 0;
  }
  int moves = 0;
  while (n % (i * 3) == 0 && n > i) {
    i *= 3;
    moves++;
  }
  while (n % (i * 2) == 0 && n > i) {
    i *= 2;
    moves++;
  }
  if (i == n)
    cout << moves << endl;
  else
    cout << -1 << endl;
  return 0;
}
