#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
  scanf("%d %d", &n, &m);
  if (m % n == 0) {
    int mv = m / n;
    int ans = 0;
    while (mv % 2 == 0) {
      mv = mv / 2;
      ans++;
    }
    while (mv % 3 == 0) {
      mv = mv / 3;
      ans++;
    }
    if (mv == 1)
      printf("%d", ans);
    else
      printf("-1");
  } else {
    printf("-1");
  }
}
