Polycarp plays "Game 23". Initially he has a number $$$n$$$ and his goal is to transform it to $$$m$$$. In one move, he can multiply $$$n$$$ by $$$2$$$ or multiply $$$n$$$ by $$$3$$$. He can perform any number of moves.Print the number of moves needed to transform $$$n$$$ to $$$m$$$. Print -1 if it is impossible to do so.It is easy to prove that any way to transform $$$n$$$ to $$$m$$$ contains the same number of moves (i.e. number of moves doesn't depend on the way of transformation).

The only line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le m \le 5\cdot10^8$$$).

Print the number of moves to transform $$$n$$$ to $$$m$$$, or -1 if there is no solution.

NoteIn the first example, the possible sequence of moves is: $$$120 \rightarrow 240 \rightarrow 720 \rightarrow 1440 \rightarrow 4320 \rightarrow 12960 \rightarrow 25920 \rightarrow 51840.$$$ The are $$$7$$$ steps in total.In the second example, no moves are needed. Thus, the answer is $$$0$$$.In the third example, it is impossible to transform $$$48$$$ to $$$72$$$.

label 1;
var q,s,n,m:int64;
i,d,e,r:integer;
begin
r:=-1; d:=-1;
readln(n,m);
if n=m then begin writeln('0');goto 1; end;
if (n=1) and (m mod 3<>0) and (m mod 2<>0) then begin writeln('-1'); goto 1; end;
if (m mod n=0) then begin r:=0; d:=0; end else begin writeln('-1'); goto 1; end;
e:=m div n;
while ( e mod 2=0) do
begin
e:=e div 2; d:=d+1;
end;
e:=m div n;
while ( e mod 3=0) do begin e:=e div 3; r:=r+1; end;
s:=1; q:=1;
for i:=1 to d do begin s:=s*2; end;
for i:=1 to r do begin q:=q*3; end;
if q*s<> m div n then writeln('-1') else writeln(r+d);
1: end.

var m,a,n,i:integer;
begin
read(n,m);
if m=n then begin
write(0);
halt(0);
end;
if (m mod n <>0)or((m mod 2 <>0) and (m mod 3<>0)) then begin
write(-1);
halt(0);
end;
a:=m div n;
while a<>1 do begin
if (a mod 2 <> 0) and (a mod 3 <> 0) then begin
write(-1);
halt(0);
end;
if a mod 3 =0 then begin a:=a div 3;
inc(i);
end
else
if a mod 2 =0 then begin a:=a div 2;
inc(i);
end;
end;
write(i);
end.

program one;
var
a,b,c,k:integer;
begin
readln(a,b);
if b mod a = 0 then
	begin
	c:=b div a;
    while c>1 do
        begin
        if (c mod 3 = 0) and (c div 3 >= 1) then
            begin
            c:=c div 3;
            end
            else
            begin
            if (c mod 2 = 0) and (c div 2 >= 1) then
                begin
                c:=c div 2;
                end
                else
                begin
                writeln('-1');
                exit;
                end;
            end;
        k:=k+1;
        end;
	end
	else
	begin
	writeln('-1');
	exit;
	end;
writeln(k);
end.

var n,m,l,k:integer;
begin
read(n);read(m);
k:=0;
if (n > m) or (m mod n <> 0) then
 write('-1')
else
 begin
   if n = m then
    write('0')
   else
    begin
     l:=m div n;
     while (l>1) and ((l mod 3 = 0) or (l mod 2 = 0)) do
      begin
       k:=k+1;
      if l mod 3 = 0 then
       l:= l div 3
      else
        l:= l div 2
       end;
      if l = 1 then
       write(k)
      else
       write('-1')
      end;
   end;
end.

var n, m, ans, k :integer;

begin
	readln(n, m);
	ans := 0;

  if m mod n > 0 then
    writeln(-1)
  else
    begin
      k := m div n;
      while k mod 3 = 0 do
      begin
        ans := ans + 1;
        k := k div 3;
      end;

      while k mod 2 = 0 do
      begin
        ans := ans + 1;
        k := k div 2
      end;

      if k = 1 then
        writeln(ans)
      else
        writeln(-1)
    end

end.

var n,m,t,t1,t2,x,y:integer;
begin
read(n,m);
if m mod n>0 then begin write(-1);exit;end;
x:=m div n;y:=1;
while (x>=y*6)and(x mod (y*6)=0) do begin t+=2;y*=6;end;
x:=x div y;y:=1;
while (x>=y*2)and(x mod (y*2)=0) do begin t1+=1;y*=2;end;
if (x mod y=0)and(x div y=1) then begin write(t+t1);exit;end;
x:=x div y;y:=1;
while (x>=y*3)and(x mod (y*3)=0) do begin t2+=1;y*=3;end;
if (x mod y=0)and(x div y=1) then begin write(t+t2);exit;end;
write(-1);
end.

var n,m,k,i:integer;
begin
 k:=0;
 read(n,m);
 if (m mod n <> 0 ) then
  begin
   writeln('-1');
  end else
   begin
   n:=m div n;
    while (n <> 1) and (( n mod 2 = 0) or (n mod 3 = 0)) do
     begin
      if ( n mod 2 = 0) then
       begin
        n:=n div 2;
        k:=k+1;
       end;
      if ( n mod 3 = 0) then
       begin
        n:=n div 3;
        k:=k+1;
       end;
     end;
     if (n = 1) then
      write(k) else
       write('-1');
   end;
end.

var a,b,k,p:integer;
begin
read(a,b);
if a=b then begin
write(0);
halt(0);
end;
if b mod a <> 0 then begin
write(-1);
halt(0);
end;
if (b mod 2 <> 0) and (b mod 3 <>0) then begin
write(-1);
halt(0);
end;
k:=b div a;
while k<>1 do begin
if (k mod 2 <>0) and (k mod 3 <> 0) then begin
write(-1);
halt(0);
end;
if k mod 3 = 0 then begin
inc(p);
k:=k div 3
end
else if (k mod 2 = 0) then begin
inc(p);
k:=k div 2;
end;
end;
write(p);
end.

var k,n,m:longint;
begin
read(n,m);
k:=0;
if m mod n >0 then begin writeln(-1);exit;end;
n:=m div n;
while n>1 do
 begin
  if n mod 2=0 then n:=n div 2 else
  if n mod 3=0 then n:=n div 3 else begin writeln(-1);exit; end;
  inc(k);
  end;
  writeln(k);
  end.

  var
    n,m,d,count:longint;

  begin
    readln(n,m);
    count:=0;
    if m=n then count:=0 else
      begin
    if (m mod n = 0) and (m<>n) then
      begin
        m:=m div n;
        while  (m mod 2 = 0) or (m mod 3 = 0) do
          begin
            if m mod 3 = 0 then
              begin
                m:=m div 3;
                inc(count);
              end;
            if m mod 2 = 0 then
              begin
                m:=m div 2;
                inc(count);
              end;
          end;
      end;
      end;
    if (m=1) or (m=n) then writeln(count)
    else writeln(-1);
  end.

Var
    n, m, Ans: UInt64;

Begin
    Read(n, m);
    For Var i := 0 to 28 do
        For Var j := 0 to 18 do
            If n*Round(Power(2, i))*Round(Power(3, j)) = m then Begin
                Write(i + j);
                Exit
            End;
    Write(-1)
End.

var
n:integer;
a,b,q:integer;
begin
read(a,b);
if b mod a <> 0 then n:=-1 else
begin
q:=b div a;
while q mod 3 = 0 do begin q:=q div 3; inc(n); end;
while q mod 2 = 0 do begin q:=q div 2; inc(n); end;
if q<>1 then n:=-1;
end;

write(n);
end.

  var
  m,n:longint;
  k,t:integer;
  Begin
  readln(n,m);
  k:=0;
  if m mod n <>0 then k:=-1;
  if (m div n=1)and(m mod n=0) then begin
                                    k:=0;
                                    t:=1;
                                    end;
  if (m mod n=0)and(m div n>1) then begin
             m:=m div n;
           while (m mod 3=0) do
            begin
            m:=m div 3;
            k:=k+1;
            end;
            while (m mod 2=0) do
            begin
            m:=m div 2;
            k:=k+1;
            end;
            end;
            if (m>1)and(t<>1) then k:=-1;
  writeln(k);
  End.

Program ILoveYou;
VAR n,m,k:int64; b:boolean;
Begin

Readln(n,m);

if ((m mod n) <> 0) then Writeln('-1') else
Begin

m:=m div n;
while ((m mod 2) = 0) and (m > 1) do
Begin
k:=k + 1;
m:=m div 2;
end;

while ((m mod 3) = 0) and (m > 1) do
Begin
k:=k + 1;
m:=m div 3;
end;

if (m = 1) then Writeln(k) else Writeln('-1');

end;

Readln;
END.

var
  n, m, an, z: uint64;

begin
  read(n, m);
  if n = m then
    write(0)
  else
  if (n = 1) and ((m mod 2 <> 0) and (m mod 3 <> 0)) then
    write(-1)
  else
  if m mod n <> 0 then
    write(-1)
  else
  begin
    z := m div n;
    while z mod 2 = 0 do
    begin
      z := z div 2;
      an := an + 1;
    end;
    while z mod 3 = 0 do
    begin
      z := z div 3;
      an := an + 1;
    end;
    if z = 1 then
      write(an)
    else
      write(-1);
  end;
end.

var n, m, a, i:longint; n1, m1:real; LABEL j;
begin
readln(n, m);
n1:=n;
m1:=m;
if m=n then begin writeln(0); goto j end;
if (m div n) = (m1/n1) then begin
  a:=m div n;
  while (a>3) do begin
    if (a mod 3 = 0) then a:=a div 3
    else if (a mod 2 = 0) then a:=a div 2
      else begin writeln(-1); goto j end;
    i:=i+1;
  end;
  writeln(i+1);
end else writeln(-1);
j:
end.

var
  a, b, res : integer;

begin
  readln(a, b);
  res:= 0;
  if (b mod a <> 0) then
    res:= -1
  else
  begin
    b:= b div a;
    while (b mod 3 = 0) do
    begin
      inc(res);
      b:= b div 3;
    end;
    while (b mod 2 = 0) do
    begin
      inc(res);
      b:= b div 2;
    end;
  end;
    if b = 1 then writeln(res)
    else writeln('-1');
  end.

var n,m,x,j : integer;
begin
  read(n,m);
  x := 0;
  j := m div n;
  if n=m then write(0)
         else if m mod n <> 0 then write('-1')
                              else if (j  mod 2 <> 0) and (j mod 3 <> 0) then write('-1')
                                                                         else
 begin
  while j mod 2 = 0 do
    begin
      inc(x);
      j := j div 2;
    end;
  while j mod 3 = 0 do
    begin
      inc(x);
      j := j div 3;
    end;
  if j = 1 then write(x)
           else write (-1);
end;
end.

var
  kol_step : longint;
  flag : boolean;
procedure kol_del(ch , res,step : longint);
begin
  if ch = res then
    begin
      flag := true;
      kol_step := step;
    end
  else
    if (ch < res) and (not flag) then
      begin
        kol_del(ch * 2, res, step+1);
        kol_del(ch * 3,res,step+1);
      end;
end;

var
  ch1 , ch2 : longint;
begin
  read(ch1,ch2);
  kol_del(ch1,ch2,0);
  if  flag then
    begin
      write(kol_step)
    end
  else
    write(-1);
end.

Var x, y, z, f:int64;
begin
  Read(x, y) ;
  If x=y then begin Writeln('0');exit;end;
  If y mod x>0 then begin writeln('-1');exit;end;
  If ((y mod 2>0) and (y mod 3>0)) and (y<>1)then begin writeln('-1');exit;end;
  Z:=y div x;
  Repeat
  If z mod 3 = 0 then begin
  Z:=z div 3;
  F:=f+1;
  End;
  If z mod 2 = 0 then begin
  Z:=z div 2;
  F:=f+1;
  End;
  If (z mod 2>0)and(z mod 3>0) and(z<>1) then begin writeln('-1');exit;end;
  Until z = 1;
  Writeln(f);
