#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, b;
  cin >> a >> b;
  int p21 = 0, p22 = 0, p31 = 0, p32 = 0;
  while (a % 2 == 0) p21++, a /= 2;
  while (a % 3 == 0) p31++, a /= 3;
  while (b % 2 == 0) p22++, b /= 2;
  while (b % 3 == 0) p32++, b /= 3;
  if (a != b) {
    cout << -1;
    return 0;
  }
  if (p21 > p22 || p31 > p32) {
    cout << -1;
    return 0;
  } else {
    cout << abs(p21 - p22) + abs(p31 - p32);
    return 0;
  }
}
