#include <bits/stdc++.h>
using namespace std;
const long long INF = LLONG_MAX;
const long long base = 1e9;
const double PI = acos(-1);
const long long MOD = 1e9 + 7;
const long long N = 2e5 + 10;
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long dv = m / n, rem = m % n;
  long long ans = 0;
  if (rem != 0) {
    cout << "-1" << endl;
    return 0;
  } else {
    while (dv != 1) {
      if ((dv % 2) == 0) {
        ans++;
        dv /= 2;
      } else if ((dv % 3) == 0) {
        ans++;
        dv /= 3;
      } else {
        cout << "-1" << endl;
        return 0;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