end.

var
  n, m, i, x: integer;

begin
  read(n, m);
  i := 0;
  if m mod n <> 0 then begin
    write(-1);
    exit
  end;
  x := m div n;
  while (x mod 2 = 0) or (x mod 3 = 0) do
  begin
    if (x mod 2 = 0) then
      x := x div 2
    else
      x := x div 3;
    i := i + 1;
  end;
  if (x = 1) then
    write(i)
  else
    write(-1);
end.

var
     k,n,m,q: integer;
begin
read(n,m);
if m mod n<>0 then begin print(-1); exit; end;
k:=m div n;
while k mod 3=0 do begin k:=k div 3; inc(q); end;
while k mod 2=0 do begin k:=k div 2; inc(q); end;
if k=1 then print(q) else print(-1);
end.

var n,m,dv,t:integer;
Begin
     readln(n,m);
     dv:=m div n;
     if m mod n <> 0 then writeln(-1)
     else
     Begin
          while dv mod 2 = 0 do begin dv:= dv div 2; t+=1; end;
          while dv mod 3 = 0 do begin dv:= dv div 3; t+=1; end;
          if dv = 1 then writeln(t) else writeln(-1);
     End;
End.

var ans,n,m,m1:integer;
begin
readln(n,m);
if (m mod n = 0) then
 m1:=m div n
else begin
 writeln(-1);
 exit;
end;

while (m1 <> 1) do
 if (m1 mod 2 = 0) then begin
  m1:=m1 div 2;
  inc(ans);
 end
 else
  if (m1 mod 3 = 0) then begin
   m1:= m1 div 3;
   inc(ans);
  end
  else begin
   writeln(-1);
   exit;
  end;
writeln(ans);
end.

var n,m,dem:int64;
begin
        readln(n,m);
        while (n < m) do
        begin
        if (m div n) mod 3 = 0 then n:=n*3
        else n:=n*2;
        inc(dem);
        end;
        if n <> m then writeln(-1)
        else writeln(dem);
        readln;
end.

var
  ans, n, m: longint;

begin
  ans:= 0;
  readln(n, m);
  if (m / n) = round(m div n) then begin
    while (m>n) and (((m div n) mod 3 = 0) or ((m div n) mod 2 = 0)) do begin
      if (m div n) mod 3 = 0 then n:= n * 3
      else if (m div n) mod 2 = 0 then n:= n * 2;

      ans:= ans+1;
    end;

    if m <> n then begin
      writeln('-1');
    end
    else writeln(ans);

  end
  else begin
    writeln('-1');
  end;
end.

Var  n,k,x,ans,m,f : Int64;
  begin
    Readln(n,m);
      if m mod n <> 0 then f:=1
                      else begin
                            k:= m div n;
                              while k>1 do
                              if k mod 3 = 0 then begin
                                                  k:= k div 3;
                                                  inc(ans);
                                                  end
                              else
                                if k mod 2 = 0 then begin
                                                    k:= k div 2;
                                                    inc(ans);
                                                    end
                                               else begin
                                                    f:=1;
                                                    Break;
                                                    end;
                           end;
         if f = 1 then writeln(-1)
                  else writeln(ans);

end.

var n,m,dem:int64;
procedure nhap;
begin
        read(m,n);
end;
function phantich(a:int64;b:int64):boolean;
var i:integer;
begin
        i:=2;
        if b mod a <>0 then
                exit(FALSE)
        else
                begin
                        b:=b div a;
                        while b mod 2 =0 do
                                begin
                                        b:=b div 2;
                                        dem:=dem+1;
                                end;
                        while b mod 3 =0 do
                                begin
                                        b:=b div 3;
                                        dem:=dem+1;
                                end;
                        if b>1 then
                                exit(FALSE)
                        else
                                exit(TRUE);

                end;

end;
procedure xuli;
begin
        dem:=0;
        if (n<m) then
                writeln(-1)
        else if (n=m) then
                writeln(0)
        else
                if phantich(m,n)=TRUE then
                        writeln(dem)
                else
                        writeln(-1);
        readln;
end;
procedure xuat;
begin
        writeln(dem);
        readln;
end;
begin
        nhap;
        xuli;
        //xuat;
end.

var m,n,i:longint;
begin
read(n,m);
if m mod n <>0 then
   begin
   write(-1);exit;
   end;
m:=trunc(m/n);i:=0;
while m mod 2=0 do
   begin
   i:=i+1;m:=trunc(m/2);
   end;
while m mod 3=0 do
   begin
   i:=i+1;m:=trunc(m/3);
   end;
if m<>1 then
   begin
   write(-1);exit;
   end
else write(i);
end.

var n,m,s:int64;
begin
	readln (n,m);
	if (m mod n <> 0) then writeln (-1)
    else begin
    	s:=0;
        m := m div n;
        while (m mod 3 = 0) do
        begin
        	m := m div 3;
            inc (s);
        end;
        while (m mod 2 = 0) do
        begin
        	m := m div 2;
            inc (s);
        end;
        //writeln (m);
        if (m <> 1) then writeln (-1) else
        writeln (s);
    end;

end.

var m,n,k,f:longint;
begin
 read(n,m);
 k:=0;
 if m mod n > 0 then write(-1) else
 begin
  m:= m div n;
  while m mod 3 =0 do begin inc(k);m:=m div 3; end;
  while m mod 2 =0 do begin inc(k);m:=m div 2;
  end;
  if m=1 then write(k) else write(-1)
 end;
end.

var a, b, c:LongInt;
begin
read(a, b);
c:=0;
if b mod a <> 0 then
begin
  write(-1);
  Exit();
end;
a := b div a;
while a mod 2 = 0 do
begin
  a := a div 2;
  inc(c);
end;
while a mod 3 = 0 do
begin
  a := a div 3;
  inc(c);
end;
if a=1 then write(c)
else write(-1);
end.

const fi='';
      fo='';
var f:text;
    n,m,s,d:int64;

begin
  assign(f,fi);reset(f);
  readln(f,n,m);

  assign(f,fo);rewrite(f);
  s:= m div n;
  if s * n <> m then write(f,-1)
  else
  if s=1 then write(f,0)
  else
  begin
    while (s <> 1) do
        begin
          if s mod 3 = 0 then
          begin
            inc(d);
            s:=s div 3;
          end
          else
          if s mod 2 = 0 then
          begin
            inc(d);
            s:=s div 2;
          end;
          if (s mod 2 <> 0 ) and (s mod 3 <> 0) and (s <> 1) then
          begin
            d:=-1;
            break;
          end;
        end;
    write(f,d);
  end;
  close(f);
end.

uses math;
var
    n,m,s,k : longint;
begin
    readln(n,m);
    if m mod n > 0 then
        begin writeln(-1); halt; end;
    m := m div n;
    while 2**s <= m do begin
        k:=0;
        while 2**s * 3**k < m do
            k += 1;
        if 2**s * 3**k = m then begin
            writeln(s+k);
            halt;
        end;
        s += 1;
    end;
    writeln(-1);
end.

var
    f,m,n,ans : longint;
begin
    readln(n,m); f := m div n;
    if m mod n > 0 then begin write(-1); halt; end;
    while f mod 2 = 0 do begin ans += 1; f := f div 2; end;
    while f mod 3 = 0 do begin ans += 1; f := f div 3; end;
    if f > 1 then ans := -1; write(ans);
end.

Var n,m,rem,count:Longint;
Begin
    Readln(n,m);
    If m mod n <> 0 then writeln(-1)
    Else begin
        rem:=m div n;
        While rem mod 2 = 0 do begin
            inc(count); rem:=rem div 2;
        End;
        While rem mod 3 = 0 do begin
            inc(count); rem:=rem div 3;
        End;
        If rem=1 then writeln(count) else writeln(-1);
    End;
End.

program ProblemA;
var a,b:longint;
    res:longint;
begin
        read(a,b);
        if b mod a <> 0 then
        begin
                write(-1);
                exit;
        end;
        a:=b div a;
        res:=0;
        while a mod 2 = 0 do
        begin
                a:=a div 2;
                inc(res);
        end;

        while a mod 3 = 0 do
        begin
                a:=a div 3;
                inc(res);
        end;
        if a=1 then write(res) else write(-1);
end.

const fi='';
      fo='';
var f,g:text;
    m,n,kq,count,count1:longint;
procedure inp;
begin
  assign(f,fi);
  reset(f);
  read(f,n,m);
  close(f);
end;

procedure solve;
begin
 if (m mod n<>0) then count:=-1
  else
   begin
    kq:=m div n;
    While (kq mod 2=0) do
       begin
        kq:=kq div 2;
        inc(count);
       end ;
    While (kq mod 3=0) do
        begin
         kq:=kq div 3;
         inc(count);
        end;
     end;
    if (kq<>1) then count:=-1;
end;
procedure output;
begin
  assign(g,fo);
  rewrite(g);
  writeln(g,count);
  close(g);
end;

begin
  inp;
  solve;
  output;
end.

program coba;

var
	n,m,x,y : longint;
begin
	n := 0;
	m := 0;
	readln(n,m);
	{writeln(n, ' dan ', m);}
	if (m mod n = 0) then begin
		n := m div n;
		x := 0;
		y := 0;
		while (n mod 2 = 0) do begin
			n := n div 2;
			x := x + 1;
		end;

		while (n mod 3 = 0) do begin
			n := n div 3;
			y := y + 1;
		end;

		if (n = 1) then begin
			writeln(x+y);
		end else begin
			writeln(-1);
		end;
	end else begin
		writeln(-1);
	end;

end.

var n,m,k,x,a,f,c:longint;
begin
{assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);}
readln(n,m);
if n=m then begin write(0);halt;end;
if n>m div 2 then begin write(-1);halt;end;
x:=m div n;
a:=x;
k:=0;
c:=m mod n;
if c>0 then begin write(-1);halt;end;
while a<>1 do
 begin
 if (a mod 2<>0) and (a mod 3<>0) then begin write(-1);break;end;
  if a mod 2=0 then begin a:=a div 2;k:=k+1;end;
  if a mod 3=0 then begin a:=a div 3;k:=k+1;end;

 end;
if a=1 then write(k);
end.

var a,b:longint;
best:integer;
procedure ok(cur, pts:longint);
begin
        if cur=b then begin
                if (best=-1) or (best>pts) then best:=pts;
        end else if cur<b then begin
                ok(cur*2, pts+1);
                ok(cur*3, pts+1);
        end;
end;
begin
        readln(a, b);
        best:=-1;
        ok(a, 0);
        writeln(best);
end.

var a,b,x,k:int64;

begin

//assign(input,'input.txt'); reset(input);
//assign(output,'output.txt'); rewrite(output);

readln(a,b);

if b mod a<>0 then begin writeln(-1);halt;end;

x:=b div a;

while x>1 do
if x mod 2=0 then begin x:=x div 2; inc(k); end
             else if x mod 3=0 then begin x:=x div 3; inc(k); end
             else begin writeln(-1);halt;end;

writeln(k);

end.

const fi='input.txt';
      fo='output.txt';
var a:array[1..100005,1..2] of longint;
    res,n,m:longint;

function Xuli:longint;
var i,countn,countm:longint;
Begin
        for i:=2 to trunc(sqrt(m)) do begin
                countn:=0; countm:=0;
                while (n mod i=0) or (m mod i=0) do begin
                        if ((i=2) or (i=3)) and (n mod i=0) then inc(countn);
                        if ((i=2) or (i=3)) and (m mod i=0) then inc(countm);
                        if (i<>2) and (i<>3) and ((n mod i<>0) or (m mod i<>0)) then exit(-1);
                        if n mod i=0 then n:=n div i;
                        if m mod i=0 then m:=m div i;
                end;
                if (i=2) or (i=3) then if countm>=countn then res:=res+countm-countn else exit(-1);
        end;
        if (n=1) and ((m=2) or (m=3)) then exit(res+1);
        if (n<>m) then exit(-1);
        exit(res);
