#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long int n, m, ans, x;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  if (m == n)
    return cout << "0\n", 0;
  else if (m % n)
    return cout << "-1\n", 0;
  x = m / n;
  while (!(x % 2)) x /= 2, ans++;
  while (!(x % 3)) x /= 3, ans++;
  (x != 1 || !ans) ? (cout << "-1\n") : (cout << ans << '\n');
  return 0;
}
