#include <bits/stdc++.h>
using namespace std;
const int INT_INF = 1e9;
const long long LL_INF = 2e18;
typedef struct E {
  int to, w;
  E(){};
  E(int _t, int _w) : to(_t), w(_w){};
};
typedef struct P {
  int l, r;
};
P cow[500002];
int n, m;
int A[500002], B[500002];
int main() {
  scanf("%d %d", &n, &m);
  if (m % n != 0) {
    puts("-1");
    return 0;
  }
  int cnt = 0;
  int k = m / n;
  while (k % 2 == 0) {
    k /= 2;
    cnt++;
  }
  while (k % 3 == 0) {
    k /= 3;
    cnt++;
  }
  if (k != 1) {
    puts("-1");
  } else {
    printf("%d", cnt);
  }
  return 0;
}
