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