End;

BEGIN
        //assign(input,fi); reset(input);
        //assign(output,fo); rewrite(output);

        read(n,m);
        write(Xuli);

        //close(input);
        //close(output);
END.

var
  a,b,c,n : longint;
begin
  readln(a,b);
  if (b mod a <> 0) then
  begin
    writeln(-1);
    exit;
  end;
  c := b div a;
  while (c > 1) do
  begin
    if (c mod 2 <> 0)and(c mod 3 <> 0) then
      break;
    if (c mod 2 = 0) then
    begin
      c := c div 2;
      inc(n);
    end;
    if (c = 0) then
      break;
    if (c mod 3 = 0) then
    begin
      c := c div 3;
      inc(n);
    end;
  end;
  if (c = 1) then
    writeln(n)
  else
    writeln(-1);
end.

var
  n,m,a : real;
  b,kol : longint;
begin
    read(n,m);
    a:=m/n;
    b:=trunc(a);
    if trunc(m) mod trunc(n) <> 0 then begin write('-1'); halt; end;
    while (b mod 2 = 0) or (b mod 3 = 0) do
     begin
      if b mod 2 = 0 then begin b:=b div 2; inc(kol); end
                     else if (b mod 3 = 0) then begin b:=b div 3; inc(kol); end;
     end;
    if b <> 1 then write('-1')
              else write(kol);

end.

var
	m,n,k,rez:longint;

function nod(m,n:longint):longint;
begin
	while m<>n do
	begin
		if m>n then
			m:=m-n
		else n:=n-m;
	end;
	nod:=m;
end;

begin
	read(m,n);
	k:=nod(m,n);
	rez:=-1;
	if k=m then begin
		n:=n div k;
		rez:=0;
		while (n>1) and ((n mod 2 =0) or (n mod 3 =0)) do
		begin
			if n mod 2 =0 then
			begin
				n:=n div 2;
				rez+=1;
			end;
			if n mod 3 =0 then
			begin
				n:=n div 3;
				rez+=1;
			end;
		end;
	end;
	if n=1 then
		writeln(rez)
	else writeln(-1);
end.

var
n,m,kol:int64;
begin
readln(n,m);
if m mod n<>0 then writeln(-1)
else begin
    m:=m div n;
    while m mod 2 =0  do begin
    m:=m div 2;
    inc(kol);
    end;
    while m mod 3=0 do begin
     m:=m div 3;
     inc(kol);
    end;
    if m<>1 then writeln(-1)
    else
    writeln(kol);
end;
end.

var a,b,kq,c,i :longint;
BEGIN
    read(a,b);  kq:=0;
    if b mod a <>0 then kq:=-1;
    if a=b then kq:=0;
    if b mod a=0 then
       begin
           c:=b div a; i:=2;
           while c<>1 do
               begin
                   while c mod i=0 do begin c:=c div i; inc(kq); end;
                   inc(i);
                   if i>4 then begin kq:=-1; break; end;
               end;
       end;
       write(kq);
END.

function count_moves(n,m:longint):longint;
var d,k:longint;
begin
    d:= m div n;
    k:=0;
    while d mod 2 = 0 do begin
        d:=d div 2;
        inc(k);
    end;
    while d mod 3 = 0 do begin
        d:=d div 3;
        inc(k);
    end;
    if d = 1 then
        count_moves:=k
    else
        count_moves:=-1;
end;

var n,m:longint;
begin
    read(n,m);
    if m mod n <> 0 then
        writeln('-1')
    else
        writeln(count_moves(n,m));
end.

var n,m,k:longint;
begin
 // assign(input,'input.txt');
 // assign(output,'output.txt');
 // reset(input);
 //rewrite(output);
  read(n,m);
  if n=m then begin writeln(0);halt;end;
  if m mod n <>0 then begin write(-1);halt;end;
  n:=m div n;
  while n mod 3=0 do
  begin
    n:=n div 3;
    inc(k);
  end;
  while n mod 2 =0 do
  begin
    n:=n div 2;
    inc(k);
  end;
  if n<>1 then write(-1) else write(k);
end.

var n,m,k:longint;
begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
read(n,m);
if m mod n<>0 then begin write(-1);halt;end;
n:=m div n;
while n mod 3=0 do begin
inc(k);n:=n div 3;end;
while n mod 2=0 do begin
inc(k);n:=n div 2;end;
if n<>1 then write(-1) else write(k);
end.

var
  n,m,ans,fofifka : longint;
begin
  read(n,m);
  if m mod n > 0 then
  begin
    write(-1);
    halt;
  end;
  fofifka:=m div n;
  while fofifka mod 2 = 0 do begin
    inc(ans);
    fofifka:=fofifka div 2;
  end;
  while fofifka mod 3 = 0 do begin
    inc(ans);
    fofifka:=fofifka div 3;
  end;
  if fofifka = 1 then write(ans)
  else write(-1);
end.

var a,b:longint;d:array [1..2] of longint;
begin
  readln(a,b);
  if (b mod a<>0)or(b<a) then writeln(-1)
  else begin
    if b=a then writeln(0)
    else begin
      b:=b div a;
      while b<>1 do begin
        if b mod 2=0 then begin
          b:=b div 2;
          inc(d[1]);
        end
        else if b mod 3=0 then begin
          b:=b div 3;
          inc(d[2]);
        end
        else begin
          writeln(-1);
          exit;
        end;
      end;
      writeln(d[1]+d[2]);
    end;
  end;
end.

const fi='1141A.inp';
      fo='1441A.OUT';
 var f:text ; n,m,dem2,dem3,s2m,s3m,s2n,s3n:longint;

 procedure nhap;
  begin
  //  assign(f,fi);
   // reset(F);
    readln({f,}n,m);
 // close(f);
  end;

 procedure demsocua(x:longint) ;
   begin
    dem2:=0;
    dem3:=0;
      while( x mod 3 =0 )and(n>0) do
      begin
        inc(dem3);
        x:=x div 3 ;
      end;

     while( x mod 2 =0 )and(n>0) do
      begin
        inc(dem2);
        x:=x div 2 ;
      end;
     if x<= 1 then s3n :=1;

    end;

 procedure xuli;
   begin
    // demsocua(n);
   //  s2n:=dem2;
   //  s3n:=dem3;
     demsocua(m div n );
     s2m:=dem2;
     s3m:= dem3;
      s2n:=0;
     s2m:=s2m{-s2n};
     s3m:=s3m{-s2n};
     if (s2m+s3m =0 )and( n =m) then s2n:=1;
   end;

 procedure xuat;
   begin
  // assign(f,fo);
   //  rewrite(F);
        if s2n=1 then writeln({f,}0)else

     if(s3n=1 )and(m mod n=0) and((m div n mod 2 =0) or( m div n mod 3 =0))  then writeln({f,}s2m+s3m)

     else
       begin

        writeln({f,}-1);
       end;
//     close(F);
   end;

 begin
   nhap;
   xuli;
   xuat;
 end.

var a,b:longint;
    res:longint;
begin
        read(a,b);
        if b mod a <> 0 then
        begin
                write(-1);
                exit;
        end;
        a:=b div a;
        res:=0;
        while a mod 2 = 0 do
        begin
                a:=a div 2;
                inc(res);
        end;

        while a mod 3 = 0 do
        begin
                a:=a div 3;
                inc(res);
        end;
        if a=1 then write(res) else write(-1);
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else
        begin
            x:=m div n;
                while x mod 3 = 0 do
                    begin
                        inc(dem);
                        x:=x div 3;
                    end;
                while x mod 2 = 0 do
                    begin
                        inc(dem);
                        x:=x div 2;
                    end;
         if x = 1 then writeln(dem)  else write(-1);
        end;
end.

program game23;
var
n,m,i,d,way:longint;
begin
        readln(n,m);
        if m mod n <>0 then
        begin
                writeln(-1);
                halt(0);
        end;
        d:=m div n;
        i:=2;
        way:=0;
        while (i<=3) and (d>1) do
        begin
                if d mod i=0 then
                begin
                        d:=d div i;
                        inc(way);
                end
                else inc(i);
        end;
        if d=1 then writeln(way)
        else writeln(-1);
end.

const fi='1141A.inp';
        fo='1141A.out';
var n,m,s:int64;
    x:array[0..10000001] of longint;
procedure nhap;
        begin
        read(n,m);
        s:=n;
        end;
procedure chinhhoplap(i:longint);
var j:longint;
        begin
        for j:=2 to 3 do
                begin
                x[i]:=j;
                s:=s*x[i];
                if s=m then
                        begin
                        write(i);
                        halt;
                        end;
                if s<m then chinhhoplap(i+1);
                s:=s div x[i];
                end;
        end;
procedure xuli;
        begin
        write(-1);
        end;
begin
//assign(input,fi); reset(input);
//assign(output,fo); rewrite(output);
nhap;
if n=m then     begin
                write(0);
                halt;
                end;
chinhhoplap(1);
xuli;
end.

program polycarp;

var
n,m:longint;
faktor:longint;
ans:longint;
i:longint;

begin
	readln (n, m);
	if (n = m) then
	begin
		writeln (0);
	end else if (n > m) then
	begin
		writeln (-1);
	end else
	begin
		if (m mod n <> 0) then
		begin
			writeln (-1);
		end else
		begin
			faktor := m div n;
			ans := 0;
			for i := 2 to 3 do
			begin
				while (faktor mod i = 0) do
				begin
					faktor := faktor div i;
					inc (ans);
				end;
			end;
			if (faktor > 1) then
			begin
				writeln (-1);
			end else
			begin
				writeln (ans);
			end;
		end;
	end;
end.

program q;
  var i,k,n,m,o:integer;
      z:real;
begin
  read(n,m);
  o:=1;
  z:=m/n;
  if z<>round(z) then
    k:=-1
  else begin
    if z/6=round(z/6) then
    begin
    while o<>0 do
    begin
      z:=z/6;
      if z/6<>round(z/6) then begin
      i:=i+1;
      break;exit;
      end;
      i:=i+1;
    end;
    k:=i*2;
    if (z/2<>round(z/2)) and (z/3<>round(z/3)) then begin
    k:=k;end
    else begin
      if z/2<>round(z/2) then
      begin
      while z<>1 do begin
      z:=z/3;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end
      else
      if z/3<>round(z/3) then
      begin
      while z<>1 do begin
      z:=z/2;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end;
    end;
    end
    else
    if (z/2<>round(z/2)) and (z/3<>round(z/3)) then begin
    k:=k;end
    else begin
      if z/2<>round(z/2) then
      begin
      while z<>1 do begin
      z:=z/3;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end
      else
      if z/3<>round(z/3) then
      begin
      while z<>1 do begin
      z:=z/2;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end;
    end;
    end;
  if n=m then k:=0;
  if z<>1 then k:=-1;
  writeln(k);
end.

  var a:integer;
      b,n,m:real;
begin
  read(n,m);a:=0;b:=m/n;
  if b=1 then a:=0
  else begin
    if round(b)<>b then a:=-1
    else begin
      if b/6=round(b/6) then
        repeat
          b:=b/6;a:=a+2;
        until b/6<>round(b/6);
      if b/3=round(b/3) then
        repeat
          b:=b/3;a:=a+1;
        until b/3<>round(b/3);
      if b/2=round(b/2) then
        repeat
          b:=b/2;a:=a+1;
        until b/2<>round(b/2);
      if b<>1 then a:=-1;
    end;
  end;
  writeln(a);
end.

var
n,m,q,c:int64;
begin
  read(n, m);
  if n = m then begin write(0); halt; end;
  if n > m then begin write(-1); halt; end;
  if frac(m / n) <> 0 then begin write(-1); halt; end;
  q := m div n;
  while q mod 2 = 0 do
  begin
    inc(c);
    q := q div 2;
  end;

  while q mod 3 = 0 do
  begin
    inc(c);
    q := q div 3;
  end;
  if q <> 1 then begin write(-1); halt; end;
  write(c);

