#include <bits/stdc++.h>
int main() {
  long n;
  long m;
  std::cin >> n >> m;
  long a = 0, b = 0, c = 0, d = 0;
  while (!(n % 2)) {
    n = n / 2;
    a++;
  }
  while (!(n % 3)) {
    n = n / 3;
    b++;
  }
  while (!(m % 2)) {
    m = m / 2;
    c++;
  }
  while (!(m % 3)) {
    m = m / 3;
    d++;
  }
  if ((n != m) || (a > c) || (b > d)) {
    std::cout << -1;
  } else {
    std::cout << (c + d) - (a + b);
  }
  return 0;
}
