#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &num) {
  char c = getchar();
  num = 0;
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    num = (num << 3) + (num << 1) + (c ^ 48);
    c = getchar();
  }
  num *= f;
}
template <typename T>
void qwq(T x) {
  if (x > 9) qwq(x / 10);
  putchar(x % 10 + '0');
}
template <typename T>
void write(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  qwq(x);
  putchar('\n');
}
int main() {
  int n, m;
  read(n);
  read(m);
  if (m % n != 0) {
    write(-1);
    return 0;
  }
  int temp = m / n;
  int ans = 0;
  while (temp % 2 == 0) {
    temp >>= 1;
    ans++;
  }
  while (temp % 3 == 0) {
    temp /= 3;
    ans++;
  }
  write((temp != 1) ? -1 : ans);
  return 0;
}