end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, m, a,s, i : longint;
begin
  read(n, m);
  s := 0;
  if (m mod n <> 0) then
    writeln(-1)
  else
    begin
      a := m div n;
      for i := 1 to a do
      begin
        if (a = 1) then
          begin
            writeln(s);
            break;
          end
        else if (a mod 2 = 0) then
          a := a div 2
        else if (a mod 3 = 0) then
          a := a div 3
        else
          begin
          writeln(-1);
          break;
          end;
        inc(s);
      end;
    end;
  readln;
  readln;
end.

var
  n, m, x: longint;
begin
  read(n, m);
  if m mod n <> 0 then
    write(-1)
  else begin
    n := m div n;
    x := 0;
    while n > 1 do begin
      if n mod 2 = 0 then
        n := n div 2
      else if n mod 3 = 0 then
        n := n div 3
      else begin
        write(-1);
        halt;
      end;
      inc(x);
    end;
    write(x);
  end;
end.

                      const eps=0.0000000001;  besk1=200000008;  nnn=4600000;
            besk=80000000000000000;
              modul=1000000007{998244353} ;

   label 22,1,2,3,4,33;
    type    real=extended;   double=extended;
    mas=array [-300000..nnn] of int64;   point=record x,y,z:real;end;
    zap=record p1,p2,p3,p4 :int64;end; zap1=^zap;
    mass=array[-10000..nnn] of longint;mass1=^mass;
    matr=array[-10..200,-10..200] of int64;
    matr1=^matr;
    var izb,kol,oldpp,t,pos1,pos2,r,razm,kollad,leftx,rightx,lefty,righty,num,dirx,diry,oldii,obr,koludal,maxdl,mindl,tmin,aaa,bbb,ccc,kolst,kollek,aa,bb,t1,t2,kolx,koly,ans,ans1,ans2,dl,posleft,posright,n,xxx,yyy,dx,dy,m,w,h,jjj,l,cc,mnog,nn,t3,t4,t5,t6,posmax,tmax,kold,kk,j2,ttt,ppp,pmin,pmax,limit,posi,posj,diam,oldt,ot1,time,pos,ll,posx,posy,dloch,newdl,oldn,oldm,ost,ot,p1,p2,p3,p4,znamzz,kolperiod,znam,tperiod,start,finish,kol1,kol2,lavel,p0,jj,ii,ogr,q,p,pp,tt,oldtt,qq,qq1,qq2,q1,q2,imax,nm,smin,cen:int64;
   istart,jstart,ifinish,jfinish,k,i2,i3,step2,k1,k2,pp0,mask,i1,j1,step1, step, i,j:longint;
   zagr,xx,yy,kolizm,test,zz,predok,hesh,yold,x1,x0,d,b,a,zsum,y,x,z:array[-200000..1535002] of int64;
    otmat,amat,bmat,cmat:matr;
   gr1,gr2,gr:array[-1..2000,-1..2000] of boolean;
    dd,ddr:array[-1..1502,-1..1502] of int64;
    prob1,prob2,prob3,pr1new,pr2new,pr3new,pr1,pr2,pr3:zap;
    iii,oldpos,finot,vv,znak:int64;
    aold:mas;  anss:zap;
    sss,ss,ss1,ss2:string;
    newstr,str:array[-1000..200000] of string;
    lll,rrr,v,dist,alpha,phi:extended;
    masmatr:array[-1..101,-1..101,-1..101] of int64;
    zzz,ddnew,koef,polex,ddsum,ddl:array[-1..4,-1..1300000] of int64;
    dpos,sum,ppos,mma,ma:array[-1000..20005,-1..50] of int64;
    xleft,xright,xplus,xminus,px,py,xc,yc,xxc,yyc:extended;
    del:array[-1..100001,-1..20] of longint;
    z0,y1,z1,z2,kolnul,fact,next,prev,s,name,dnew, voltay,c,day:mas;
    zzzz:array[0..125000000] of byte;
    aaaa,bbbb,x2:extended;
    delit:array[0..100000,0..20] of longint;
    pole:array[-10..2002,-10..2005] of char;
    ddd,matrr,ppole,dd3,dd4:array[-10..2000,-10..2200] of int64;
    ddnnew:array[-1..12,-1..12,0..200,0..3] of int64;
   newzan,marked,zanx,zany:array[-1000000..2000000] of boolean;
    ssold,sss1,sss2:string;
    def,stepen,koled,och,newoch,place,newnabor,stoim,nabor:array[-10..500005] of int64;
     zanleft,zanright,zan,fl:array[-100000..10000000] of boolean;
     dost:array[-1..210,-1..210,0..3,0..3] of int64;
    sss12:string;ch,ch1,ch2:char;
    pr,heshh,numright,dr: array [-10..206,-1..16] of int64;
    s3,s2,s1:array[-100..500000] of string;

    grr :array [-10..2010,-10..2010] of extended;
    maxpr:array[0..nnn] of string;
     tx,ty,vel,ott,v0,v1,s0,t0:int64;
     zv:point;
     left,right,ugol,rot,temp,sumtemp,vol,newx,newy:array[-1000..400000] of int64;
     mash,sqx,sqy,ottt:extended;
     xxxx,yyyy,yot:double;
       flag2,flag,flag1:boolean;

  function max(a,b:int64):int64;
begin max:=a; if b>a then max:=b;   end;

  function min(a,b:int64):int64;
begin min:=a; if b<a then min:=b;   end;

procedure udal(p:longint);
begin  next[prev[p]]:=next[p]; prev[next[p]]:=prev[p]; end;

      function nod(x,y:int64):int64; var z:int64;
begin   repeat  z:=x mod y;x:=y;y:=z;   until  z=0 ;  nod:=x;end;

     function summ(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p2+xx.p2*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=t1 div tt mod modul;
    t2:=t2 div tt mod modul;
    ott.p1:=t1; ott.p2:=t2;

   summ:=@ott;
 end;

     function prod(xx,yy:zap):zap1;
var t1,t2,tt:int64; ott:zap;
 begin
    t1:=(xx.p1*yy.p1) mod modul;
    t2:=(xx.p2*yy.p2) mod modul;
    tt:=nod(t1,t2);
    t1:=(t1 div tt) mod modul;
    t2:=(t2 div tt) mod modul;
    ott.p1:=t1; ott.p2:=t2;

   prod:=@ott;
 end;

    function al(x,y:real):real; var t0,t1,p,s0,s1:real;
 begin
     if (x>0)and(y>=0) then p:=arctan(y/x);
     if x<0 then p:=pi+arctan(y/x);
     if(x>0)and(y<0) then p:=2*pi+arctan(y/x);
     if x=0 then if (y>=0) then p:=pi/2 else p:=3*pi/2;
     al:=p{/pi*180};
 end;

function degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k shr 1); degg:=p*p mod modul;   end;

end;

function prost(tt:int64):boolean;
var aa:extended; i,p,t:longint;  fl:boolean;
 begin
   fl:=true;
   aa:=tt;
   aa:=sqrt(aa);
   t:=round(aa);
   for i:=2 to t do if tt mod i=0 then begin fl:=false; break; end;

   prost:=fl;
 end;

function arcsin(pp:extended):extended;
var tt:extended;
begin
   if (abs(pp)<=1+eps)and (abs(pp)>=1-eps)
   then if pp>0 then arcsin:=pi/2 else arcsin:=-pi/2;
   tt:=1-pp*pp;
   if abs(tt)>=0 then
     begin
        arcsin:=arctan(pp/sqrt(tt));

     end;

end;

function gl(ch:char):boolean;
begin
 if (ch='o')or (ch='a')or(ch='e')or(ch='u')or(ch='i') then gl:=true else gl:=false;

end;

function zaprmin(left,right:longint):int64;
begin
   if left>=right then zaprmin:=y[left] else
     begin
        if not odd(left) and odd(right) then zaprmin:=zaprmin(left shr 1,right shr 1) else
         begin
          if odd(left) then zaprmin:=min(zaprmin(left+1,right),y[left]);
          if not odd(right) then zaprmin:=min(zaprmin(left,right-1),y[right]);

         end;

     end;

end;

 function ff(gr:int64):boolean;

var ot,aa,bb,cc:int64; fl:boolean;
begin
  aa:=1; bb:=m;  cc:=aa; fl:=false;
  while abs(aa-bb)>1 do
    begin
       cc:=(aa+bb) shr 1;
       if y[cc]<gr then aa:=cc else bb:=cc;

    end;
   writeln(' cc= ',cc);
   cc:=min(cc+3,m+1);
   repeat dec(cc) until y[cc]<=gr;
   if y[cc]=gr then fl:=true else fl:=false;
  ff:=fl;
end;

begin
   { assign (input,'D:\Projects\input.txt');reset(input);
      assign (output,'D:\Projects\output.txt'); rewrite(output);
    }
      readln(p1,p2);
      while p1 mod 2=0 do
        begin
          p1:=p1 div 2;
          inc(x[2]);
        end;
          while p1 mod 3=0 do
        begin
          p1:=p1 div 3;
          inc(x[3]);
        end;

         while p2 mod 2=0 do
        begin
          p2:=p2 div 2;
          inc(y[2]);
        end;
          while p2 mod 3=0 do
        begin
          p2:=p2 div 3;
          inc(y[3]);
        end;

     {  writeln(p1,' ',p2);
       for i:=2 to 3 do writeln(x[i],' ',y[i]);
      } if p1<>p2 then begin writeln(-1); goto 1;  end;
       if x[2]>y[2] then begin writeln(-1); goto 1;  end;
       if x[3]>y[3] then begin writeln(-1); goto 1;  end;
       writeln(y[2]+y[3]-(x[2]+x[3]));

1:             close(output);
end.

program solution;
{$APPTYPE CONSOLE}

var
  i, n, m, counter: Int64;

begin
  readln(n, m);
  counter := 0;
  if m mod n = 0 then
    begin
    if n <> m then
      begin
        m := m div n;
        while m mod 2 = 0 do
          begin
            m := m div 2;
            inc(counter);
          end;
        while m mod 3 = 0 do
          begin
            m := m div 3;
            inc(counter);
          end;
        if m = 1 then
          writeln(counter)
        else
          writeln(-1);
      end
    else
      writeln(0);
    end
      else
      writeln(-1);
end.

var n,m,k:longint;
begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
readln(n,m);
if n=m then begin writeln(0);halt;end;
if n>m div 2 then begin writeln(-1);halt;end;
if m mod n<>0 then begin write(-1);halt;end;
m:=m div n;
while m mod 3=0 do
begin
m:=m div 3;
inc(k);
end;
while m mod 2=0 do
begin
m:=m div 2;
inc(k);
end;
if m=1 then writeln(k) else writeln(-1);
end.

program jvr;
uses math;
var
    i, a, answer, n, c, p, s, m : int64;
    ok :boolean;
begin
    readln(n,m);
    if (m = n) then write(0)
    else if (m mod n <> 0) then write(-1)
    else
    begin
    a := m div n;
    p := 2;

    ok := false;
    while (a > 1) do begin
     c := 0;
    while (a mod p = 0) do begin
    inc(c);
    a := a div p;
    end;
    if (c <> 0) then
     begin
     if (p = 2) then s := s + c;
     if (p = 3) then s := s + c;
     if (p <> 2) and (p <> 3) then ok := true;
     end;
    inc(p);
    if (p * p > a) then p :=a ;
    end;
    if (ok = false) then write(s)
    else write(-1);
    end;

end.

program game23;
var
        n,m,x:longint;
begin
        read(n,m);
        while(n<m)do
        begin
                if((m div n)mod 3=0)then
                n:=n*3
                else
                n:=n*2;
                inc(x);
        end;
        if(n<>m)then
        writeln(-1)
        else
        writeln(x);
