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