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