end.

var
    n,m,min,max:longint;

procedure sc(x,num:longint);
begin

        if x=m then
                begin

                if min>num then min:=num;
                exit;

                end;

        if x>m then exit;

        sc(x*2,num+1);
        sc(x*3,num+1);

end;
begin

    readln(n,m);
    min:=maxlongint;

    sc(n,0);

    if min=maxlongint then min:=-1;
    writeln(min);

end.

program NA3;
var
        n, m, i, cnt: LongInt;

begin
        ReadLn (n, m);

        cnt := 0;
        i := 2;

        while n <> 1 do
        begin
                if n mod i = 0 then
                        if m mod i = 0 then
                        begin
                                m := m div i;
                                n := n div i;
                        end
                        else
                        begin
                                WriteLn (-1);
                                Exit;
                        end
                else
                        Inc (i)
        end;

        while m <> 1 do
        begin
                if m mod 2 = 0 then
                        m := m div 2
                else
                        if m mod 3 = 0 then
                                m := m div 3
                        else
                        begin
                                WriteLn (-1);
                                Exit
                        end;
                Inc (cnt)
        end;

        WriteLn (cnt);
end.

//uses crt;
const fi='A.inp'; fo='A.out';
var n,m,t,s:longint;
    ok:boolean;

procedure input;
var f:text;
 begin
   {assign(f,fi);
   reset(f);}
   //clrscr;
   readln(n,m);
   //close(f);
 end;

procedure run;
 begin
   ok:=false;
   if m=n then
    begin
      ok:=true;
      exit;
    end;
   if m mod n<>0 then exit;
   if (m mod 2<>0) and (m mod 3<>0) then exit;
   t:=0;
   m:=m div n;
   while m mod 2=0 do
    begin
      inc(t);
      m:=m div 2;
    end;
   while m mod 3=0 do
    begin
      inc(t);
      m:=m div 3;
    end;
   if m=1 then ok:=true;
 end;

procedure output;
var f:text;
 begin
   {assign(f,fo);
   rewrite(f);}
   if ok then writeln(t)
   else writeln('-1');
   //readln
   //close(f);
 end;

BEGIN
  input;
  run;
  output;
END.

  var i,j,n,m,ans:longint;
   begin
    read (n,m);

    if m mod n <> 0 then write (-1)
     else
    begin
     m:=m div n;

     while m mod 2 = 0 do
      begin
       m:=m div 2;
       inc (ans);
      end;

     while m mod 3 = 0 do
      begin
       m:=m div 3;
       inc (ans);
      end;

      if m=1 then write (ans)
       else write (-1);

    end;

   end.

var n,m,c: longint;

procedure backTracking(k: longint);
var i: longint;
begin
        for i:=2 to 3 do begin
                n:=n*i;
                inc(c);
                if n=m then begin
                        write(c);
                        halt;
                end
                else if n<m then backTracking(k+1);
                n:=n div i;
                dec(c);
        end;
end;

begin
    //    assign(input,'test.inp'); reset(input);
    //    assign(output,'test.out'); rewrite(output);
        readln(n,m);
        c:=0;
        if n=m then begin
                write(0);
                halt;
        end;
        backTracking(1);
        write(-1);
end.

var n,m,x,a,k,c:longint;
begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
readln(n,m);
if n=m then begin writeln(0);halt;end;
if n>m div 2 then begin writeln(-1);halt;end;
x:=m div n;
a:=x;
k:=0;
c:=m mod n;
if c>0 then begin write(-1);halt;end;
while a<>1 do
begin
  if (a mod 2<>0)and(a mod 3<>0)then begin write(-1);break;end;
  if a mod 2=0 then begin a:=a div 2;inc(k);end;
  if a mod 3=0 then begin a:=a div 3;inc(k);end;
end;
if a=1 then writeln(k);
end.

label 1;
var n,m:int64;
e,r:integer;
begin
r:=-1;
readln(n,m);
if n=m then begin writeln('0');goto 1; end;
if (n=1) and (m mod 3<>0) and (m mod 2<>0) then begin writeln('-1'); goto 1; end;
if (m mod n=0) then r:=0 else begin writeln('-1'); goto 1; end;
e:=m div n;
while ( e mod 2=0) do
begin
e:=e div 2; r:=r+1;
end;
e:=m div n;
while ( e mod 3=0) do begin e:=e div 3; r:=r+1; end;
writeln(r);
1: end.

label 1;
var n,m:int64;
e,r:integer;
begin
r:=-1;
readln(n,m);
if (n=1) and (m mod 3<>0) and (m mod 2<>0) then begin writeln('-1'); goto 1; end;
if n=m then begin writeln('0');goto 1; end;
if (m mod n=0) then r:=0 else begin writeln('-1'); goto 1; end;
e:=m div n;
while ( e mod 2=0) do
begin
e:=e div 2; r:=r+1;
end;
e:=m div n;
while ( e mod 3=0) do begin e:=e div 3; r:=r+1; end;
writeln(r);
1: end.

label 1;
var q,s,n,m:int64;
i,d,e,r:integer;
begin
r:=-1; d:=-1;
readln(n,m);
if n=m then begin writeln('0');goto 1; end;
if (n=1) and (m mod 3<>0) and (m mod 2<>0) then begin writeln('-1'); goto 1; end;
if (m mod n=0) then begin r:=0; d:=0; end else begin writeln('-1'); goto 1; end;
e:=m div n;
while ( e mod 2=0) do
begin
e:=e div 2; d:=d+1;
end;
e:=m div n;
while ( e mod 3=0) do begin e:=e div 3; r:=r+1; end;
s:=1; q:=1;
for i:=1 to d do begin s:=s*2; end;
for i:=1 to r do begin q:=q*3; end;
if q+s<>m then writeln('-1') else writeln(r+d);
1: end.

label 1;
var n,m:int64;
e,r:integer;
begin
r:=-1;
readln(n,m);
if n=m then begin writeln('0');goto 1; end;
if (m mod n=0) then r:=0 else begin writeln('-1'); goto 1; end;
e:=m div n;
while ( e mod 2=0) do
begin
e:=e div 2; r:=r+1;
end;
e:=m div n;
while ( e mod 3=0) do begin e:=e div 3; r:=r+1; end;
writeln(r);
1: end.

var m,a,n,i:integer;
begin
read(n,m);
if (m mod n <>0)or((m mod 2 <>0) and (m mod 3<>0)) then begin
write(-1);
halt(0);
end;
if m=n then begin
write(0);
halt(0);
end;
a:=m div n;
while a<>1 do begin
if a mod 3 =0 then begin a:=a div 3;
inc(i);
end
else
if a mod 2 =0 then begin a:=a div 2;
inc(i);
end;
end;
write(i);
end.

var n,sum:int64;
    k:integer;
    n_str,sum_str:string;
begin
readln(n);
n_str:=IntToStr(n);
sum:=0;
for var i:=1 to length(n_str) do
 begin
 if (n_str[i] = '4') or (n_str[i] = '7') then
 sum:=sum+StrToInt(n_str[i])
 end;
if sum = 0 then
 begin
 writeln('NO');
 exit;
 end;
sum_str:=IntToStr(sum);
k:=0;
for var i:=1 to length(sum_str) do
 begin
 if (n_str[i] = '4') or (n_str[i] = '7') then
 K:=k+1
 end;
if k = length(sum_str) then
 writeln('YES')
else
 writeln('NO')
end.

begin
	var n, m, ans:integer;
	readln(n, m);
	ans := 0;

	while m mod 3 = 0 do
	begin
		ans += 1;
		m := m div 3;
	end;

	while m mod 2 = 0 do
	begin
		ans += 1;
		m := m div 2;
	end;

	while n mod 3 = 0 do
	begin
		ans -= 1;
		n := n div 3;
	end;

	while n mod 2 = 0 do
	begin
		ans -= 1;
		n := n div 2;
	end;

	if n <> m then
		ans := -1;

	writeln(ans);
end.

var a,b,k,p:integer;
begin
read(a,b);
if b mod a <> 0 then begin
write(-1);
halt(0);
end;
if (b mod 2 <> 0) and (b mod 3 <>0) and (a<>b) then begin
write(-1);
halt(0);
end;
k:=b div a;
while k<>1 do begin
if k mod 3 = 0 then begin
inc(p);
k:=k div 3
end
else begin
inc(p);
k:=k div 2;
end;
end;
write(p);
end.

var a,b,k,p:integer;
begin
read(a,b);
if b mod a <> 0 then begin
write(-1);
halt(0);
end;
if (b mod 2 <> 0) and (b mod 3 <>0) then begin
write(-1);
halt(0);
end;
k:=b div a;
while k<>1 do begin
if k mod 3 = 0 then begin
inc(p);
k:=k div 3
end
else begin
inc(p);
k:=k div 2;
end;
end;
write(p);
end.

var a,b,k,p:integer;
begin
read(a,b);
if b mod a <> 0 then begin
write(-1);
halt(0);
end;
k:=b div a;
while k<>1 do begin
if k mod 3 = 0 then begin
inc(p);
k:=k div 3
end
else begin
inc(p);
k:=k div 2;
end;
end;
write(p);
end.

var a,b,k,p:integer;
begin
read(a,b);
if b mod a <> 0 then begin
write(-1);
halt(0);
end;
k:=b div a;
while k<>0 do begin
if k mod 3 = 0 then begin
inc(p);
k:=k div 3
end
else begin
inc(p);
k:=k div 2;
end;
end;
write(p-1);
end.

var k,n,m:longint;
begin
read(n,m);
k:=0;
if m mod n >0 then begin writeln(-1);exit;end;
n:=m div n;
while n>1 do
 begin
  if n mod 2=0 then n:=n div 2 else n:=n div 3;
  inc(k);
  end;
  writeln(k);
  end.

  var
    n,m,count:longint;

  begin
    readln(n,m);
    count:=0;
    if (m mod n = 0) then
      begin
        m:=m div n;
        while m mod 3 = 0 do
          begin
          m:=m div 3;
          inc(count);
          end;
        while m mod 2 = 0 do
          begin
          m:=m div 2;
          inc(count);
          end;
      end
    else count:=-1;
    if m=n then count:=0;
    writeln(count);
  end.

Var
    n, m, Ans: UInt64;

Begin
    Read(n, m);
    For Var i := 1 to 28 do
        For Var j := 1 to 18 do
            If n*Round(Power(2, i))*Round(Power(3, j)) = m then Begin
                Write(i + j);
                Exit
            End;
    Write(-1)
End.

var
a,b,q,n:integer;
begin
read(a,b);
if b mod a <> 0 then n:=-1 else
begin
q:=b div a;
while q<>1 do begin
if q mod 3 = 0 then q:=q div 3;
if q mod 2 = 0 then q:=q div 2 else begin n:=-1; break; end;
inc(n);
end;
end;
write(n);
end.

var
a,b,q,n:integer;
begin
read(a,b);
if b mod a <> 0 then n:=-1 else
begin
q:=b div a;
while q<>1 do begin
if q mod 3 = 0 then q:=q div 3 else q:=q div 2;
inc(n);
end;
end;
write(n);
end.

var
a,b,q,n:integer;
begin
read(a,b);
if b mod a <> 0 then n:=-1 else
begin
q:=b div a;
while q<>1 do begin
if q mod 3 = 0 then q:=q div 3;
if q mod 2 = 0 then q:=q div 2;
if (q mod 3 <>0 ) and (q mod 2 <> 0) then begin n:=-1; break; end;
inc(n);
end;
end;
write(n);
end.

  var
  m,n:longint;
  k:integer;
  Begin
  readln(n,m);
  k:=0;
  if m mod n <>0 then k:=-1;
  if (m div n=1)and(m mod n=0) then k:=0;
  if (m mod n=0)and(m div n>1) then begin
             m:=m div n;
           while (m mod 3=0) do
            begin
            m:=m div 3;
            k:=k+1;
            end;
            while (m mod 2=0) do
            begin
            m:=m div 2;
            k:=k+1;
            end;
            end;
            if m>1 then k:=-1;
  writeln(k);
  End.

  var
  m,n:longint;
  k:integer;
  Begin
  readln(n,m);
  k:=0;
  if m mod n <>0 then k:=-1;
  if (m div n=1)and(m mod n=0) then k:=0;
  if (m mod n=0)and(m div n>1) then begin
             m:=m div n;
           while (m mod 3=0) do
            begin
            m:=m div 3;
            k:=k+1;
            end;
            while (m mod 2=0) do
            begin
            m:=m div 2;
            k:=k+1;
            end;
            end;
  writeln(k);
  End.

