#include <bits/stdc++.h>
using namespace std;
bool comparator(pair<long long int, long long int> p1,
                pair<long long int, long long int> p2) {
  if (p1.first < p2.first) return true;
  return false;
}
int sumNatural(int n) {
  int sum = (n * (n + 1)) / 2;
  return sum;
}
int main() {
  ios::sync_with_stdio(false);
  long long int i, j, k, q, t, tmp, n, flg = 0, cntr = 0, maxxer = 0;
  long long int m;
  long long int ans = 100;
  cin >> n >> m;
  for (i = 0; i < 62; i++) {
    for (j = 0; j < 62; j++) {
      tmp = m;
      if (tmp % (int)pow(2, i) == 0) {
        tmp = tmp / (int)pow(2, i);
      }
      if (tmp % (int)pow(3, j) == 0) {
        tmp = tmp / (int)pow(3, j);
      }
      if (tmp == n) {
        flg = 1;
        ans = min(ans, i + j);
      }
    }
  }
  if (!flg) ans = -1;
  if (n == m) {
    ans = 0;
  }
  cout << ans;
  return 0;
}
