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