#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return power((x * x) % (long long int)1000000007, y / 2);
  else
    return (x * power((x * x) % (long long int)1000000007, (y - 1) / 2)) %
           (long long int)1000000007;
}
long long int modInverse(long long int n) {
  return power(n, (long long int)1000000007 - 2);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    long long int n, m;
    cin >> n >> m;
    if (m % n != 0) {
      cout << -1;
      return 0;
    }
    long long int p = m / n;
    long long int cnt = 0;
    while (p % 2 == 0) {
      p /= 2;
      cnt++;
    }
    while (p % 3 == 0) {
      p /= 3;
      cnt++;
    }
    if (p == 1)
      cout << cnt;
    else
      cout << -1;
  }
}
