#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, m;
  cin >> n >> m;
  long long first = m / n;
  if (first * n != m) {
    cout << -1;
    exit(0);
  }
  long long ans = 0;
  while (first > 1) {
    if (first % 2 == 0 || first % 3 == 0) {
      if (first % 2 == 0) {
        ans++;
        first = first / 2;
      } else {
        ans++;
        first = first / 3;
      }
    } else {
      cout << -1;
      exit(0);
    }
  }
  cout << ans;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
