#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, m, p3 = 0, p2 = 0;
  cin >> n >> m;
  if (m >= n) {
    if (m % n == 0) {
      if (n != m) {
        n = m / n;
        while (n % 2 == 0) {
          n = n / 2;
          p2++;
        }
        while (n % 3 == 0) {
          n = n / 3;
          p3++;
        }
        if (n == 1) {
          cout << p2 + p3;
        } else
          cout << "-1";
      } else if (n == m)
        cout << "0";
    } else if (m % n != 0)
      cout << "-1";
  } else if (n > m)
    cout << "-1";
  return 0;
}
