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