#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  if (m % n != 0)
    cout << -1;
  else {
    int ans = 0;
    int val = m / n;
    while (val % 3 == 0) {
      val /= 3;
      ans++;
    }
    while (val % 2 == 0) {
      val /= 2;
      ans++;
    }
    if (val != 1)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}
