#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  double num;
  num = (double)m / n;
  if (n == m) {
    cout << "0";
  } else {
    long long int x = num, counter1 = 0, counter2 = 0;
    if (ceil(num) == num) {
      if (x % 2 == 0 || x % 3 == 0) {
        while (x % 2 == 0) {
          x = x / 2;
          counter1++;
        }
        while (x % 3 == 0) {
          x = x / 3;
          counter2++;
        }
        if (pow(2, counter1) * pow(3, counter2) == num)
          cout << counter1 + counter2;
        else
          cout << "-1";
      } else
        cout << "-1";
    } else
      cout << "-1";
  }
  return 0;
}
