#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5;
const long long INF = 1e18;
const int mod = 1e9 + 7;
const double pi = 3.1415926536;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, -1, 1};
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  ;
  int n, m;
  cin >> n >> m;
  if (n == m) return cout << "0\n", 0;
  if (m % n) return cout << "-1\n", 0;
  int x = m / n, cnt = 0;
  while (x % 2 == 0) x /= 2, cnt++;
  while (x % 3 == 0) x /= 3, cnt++;
  if (x != 1) cnt = -1;
  cout << cnt << '\n';
  return 0;
}