var
  n, m, an, z: uint64;

begin
  read(n, m);
  if n = m then
    write(0)
  else
  if m mod n <> 0 then
    write(-1)
  else
  begin
    z := m div n;
    while z mod 2 = 0 do
    begin
      z := z div 2;
      an := an + 1;
    end;
    while z mod 3 = 0 do
    begin
      z := z div 3;
      an := an + 1;
    end;
    write(an);
  end;
end.

var
  n, m, an, z: uint64;

begin
  read(n, m);
  if n = m then
    write(0)
  else
  if (n = 1) and ((m mod 2 <> 0) and (m mod 3 <> 0)) then
    write(-1)
  else
  if m mod n <> 0 then
    write(-1)
  else
  begin
    z := m div n;
    while z mod 2 = 0 do
    begin
      z := z div 2;
      an := an + 1;
    end;
    while z mod 3 = 0 do
    begin
      z := z div 3;
      an := an + 1;
    end;
    if z = 0 then
      write(an)
    else
      write(-1);
  end;
end.

var
  n, m, an, z: uint64;

begin
  read(n, m);
  if n = m then
    write(0)
  else
  if (n = 1) and ((m mod 2 <> 0) and (m mod 3 <> 0)) then
    write(-1)
  else
  if m mod n <> 0 then
    write(-1)
  else
  begin
    z := m div n;
    while z mod 2 = 0 do
    begin
      z := z div 2;
      an := an + 1;
    end;
    while z mod 3 = 0 do
    begin
      z := z div 3;
      an := an + 1;
    end;
    write(an);
  end;
end.

var n,m,x,j : integer;
begin
  read(n,m);
  x := 0;
  j := m div n;
  if n=m then write(0)
         else if m mod n <> 0 then write('-1')
                              else if (j  mod 2 <> 0) and (j mod 3 <> 0) then write('-1')
                                                                         else
 begin
  while j mod 2 = 0 do
    begin
      inc(x);
      j := j div 2;
    end;
  while j mod 3 = 0 do
    begin
      inc(x);
      j := j div 3;
    end;
  write(x);
end;
end.

procedure kol_del(var ch , kol_two,kol_three : longint; res : longint; var flag : boolean);
begin
  while (ch mod 2 = 0) or (ch mod 3 = 0) do
    begin
      if ch = res then
        flag := true;
      if ch mod 2 = 0 then
        begin
          ch := ch div 2;
          kol_two += 1;
        end
      else
        if ch mod 3 = 0 then
          begin
            ch := ch div 3;
            kol_three += 1;
          end;
    end;
end;

var
  chislo1 , chislo2 , ch1 , ch2 , kol_two1, kol_two2 , kol_three1, kol_three2 : longint;
  flag : boolean;
begin
  flag := false;
  read(ch1,ch2);
  kol_del(ch2,kol_two2,kol_three2,ch1,flag);
  kol_del(ch1,kol_two1,kol_three1,0,flag);
  if (kol_two2 >= kol_two1) and (kol_three2 >= kol_three1) then
    begin
      write(kol_two2 - kol_two1 + kol_three2 - kol_three1)
    end
  else
    write(-1);
end.

Var x, y, z, f:int64;
begin
  Read(x, y) ;
  If y mod x>0 then begin writeln('-1');exit;end;
  If ((y mod 2>0) and (y mod 3>0)) and (y<>1)then begin writeln('-1');exit;end;
  Z:=y div x;
  Repeat
  If z mod 3 = 0 then begin
  Z:=z div 3;
  F:=f+1;
  End;
  If z mod 2 = 0 then begin
  Z:=z div 2;
  F:=f+1;
  End;
  If (z mod 2>0)and(z mod 3>0) then begin writeln('-1');exit;end;
  Until z = 1;
  Writeln(f);
end.

Var x, y, z, f:int64;
begin
  Read(x, y) ;
  If y mod x>0 then begin writeln('-1');exit;end;
  If y mod 2>0 then begin writeln('-1');exit;end;
If y mod 3>0 then begin writeln('-1');exit;end;
  Z:=y div x;
  Repeat
  If z mod 3 = 0 then begin
  Z:=z div 3;
  F:=f+1;
  End;
  If z mod 2 = 0 then begin
  Z:=z div 2;
  F:=f+1;
  End;
  Until z = 1;
  Writeln(f);
end.

Var x, y, z, f:int64;
begin
  Read(x, y) ;
  If y mod x>0 then begin writeln('-1');exit;end;
  If ((y mod 2>0) and (y mod 3>0)) and (y<>1)then begin writeln('-1');exit;end;
  Z:=y div x;
  Repeat
  If z mod 3 = 0 then begin
  Z:=z div 3;
  F:=f+1;
  End;
  If z mod 2 = 0 then begin
  Z:=z div 2;
  F:=f+1;
  End;
  If (z mod 2>0)and(z mod 3>0) and(z<>1) then begin writeln('-1');exit;end;
  Until z = 1;
  Writeln(f);
end.

Var x, y, z, f:int64;
begin
  Read(x, y) ;
  If y mod x>0 then begin writeln('-1');exit;end;
  If (y mod 2>0) and (y mod 3>0) then begin writeln('-1');exit;end;
  Z:=y div x;
  Repeat
  If z mod 3 = 0 then begin
  Z:=z div 3;
  F:=f+1;
  End;
  If z mod 2 = 0 then begin
  Z:=z div 2;
  F:=f+1;
  End;
  Until z = 1;
  Writeln(f);
end.

program q;
  var i,k,n,m,o:integer;
      z:real;
begin
  read(n,m);
  o:=1;
  z:=m/n;
  if z<>round(z) then
    k:=-1
  else begin
    while o<>0 do
    begin
      z:=z/6;
      if z/6<>round(z/6) then begin
      i:=i+1;
      break;exit;
      end;
      i:=i+1;
    end;
    k:=i*2;
    if (z/2<>round(z/2)) and (z/3<>round(z/3)) then begin
    k:=k;end
    else begin
      if z/2<>round(z/2) then
      begin
      while z<>1 do begin
      z:=z/3;
      k:=k+1;
      end;
      end
      else
      if z/3<>round(z/3) then
      begin
      while z<>1 do begin
      z:=z/2;
      k:=k+1;
      end;
      end;
    end;
    end;
  if n=m then k:=0;
  writeln(k);
end.

  var a:integer;
      b,n,m:real;
begin
  read(n,m);a:=0;b:=m/n;
  if b=1 then a:=0
  else begin
    if round(b)<>b then a:=-1
    else begin
      if b/6=round(b/6) then
        repeat
          b:=b/6;a:=a+2;
        until b/6<>round(b/6);
      if b/3=round(b/3) then
        repeat
          b:=b/3;a:=a+1;
        until b/3<>round(b/3);
      if b/2=round(b/2) then
        repeat
          b:=b/2;a:=a+1;
        until b/2<>round(b/2);
    end;
  end;
  writeln(a);
end.

program q;
  var i,k,n,m,o:integer;
      z:real;
begin
  read(n,m);
  o:=1;
  z:=m/n;
  if z<>round(z) then
    k:=-1
  else begin
    if z/6=round(z/6) then
    begin
    while o<>0 do
    begin
      z:=z/6;
      if z/6<>round(z/6) then begin
      i:=i+1;
      break;exit;
      end;
      i:=i+1;
    end;
    k:=i*2;
    if (z/2<>round(z/2)) and (z/3<>round(z/3)) then begin
    k:=k;end
    else begin
      if z/2<>round(z/2) then
      begin
      while z<>1 do begin
      z:=z/3;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end
      else
      if z/3<>round(z/3) then
      begin
      while z<>1 do begin
      z:=z/2;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end;
    end;
    end
    else
    if (z/2<>round(z/2)) and (z/3<>round(z/3)) then begin
    k:=k;end
    else begin
      if z/2<>round(z/2) then
      begin
      while z<>1 do begin
      z:=z/3;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end
      else
      if z/3<>round(z/3) then
      begin
      while z<>1 do begin
      z:=z/2;
      k:=k+1;
      if (z/2<>round(z/2)) and (z/3<>round(z/3)) and (z<>1) then begin
      k:=-1;break;end;
      end;
      end;
    end;
    end;
  if n=m then k:=0;
  writeln(k);
end.

var n,m,dem:int64;
procedure nhap;
begin
        read(m,n);
end;
function phantich(a:int64;b:int64):boolean;
var i:integer;
begin
        i:=2;
        if b mod a <>0 then
                exit(FALSE)
        else
                begin
                        b:=b div a;
                        while b mod 2 =0 do
                                begin
                                        b:=b div i;
                                        dem:=dem+1;
                                end;
                        while b mod 3 =0 do
                                begin
                                        b:=b div i;
                                        dem:=dem+1;
                                end;
                        if b>1 then
                                exit(FALSE)
                        else
                                exit(TRUE);

                end;

end;
procedure xuli;
begin
        dem:=0;
        if (n<m) then
                writeln('-1')
        else if (n=m) then
                writeln('0')
        else
                if phantich(m,n)=FALSE then
                        writeln('-1')
                else
                        writeln(dem);
        readln;
end;
procedure xuat;
begin
        writeln(dem);
        readln;
end;
begin
        nhap;
        xuli;
        //xuat;
end.

var n,m,dem:int64;
procedure nhap;
begin
        read(m,n);
end;
function phantich(a:int64;b:int64):boolean;
var i:integer;
begin
        i:=2;
        if b mod a <>0 then
                exit(FALSE)
        else
                begin
                        b:=b div a;
                        while b mod 2 =0 do
                                begin
                                        b:=b div i;
                                        dem:=dem+1;
                                end;
                        while b mod 3 =0 do
                                begin
                                        b:=b div i;
                                        dem:=dem+1;
                                end;
                        if b>1 then
                                exit(FALSE)
                        else
                                exit(TRUE);

                end;

end;
procedure xuli;
begin
        writeln;
        dem:=0;
        if (n<m) then
                writeln('-1')
        else if (n=m) then
                writeln('0')
        else
                if phantich(m,n)=FALSE then
                        writeln('-1')
                else
                        writeln(dem);
        readln;
end;
procedure xuat;
begin
        writeln(dem);
        readln;
end;
begin
        nhap;
        xuli;
        //xuat;
end.

var m,n,i:longint;
begin
read(m,n);
if m mod n <>0 then
   begin
   write(-1);exit;
   end;
m:=trunc(m/n);i:=0;
while m mod 2=0 do
   begin
   i:=i+1;m:=trunc(m/2);
   end;
while m mod 3=0 do
   begin
   i:=i+1;m:=trunc(m/3);
   end;
if m<>1 then
   begin
   write(-1);exit;
   end
else write(i);
end.

const fi='';
      fo='';
var f:text;
    n,m,s,d:int64;

begin
  assign(f,fi);reset(f);
  readln(f,n,m);

  assign(f,fo);rewrite(f);
  s:= m div n;
  if s * n <> m then write(f,-1)
  else
  if s=1 then write(f,1)
  else
  begin
    while (s <> 1) do
        begin
          if s mod 3 = 0 then
          begin
            inc(d);
            s:=s div 3;
          end
          else
          if s mod 2 = 0 then
          begin
            inc(d);
            s:=s div 2;
          end;
          if (s mod 2 <> 0 ) and (s mod 3 <> 0) and (s <> 1) then
          begin
            d:=-1;
            break;
          end;
        end;
    write(f,d);
  end;
  close(f);
