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