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