#include <bits/stdc++.h>
using namespace std;
int res;
long long c2[31], c3[31], n, m;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  c2[0] = 1;
  c3[0] = 1;
  for (int i = 1; i <= 30; i++) {
    c2[i] = c2[i - 1] * 2;
    c3[i] = c3[i - 1] * 3;
  }
  res = 1e7;
  for (int i = 0; i <= 30; i++)
    for (int j = 0; j <= 30; j++)
      if (n * c2[i] * c3[j] == m) res = min(res, i + j);
  if (res != 1e7)
    cout << res;
  else
    cout << -1;
}
