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