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