#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M;
  cin >> N >> M;
  long long q = M / N;
  if (q * N != M)
    cout << -1;
  else {
    long long res = 0;
    while (q && q % 2 == 0) {
      q /= 2;
      res++;
    }
    while (q && q % 3 == 0) {
      q /= 3;
      res++;
    }
    if (q != 1) res = -1;
    cout << res;
  }
}