end.

var
    f,m,n,ans : longint;
begin
    readln(n,m); f := m div n;
    if m mod n > 0 then begin write(-1); halt; end;
    while f mod 2 = 0 do begin ans += 1; f := f div 2; end;
    while f mod 3 = 0 do begin ans += 1; f := f div 3; end;
    write(ans);
end.

Var  b,c,tam:int64;
     k:longint;

Begin

  Readln(c,b);
  k:=0;
  If (b mod c=0) and (b div c=1) then k:=0
  Else if (2*c>b) then k:=-1
  Else
    Begin
     tam:=b div c;
      While (tam mod 2=0) do
        begin
          tam:=tam div 2;
          inc(k);

        end;
      While (tam mod 3=0) do
        begin
          tam:=tam div 3;
          inc(k);
        end;
         If k=0 then k:=-1;
     end;

  writeln(k);

ENd.

Var  b,c,tam:int64;
     k:longint;

Begin

  Readln(c,b);
  k:=0;
  If (b mod c=0) and (b div c=1) then k:=0
  Else if (2*c>b) then k:=-1
  Else
    Begin
     tam:=b div c;
      While (tam mod 2=0) do
        begin
          tam:=tam div 2;
          inc(k);

        end;
      While (tam mod 3=0) do
        begin
          tam:=tam div 3;
          inc(k);
        end;
         If (k=0) or (tam<>c) then k:=-1;

     end;

  writeln(k);

ENd.

Var  b,c,tam:int64;
     k:longint;

Begin

  Readln(c,b);
  k:=0;
  If (b mod c=0) and (b div c=1) then k:=0
  Else if (2*c>b) then k:=-1
  Else
    Begin
      tam:=b div c;
      While (tam mod 2=0) do
        begin
          tam:=tam div 2;
          inc(k);
        end;
      While (tam mod 3=0) do
        begin
          tam:=tam div 3;
          inc(k);
        end;
     end;
  writeln(k);

ENd.

Var  b,c,tam:int64;
     k:longint;

Begin

  Readln(c,b);
  k:=0;
  If (b mod c=0) and (b div c=1) then k:=1
  Else if (2*c>b) then k:=-1
  Else
    Begin
      tam:=b div c;
      While (tam mod 2=0) do
        begin
          tam:=tam div 2;
          inc(k);
        end;
      While (tam mod 3=0) do
        begin
          tam:=tam div 3;
          inc(k);
        end;
     end;
  writeln(k);

ENd.

Var  b,c,tam:int64;
     k:longint;

Begin

  Readln(c,b);
  k:=0;
  If (b mod c=0) and (b div c=1) then k:=0
  Else if (2*c>b) then k:=-1
  Else
    Begin
     tam:=b div c;
      While (tam mod 2=0) do
        begin
          tam:=tam div 2;
          inc(k);

        end;
      While (tam mod 3=0) do
        begin
          tam:=tam div 3;
          inc(k);
        end;
     end;
 If k=0 then k:=-1;

  writeln(k);

ENd.

var n,m,k,x,a,f:longint;
begin
{assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);}
readln(n,m);
if n=m then begin write(0);halt;end;
if n>m div 2 then begin write(-1);halt;end;
x:=m div n;
a:=x;
k:=0;
if (a mod 2<>0) and (a mod 3<>0) then begin write(-1);halt;end;
while a<>1 do
 begin
 if (a mod 2<>0) and (a mod 3<>0) then begin write(-1);break;end;
  if a mod 2=0 then begin a:=a div 2;k:=k+1;end;
  if a mod 3=0 then begin a:=a div 3;k:=k+1;end;

 end;
if a=1 then write(k);
end.

var n,m,k,x,a,f:longint;
begin
{assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);}
readln(n,m);
if n=m then begin write(0);halt;end;
if n>m div 2 then begin write(-1);halt;end;
x:=m div n;
a:=x;
f:=0;k:=0;
while a<>1 do
 begin
 if (a mod 2<>0) and (a mod 3<>0) then begin write(-1);break;end;
  if a mod 2=0 then begin a:=a div 2;k:=k+1;end;
  if a mod 3=0 then begin a:=a div 3;k:=k+1;end;

 end;
if a=1 then write(k);
end.

const fi='input.txt';
      fo='output.txt';
var a:array[1..100005,1..2] of longint;
    res,n,m:longint;

procedure Xuli(n,x:longint);
var i:longint;
Begin
        for i:=2 to trunc(sqrt(n)) do begin
                while n mod i=0 do begin
                        inc(a[i][x]);
                        n:=n div i;
                end;
        end;
End;

function Check:boolean;
var i:longint;
Begin
        if (a[2][1]>a[2][2]) or (a[3][1]>a[3][1]) then exit(false);
        for i:=4 to 100000 do if a[i][1]<>a[i][2] then exit(false);
        exit(true);
End;

BEGIN
        //assign(input,fi); reset(input);
        //assign(output,fo); rewrite(output);

        read(n,m);
        Xuli(n,1); Xuli(m,2);

        if Check=true then write(abs(a[2][1]-a[2][2])+abs(a[3][1]-a[3][2])) else write(-1);

        //close(input);
        //close(output);
END.

const fi='input.txt';
      fo='output.txt';
var a:array[1..100005,1..2] of longint;
    res,n,m:longint;

procedure Xuli(n,x:longint);
var i:longint;
Begin
        if (n=2) or (n=3) then inc(a[n][x]);
        for i:=2 to trunc(sqrt(n)) do begin
                while n mod i=0 do begin
                        inc(a[i][x]);
                        n:=n div i;
                end;
        end;
End;

function Check:boolean;
var i:longint;
Begin
        if (a[2][1]=0) or (a[2][2]=0) or (a[3][1]=0) or (a[3][2]=0) then exit(false);
        if (a[2][1]>a[2][2]) or (a[3][1]>a[3][1]) then exit(false);
        for i:=4 to 100000 do if a[i][1]<>a[i][2] then exit(false);
        exit(true);
End;

BEGIN
        //assign(input,fi); reset(input);
        //assign(output,fo); rewrite(output);

        read(n,m);
        Xuli(n,1);
        Xuli(m,2);

        if Check=true then write(abs(a[2][1]-a[2][2])+abs(a[3][1]-a[3][2])) else write(-1);

        //close(input);
        //close(output);
END.

const fi='input.txt';
      fo='output.txt';
var a:array[1..100005,1..2] of longint;
    res,n,m:longint;

procedure Xuli(n,x:longint);
var i:longint;
Begin
        if (n=2) or (n=3) then inc(a[n][x]);
        for i:=2 to trunc(sqrt(n)) do begin
                while n mod i=0 do begin
                        inc(a[i][x]);
                        n:=n div i;
                end;
        end;
End;

function Check:boolean;
var i:longint;
Begin
        if (a[2][1]>a[2][2]) or (a[3][1]>a[3][1]) then exit(false);
        for i:=4 to 100000 do if a[i][1]<>a[i][2] then exit(false);
        exit(true);
End;

BEGIN
        //assign(input,fi); reset(input);
        //assign(output,fo); rewrite(output);

        read(n,m);
        Xuli(n,1);
        Xuli(m,2);

        if Check=true then write(abs(a[2][1]-a[2][2])+abs(a[3][1]-a[3][2])) else write(-1);

        //close(input);
        //close(output);
END.

var
  n,m,a : real;
  b,kol : longint;
begin
    read(n,m);
    a:=m/n;
    b:=trunc(a);
    if trunc(a) <> a then begin write('-1'); halt; end;
    if (trunc(m/n) mod 2 <> 0) and (trunc(m/n) mod 2 <> 0) then begin write('-1'); halt; end;
    while (b mod 2 = 0) or (b mod 3 = 0) do
     begin
      if b mod 2 = 0 then begin b:=b div 2; inc(kol); end
                     else if (b mod 3 = 0) then begin b:=b div 3; inc(kol); end;
     end;
    write(kol);

end.

var
  n,m,a : real;
  b,kol : longint;
begin
    read(n,m);
    a:=m/n;
    b:=trunc(a);
    if trunc(a) <> a then begin write('-1'); halt; end;
    while (b mod 2 = 0) or (b mod 3 = 0) do
     begin
      if b mod 2 = 0 then begin b:=b div 2; inc(kol); end
                     else if (b mod 3 = 0) then begin b:=b div 3; inc(kol); end;
     end;
    write(kol);

end.

var a,b,kq,dem,c,i :longint;
BEGIN
    read(a,b);  kq:=0;
    if b mod a <>0 then kq:=-1;
    if a=b then kq:=0;
    if b mod a=0 then
       begin
           c:=b div a; i:=2;  dem:=0;
           while c<>1 do
               begin
                   while c mod i=0 do begin c:=c div i; inc(kq); end;
                   inc(i);
               end;
       end;
       write(kq);
END.

var a,b,kq,c,i :longint;
BEGIN
    read(a,b);  kq:=0;
    if b mod a <>0 then kq:=-1;
    if a=b then kq:=0;
    if b mod a=0 then
       begin
           c:=b div a; i:=2;
           while c<>1 do
               begin
                   while c mod i=0 do begin c:=c div i; inc(kq); end;
                   inc(i);
                   if i>3 then begin kq:=-1; break; end;
               end;
       end;
       write(kq);
END.

var a,b,kq,c,i :longint;
BEGIN
    read(a,b);  kq:=0;
    if b mod a <>0 then kq:=-1;
    if a=b then kq:=0;
    if b mod a=0 then
       begin
           c:=b div a; i:=2;
           while c<>1 do
               begin
                   while c mod i=0 do begin c:=c div i; inc(kq); end;
                   inc(i);
               end;
       end;
       write(kq);
END.

const kt=['u','e','a','o','i'];
var st1,st2 :string;
    i,demn,demn1,demp,demp1 :integer;
BEGIN
    readln(st1);read(st2);
    for i:=1 to length(st1) do if st1[i] in kt then inc(demn) else inc(demp);
    for i:=1 to length(st2) do if st2[i] in kt then inc(demn1) else inc(demp1);

    if (demn<>demn1) or (demp1<>demp) then write('NO') else write('YES');
END.

const fi='';
      fo='';
var f,g:text;
    m,n,kq,count,count1:longint;
procedure inp;
begin
  assign(f,fi);
  reset(f);
  read(f,n,m);
  close(f);
end;

procedure solve;
begin
 if (m mod n<>0) then count:=-1
  else
   begin
    kq:=m div n;
    While (kq mod 2=0) do
       begin
        kq:=kq div 2;
        inc(count);
       end ;
    While (kq mod 3=0) do
        begin
         kq:=kq div 3;
         inc(count);
        end;
     end;

end;
procedure output;
begin
  assign(g,fo);
  rewrite(g);
  writeln(g,count);
  close(g);
end;

begin
  inp;
  solve;
  output;
end.

const fi='';
      fo='';
var f,g:text;
    m,n,kq,count,count1:longint;
procedure inp;
begin
  assign(f,fi);
  reset(f);
  read(f,n,m);
  close(f);
end;

procedure solve;
begin
 if (m mod n<>0) then count:=-1
  else
   begin
    kq:=m div n;
    While (kq mod 2<>0) do
       begin
        kq:=kq div 2;
        inc(count);
       end ;
    While (kq mod 3<>0) do
        begin
         kq:=kq div 3;
         inc(count);
        end;
     end;

end;
procedure output;
begin
  assign(g,fo);
  rewrite(g);
  writeln(g,count);
  close(g);
end;

begin
  inp;
  solve;
  output;
end.

var a,b,c:longint;
begin
  readln(a,b);
  if (b mod a<>0)or(b<a) then writeln(-1)
  else begin
    if b=a then writeln(0)
    else begin
      b:=b div a;
      while b<>1 do begin
        if b mod 2=0 then b:=b div 2
        else b:=b div 3;
        inc(c);
      end;
      writeln(c);
    end;
  end;
