#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n1, k1, n, k;
  cin >> n1 >> k1;
  n = n1;
  k = k1;
  int a, b, c, d;
  int i = 0, sum1 = 0, sum = 0;
  while (n % 2 == 0) {
    n = n / 2;
    i++;
  }
  a = i;
  sum = sum + i;
  i = 0;
  while (n % 3 == 0) {
    i++;
    n = n / 3;
  }
  b = i;
  sum = sum + i;
  i = 0;
  while (k % 2 == 0) {
    i++;
    k = k / 2;
  }
  sum1 = sum1 + i;
  c = i;
  i = 0;
  while (k % 3 == 0) {
    i++;
    k = k / 3;
  }
  sum1 = sum1 + i;
  d = i;
  if (n1 == k1) {
    cout << "0";
  } else if (a > c || b > d || (a == 0 && b == 0 && c == 0 && d == 0) ||
             n != k) {
    cout << "-1";
  } else {
    cout << abs(sum1 - sum);
  }
}
