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