end.

const fi='1141A.inp';
      fo='1441A.OUT';
 var f:text ; n,m,dem2,dem3,s2m,s3m,s2n,s3n:longint;

 procedure nhap;
  begin
  //  assign(f,fi);
   // reset(F);
    readln({f,}n,m);
 // close(f);
  end;

 procedure demsocua(x:longint) ;
   begin
    dem2:=0;
    dem3:=0;
      while( x mod 3 =0 )and(n>0) do
      begin
        inc(dem3);
        x:=x div 3 ;
      end;

     while( x mod 2 =0 )and(n>0) do
      begin
        inc(dem2);
        x:=x div 2 ;
      end;

    end;

 procedure xuli;
   begin
    // demsocua(n);
   //  s2n:=dem2;
   //  s3n:=dem3;
     demsocua(m div n );
     s2m:=dem2;
     s3m:= dem3;
      s2n:=0;
     s2m:=s2m{-s2n};
     s3m:=s3m{-s2n};
     if (s2m+s3m =0 )and( n =m) then s2n:=1;
   end;

 procedure xuat;
   begin
  // assign(f,fo);
   //  rewrite(F);
        if s2n=1 then writeln({f,}0)else

     if (m mod n=0) and((m div n mod 2 =0) or( m div n mod 3 =0))  then writeln({f,}s2m+s3m)

     else
       begin

        writeln({f,}-1);
       end;
//     close(F);
   end;

 begin
   nhap;
   xuli;
   xuat;
 end.

const fi='1141A.inp';
      fo='1441A.OUT';
 var f:text ; n,m,dem2,dem3,s2m,s3m,s2n,s3n:longint;

 procedure nhap;
  begin
   // assign(f,fi);
   // reset(F);
    readln({f,}n,m);
 //   close(f);
  end;

 procedure demsocua(x:longint) ;
   begin
    dem2:=0;
    dem3:=0;
      while( x mod 3 =0 )and(n>0) do
      begin
        inc(dem3);
        x:=x div 3 ;
      end;

     while( x mod 2 =0 )and(n>0) do
      begin
        inc(dem2);
        x:=x div 2 ;
      end;

    end;

 procedure xuli;
   begin
    // demsocua(n);
   //  s2n:=dem2;
   //  s3n:=dem3;
     demsocua(m div n );
     s2m:=dem2;
     s3m:= dem3;

     s2m:=s2m{-s2n};
     s3m:=s3m{-s2n};

   end;

 procedure xuat;
   begin
   //  assign(f,fo);
    // rewrite(F);
     if m mod n=0 then writeln({f,}s2m+s3m) else writeln({f,}-1);
     //close(F);
   end;

 begin
   nhap;
   xuli;
   xuat;
 end.

const fi='1141A.inp';
      fo='1441A.OUT';
 var f:text ; n,m,dem2,dem3,s2m,s3m,s2n,s3n:longint;

 procedure nhap;
  begin
   // assign(f,fi);
   // reset(F);
    readln({f,}n,m);
 //   close(f);
  end;

 procedure demsocua(x:longint) ;
   begin
    dem2:=0;
    dem3:=0;
      while( x mod 3 =0 )and(n>0) do
      begin
        inc(dem3);
        x:=x div 3 ;
      end;

     while( x mod 2 =0 )and(n>0) do
      begin
        inc(dem2);
        x:=x div 2 ;
      end;

    end;

 procedure xuli;
   begin
    // demsocua(n);
   //  s2n:=dem2;
   //  s3n:=dem3;
     demsocua(m div n );
     s2m:=dem2;
     s3m:= dem3;

     s2m:=s2m{-s2n};
     s3m:=s3m{-s2n};
     if s2m+s3m =0 then if n =m then s2n:=1;
   end;

 procedure xuat;
   begin
   //  assign(f,fo);
    // rewrite(F);

     if (m mod n=0) and( m div n >0) then writeln({f,}s2m+s3m)

     else
       begin
        if s2n=1 then writeln(0)else

        writeln({f,}-1);
       end;
     //close(F);
   end;

 begin
   nhap;
   xuli;
   xuat;
 end.

const fi='1141A.inp';
      fo='1441A.OUT';
 var f:text ; n,m,dem2,dem3,s2m,s3m,s2n,s3n:longint;

 procedure nhap;
  begin
  //  assign(f,fi);
   // reset(F);
    readln({f,}n,m);
 // close(f);
  end;

 procedure demsocua(x:longint) ;
   begin
    dem2:=0;
    dem3:=0;
      while( x mod 3 =0 )and(n>0) do
      begin
        inc(dem3);
        x:=x div 3 ;
      end;

     while( x mod 2 =0 )and(n>0) do
      begin
        inc(dem2);
        x:=x div 2 ;
      end;

    end;

 procedure xuli;
   begin
    // demsocua(n);
   //  s2n:=dem2;
   //  s3n:=dem3;
     demsocua(m div n );
     s2m:=dem2;
     s3m:= dem3;
      s2n:=0;
     s2m:=s2m{-s2n};
     s3m:=s3m{-s2n};
     if (s2m+s3m =0 )and( n =m) then s2n:=1;
   end;

 procedure xuat;
   begin
  // assign(f,fo);
   //  rewrite(F);
        if s2n=1 then writeln({f,}0)else

     if (m mod n=0) and(m div n mod 2 =0) and( m div n mod 3 =0)  then writeln({f,}s2m+s3m)

     else
       begin

        writeln({f,}-1);
       end;
//     close(F);
   end;

 begin
   nhap;
   xuli;
   xuat;
 end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if ( x mod 2 = 0 ) or ( x mod 3 = 0) or (x mod 6 =0 ) then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if x mod 5 =0 then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if ( x mod 6 = 0 ) or ( x mod 9 = 0) or (x mod 4 = 0 ) then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if x mod 6 =0 then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if x mod 5 =0 then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        writeln(dem);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if n*2 >  m    then write(-1) else begin
        x:=m div n;
        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if ( x mod 2 = 0 ) or ( x mod 3 = 0) then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        if (x mod 6 = 0 ) then begin

        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end else write(-1);
        end;
end.

program CF_1141_A;
var n,m,dem,x:longint;

Begin
       dem:=0;
        readln(n,m);
        if m=n then write(0) else
        if m mod n <> 0    then write(-1) else begin
        x:=m div n;
        while x mod 3 = 0 do
        begin
                inc(dem);
                x:=x div 3;
        end;
        while x mod 2 = 0 do
        begin
                inc(dem);
                x:=x div 2;
        end;
        writeln(dem);
        end;
end.

program Project2;

{$APPTYPE CONSOLE}

uses
  SysUtils;

var
  n, m, a,s, i : longint;
begin
  read(n, m);
  s := 0;
  if (m mod n <> 0) then
    writeln(-1)
  else
    begin
      a := m div n;
      for i := 1 to a do
      begin
        if (a = 1) then
          begin
            writeln(s);
            break;
          end
        else if (a mod 2 = 0) then
          a := a div 2
        else if (a mod 3 = 0) then
          a := a div 3
        else
          writeln(-1);
        inc(s);
      end;
    end;
end.

var
  n, m, x: longint;
begin
  read(n, m);
  if m mod n <> 0 then
    write(-1)
  else begin
    n := m div n;
    x := 0;
    while n > 1 do begin
      if n mod 2 = 0 then
        n := n div 2
      else if n mod 3 = 0 then
        n := n div 3
      else begin
        write(-1);
        halt;
      end;
      inc(x);
    end;
  end;
end.

program solution;
{$APPTYPE CONSOLE}

var
  i, n, m, counter: Int64;

begin
  readln(n, m);
  counter := 0;
    if n <> m then
      begin
        m := m div n;
        while m mod 2 = 0 do
          begin
            m := m div 2;
            inc(counter);
          end;
        while m mod 3 = 0 do
          begin
            m := m div 3;
            inc(counter);
          end;
        if m <> 1 then
          counter := -1;
      end;
    writeln(counter);
end.

program solution;
{$APPTYPE CONSOLE}

var
  i, n, m, counter: Int64;

begin
  readln(n, m);
  counter := 0;
  if m mod n <> 0 then
    writeln(-1)
  else
  begin
    if n <> m then
      begin
        m := m div n;
        while m mod 2 = 0 do
          begin
            m := m div 2;
            inc(counter);
          end;
        while m mod 3 = 0 do
          begin
            m := m div 3;
            inc(counter);
          end;
      end;
    writeln(counter);
  end;
  readln;
end.

var n,m,k:longint;
begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
readln(n,m);
if n=m then begin writeln(0);halt;end;
if n>m div 2 then begin writeln(-1);halt;end;
m:=m div n;
while m mod 3=0 do
begin
m:=m div 3;
inc(k);
end;
while m mod 2=0 do
begin
m:=m div 2;
inc(k);
end;
if m=1 then writeln(k) else writeln(-1);
end.

var n,m,k:longint;
begin
{assign(input,'input.txt');reset(input);
assign(output,'output.txt');rewrite(output);}
readln(n,m);
if n>m then begin writeln(-1);halt;end;
if n=m then begin writeln(0);halt;end;
m:=m div n;
while m mod 3=0 do
begin
m:=m div 3;
inc(k);
end;
while m mod 2=0 do
begin
m:=m div 2;
inc(k);
end;
if m=1 then writeln(k) else writeln(-1);
end.

var n,m,k:longint;
begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
readln(n,m);
if n>m div 2 then begin writeln(-1);halt;end;
if n=m then begin writeln(0);halt;end;
m:=m div n;
while m mod 3=0 do
begin
m:=m div 3;
inc(k);
end;
while m mod 2=0 do
begin
m:=m div 2;
inc(k);
end;
if m=1 then writeln(k) else writeln(-1);
end.

var
    n,m,min,max:longint;

procedure sc(x,num:longint);
begin

        if x=m then
                begin

                if min>num then min:=num;
                exit;

                end;

        if x>m then exit;

        sc(x*2,num+1);
        sc(x*3,num+1);

end;
begin

    readln(n,m);
    min:=maxlongint;

    sc(n,0);

    writeln(min);

end.

//uses crt;
const fi='A.inp'; fo='A.out';
var n,m,t,s:longint;
    ok:boolean;

procedure input;
var f:text;
 begin
   {assign(f,fi);
   reset(f);}
   //clrscr;
   readln(n,m);
   //close(f);
 end;

procedure find(var i,j:longint);
 begin
   j:=0;
   while i mod 2=0 do
    begin
      inc(j);
      i:=i div 2;
    end;
   while i mod 3=0 do
    begin
      inc(j);
      i:=i div 3;
    end;
 end;

procedure run;
 begin
   ok:=false;
   if m=n then
    begin
      ok:=true;
      exit;
    end;
   if m mod n<>0 then exit;
   if (m mod 2<>0) and (m mod 3<>0) then exit;
   ok:=true;
   find(m,t);
   find(n,s);
   t:=t-s;
 end;

procedure output;
var f:text;
 begin
   {assign(f,fo);
   rewrite(f);}
   if ok then writeln(t)
   else writeln('-1');
   //readln
   //close(f);
 end;

BEGIN
  input;
  run;
  output;
END.

var n,m,x,a,k,c:longint;
begin
//assign(input,'input.txt');reset(input);
//assign(output,'output.txt');rewrite(output);
readln(n,m);
if n>m div 2 then begin writeln(-1);halt;end;
if n=m then begin writeln(0);halt;end;
x:=m div n;
a:=x;
k:=0;
c:=m mod n;
if c>0 then begin write(-1);halt;end;
while a<>1 do
begin
  if (a mod 2<>0)and(a mod 3<>0)then begin write(-1);break;end;
  if a mod 2=0 then begin a:=a div 2;inc(k);end;
  if a mod 3=0 then begin a:=a div 3;inc(K);end;
end;
if a=1 then writeln(k);
end.