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