{$R+}{$Q+}
Var v,rez,j,kol,i,n:longint;
dp,m:array [1..100] of int64;
a:array [1..1000000] of int64;
used:array [2..1000000] of boolean;
used2:array [1..8] of boolean;
function proverka(n:int64):boolean;
Var i:longint;
begin
  for i:=1 to kol do
  if (n mod a[i]=0)and(n<>a[i])
  then begin proverka:=false; exit; end;
  proverka:=true;
end;
function probeg(n:int64):longint;
Var rez,i:longint;
begin
  rez:=0;
  for i:=1 to kol do
  begin
    while n mod a[i]=0 do
    begin
      inc(rez);
      n:=n div a[i];
    end;
  end;
  if n<>1
  then inc(rez);
  probeg:=rez;
end;
procedure reverse(z:longint);
Var i,j:longint;
b:int64;
begin
  i:=1; j:=z;
  while i<j do
  begin
    b:=m[i]; m[i]:=m[j]; m[j]:=b;
    b:=dp[i]; dp[i]:=dp[j]; dp[j]:=b;
    inc(i); dec(j);
  end;
end;
function bb(i,m:longint):longint;
begin
  if (m mod 2=0)and(m>2)
  then if i<m-1
       then bb:=i
       else bb:=m-2
       else bb:=m-1;
end;
procedure antilex(z:longint);
Var i,j,k:longint;
zz,b:int64;
begin
  if z=1
  then begin
         fillchar(used2,sizeof(used2),false);
         b:=v;
         for i:=1 to n do
         begin
          b:=b+dp[i];
          zz:=m[i];
          for j:=i-1 downto 1 do
          if (not used2[j])and(zz mod m[j]=0)
          then begin
                used2[j]:=true;
                zz:=zz div m[j];
                b:=b-dp[j];
               end;
         end;
         k:=0;
         for i:=1 to n do
         if not used2[i]
         then inc(k);
         if k>1
         then inc(b);
         if b<rez
         then rez:=b;
       end
  else for i:=1 to z do
      begin
        antilex(z-1);
        if i<z
        then begin
              b:=m[bb(i,z)]; m[bb(i,z)]:=m[z]; m[z]:=b;
              b:=dp[bb(i,z)]; dp[bb(i,z)]:=dp[z]; dp[z]:=b;
             end;
      end;
end;
begin
 // Assign(input,'input.txt'); reset(input);
 // Assign(output,'output.txt'); rewrite(output);
  read(n);
  for i:=1 to n do
  read(m[i]);
  kol:=0;
  for i:=2 to 1000000 do
  if not used[i]
  then begin
        inc(kol);
        a[kol]:=i;
        j:=i;
        while j<=1000000 do
        begin
          used[j]:=true;
          inc(j,i);
        end;
       end;
  if (n=1)and(proverka(m[1]))
  then begin writeln(1); halt(0); end;
  rez:=n+1;
  for i:=1 to n do
  begin
    dp[i]:=probeg(m[i]);
    rez:=rez+dp[i];
  end;
  v:=n;
  for i:=1 to n do
  if proverka(m[i])
  then dec(v);
  antilex(n);
  writeln(rez);
end.

Const
   fin = '';
   fon = '';
Var
   n : Longint;
   kt : Boolean;
   kq : Int64;
   free : array[1..1000001] of Boolean;
   A , luu , pre : array[1..8] of Int64;
Procedure Start;
   Begin
      Assign (input , fin);
      Reset (input);
      Assign (output , fon);
      Rewrite (output);
   End;
Procedure Sort (Left , Right : Longint);
   Var
      i , j : Longint;
      x , tmp : Int64;
   Begin
      i := Left;
      j := Right;
      x := A[(Left + Right) Div 2];
      Repeat
         while A[i] > x do Inc (i);
         while A[j] < x do Dec (j);
         if i <= j then
         begin
            tmp := A[i];
            A[i] := A[j];
            A[j] := tmp;
            tmp := luu[i];
            luu[i] := luu[j];
            luu[j] := tmp;
            Inc (i);
            Dec (j);
         end;
      Until i > j;
      if Left < j then Sort (Left , j);
      if i < Right then Sort (i , Right);
   End;
Procedure Tries (x , y : Int64);
   Var
      i : Longint;
      tmp : Int64;
   Begin
      if A[x] = A[x-1] then
      begin
         Tries (x + 1 , y);
         Exit;
      end;
      if x > n then
      begin
         tmp := y;
         if not kt then  Inc (tmp);
         if tmp < kq then kq := tmp;
         Exit;
      end;
      pre[x] := A[x];
      for i := 1 to x - 1 do
         if (pre[i] Mod A[x]) = 0 then
         begin
            tmp := y;
            if luu[x] <> 1 then tmp := y + 1;
            pre[i] := pre[i] Div A[x];
            Tries (x + 1 , tmp);
            pre[i] := pre[i] * A[x];
         end;
      kt := False;
      tmp := y + luu[x];
      if luu[x] <> 1 then Inc (tmp);
      Tries (x + 1 , tmp);
   End;
Procedure Programe;
   Var
      i , j : Longint;
      dem , tmp : Int64;
   Begin
      readln (n);
      for i := 1 to n do read (A[i]);
      fillchar (free , sizeof(free) , True);
      free[1] := False;
      for i := 2 to n do
      begin
         if free[i] then
            for j:=2 to (1000000 Div i) do
               free[i * j] := False;
      end;
      for i := 1 to n do
      begin
         tmp := A[i];
         dem := 0;
         for j := 1 to 1000000 do
         begin
            if j > tmp then Break;
            if not free[j] then Continue;
            while (tmp Mod j) = 0 do
            begin
               tmp := tmp Div j;
               Inc (dem);
            end;
         end;
         if tmp <> 1 then  Inc (dem);
         luu[i] := dem;
      end;
      Sort (1 , n);
      kq := 1000000000000000000;
      kt := True;
      pre[1] := A[1];
      if luu[1] = 1 then Tries (2 , luu[1])
      else Tries (2 , luu[1] + 1);
      writeln (kq);
   End;
Procedure Finish;
   Begin
      Close (input);
      Close (output);
   End;
Begin
   Start;
   Programe;
   Finish;
End.

var     i,j,k,n:longint;
        p,q,l,ans:int64;
        u:boolean;
        a,b,now:array[0..10]of int64;
        f:array[1..1000000]of boolean;
procedure dfs(x,y:longint);
var       i,w:longint;
begin
        if x>n then
        begin
                w:=y;
                if not u then  w:=w+1;
                if w<ans then ans:=w;
                exit;
        end
        else if a[x]=a[x-1] then
        begin
                dfs(x+1,y);
                exit;
        end
        else
        begin
                now[x]:=a[x];
                for i:=1 to x-1 do
                begin
                        if now[i] mod a[x]=0 then
                        begin
                                w:=y;
                                if b[x]<>1 then w:=y+1;
                                now[i]:=now[i] div a[x];
                                dfs(x+1,w);
                                now[i]:=now[i]*a[x];
                        end;
                end;

                if x>1 then u:=false;
                w:=y+b[x];
                if b[x]<>1 then w:=w+1;
                dfs(x+1,w);
        end;
end;
procedure sushu;
begin
        fillchar(f,sizeof(f),true);
        f[1]:=false;
        for i:=2 to n do
        begin
                if f[i] then

                        for j:=2 to 1000000 div i do f[i*j]:=false;
        end;

end;
procedure paixu;
begin
        for i:=1 to n do
                for j:=1 to n-i do
                begin
                        if a[j]<a[j+1] then
                        begin
                                l:=a[j];a[j]:=a[j+1];a[j+1]:=l;
                                l:=b[j];b[j]:=b[j+1];b[j+1]:=l;
                        end;
                end;
end;
procedure duru;
begin
        read(n);
        for i:=1 to n do read(a[i]);
        a[0]:=0;
end;
procedure work;
begin
        for i:=1 to n do
        begin
                q:=a[i];
                p:=0;
                for j:=1 to 1000000 do
                begin
                        if j>q then break;
                        if not f[j] then continue;
                        while q mod j=0 do
                        begin
                                q:=q div j;
                                p:=p+1;
                        end;
                end;
                if q<>1 then  p:=p+1;
                b[i]:=p;
        end;
end;
procedure solve;
begin
        ans:=maxlongint;
        u:=true;
        dfs(1,0);
        writeln(ans);
end;
begin
        duru;
        sushu;
        paixu;
        work;
        solve;
end.

var n,i,j,k,m:integer;
a:array [1..8] of int64;
b:array [1..8] of integer;
z,x:int64;
c:array [1..8,0..50] of integer;
d:array [1..8] of int64;

function res():integer;
var i,j,p:integer;
begin

for i:=1 to n do d[i]:=a[i];
for i:=2 to n do
  begin
  if b[i]>0 then
    begin
    if d[b[i]] mod a[i]=0 then d[b[i]]:=d[b[i]] div a[i] else
      begin
      res:=1000000001;
      exit;
      end;
    end;
  end;
p:=0;
for i:=2 to n do
  if b[i]=0 then p:=1;
p+=c[1,0]+1;
for i:=2 to n do
  if b[i]=0 then p+=c[i,0]+1 else
    begin
    if c[i,0]>0 then p+=1;
    end;
res:=p;

end;

begin
readln(n);
for i:=1 to n do read(a[i]);
for i:=1 to n-1 do
  for j:=1 to n-i do
    begin
    if a[j]<a[j+1] then
      begin
      z:=a[j];
      a[j]:=a[j+1];
      a[j+1]:=z;
      end;
    end;
//for i:=1 to n do write(a[i],' ');
//writeln();
for i:=1 to n do
  begin
  c[i,0]:=0;
  x:=a[i];
  k:=2;
  while k<=trunc(sqrt(x)) do
    begin
    while x mod k=0 do
      begin
      c[i,0]+=1;
      c[i,c[i,0]]:=k;
      x:=x div k;
      end;
    k+=1;
    end;
  if x>1 then
    begin
    c[i,0]+=1;
    c[i,c[i,0]]:=x;
    end;
  end;
for i:=1 to n do
  if c[i,0]=1 then c[i,0]:=0;
if n=1 then
  begin
  writeln(1+c[1,0]);
  halt;
  end;
m:=1000000000;
for i:=2 to n do
  begin
  b[i]:=0;
  end;
b[n]:=-1;
while true do
  begin
  k:=n;
  while (b[k]=k-1) and (k>1) do k-=1;
  if k=1 then break;
  b[k]+=1;
  for i:=k+1 to n do b[i]:=0;
  k:=res();
{  if k<100000 then
    begin
    for i:=2 to n do write(b[i],' ');
    writeln(k);
    end;
}  if k<m then m:=k;
  end;
writeln(m);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (not ffff[i]) then
    inc(p);
  if (p>1) then
  inc(ans);
  end;
  if (ans=24)and(k<>4) then
  ans:=ans-1;
  writeln(ans);
  //close(input);
  //close(output);
end.

const l=1000000;
var
  a,b:array[1..8] of int64; pr:array[1..l] of int64; used:array[1..l] of boolean;
  ok:array[1..8] of boolean; ans,n,i,j,k:longint; w,x,y:int64;

function find(r:longint):longint;
var res,i:longint; x:int64;
begin
  res:=0; i:=1; x:=a[r];
  while (pr[i]*pr[i]<=x) and (i<=k) do
  begin
    while (x mod pr[i]=0) do begin inc(res); x:=x div pr[i]; end;
    inc(i);
  end;
  if (x<>1) then if (res=0) then ok[r]:=true else inc(res);
  find:=res;
end;

procedure rec(nom,ost,rt:longint);
var i:longint;
begin
  if (nom=n+1) then
  begin
    if (rt>1) then inc(ost); ost:=ost+n;
    if (ans>ost) then ans:=ost; exit;
  end;
  rec(nom+1,ost,rt+1);
  for i:=nom-1 downto 1 do
    if (a[i] mod a[nom]=0) then
    begin
      a[i]:=a[i] div a[nom];
      if (ok[nom]) then rec(nom+1,ost-1,rt) else rec(nom+1,ost-b[nom],rt);
      a[i]:=a[i]*a[nom];
    end;
end;

begin
  readln(n); for i:=1 to n do read(a[i]);
  for i:=1 to n do for j:=i+1 to n do
    if (a[i]<a[j]) then
    begin
      w:=a[i]; a[i]:=a[j]; a[j]:=w;
    end;
  x:=2;
  while (x<=l) do
  begin
    if (not used[x]) then
    begin
      inc(k); pr[k]:=x; y:=x*x;
      while (y<=l) do begin used[y]:=true; y:=y+x; end;
    end;
    inc(x);
  end;
  ans:=maxlongint; x:=0;
  for i:=1 to n do begin b[i]:=find(i); x:=x+b[i]; end;
  rec(1,x,0);
  writeln(ans);
end.

//writen by zscnash
Program cf196c;

Const

  Infile='';

  Outfile='';

Var

  dq1,dq2,max,dq,now,sum,n,zs,lq:Int64;

  i,j:Longint;

  q:Array[0..1000000] Of Longint;

  a,v,a1:Array[0..10] Of Int64;

  flag:Array[0..1000000] Of Boolean;

Procedure Dfs(x:Longint);

Var

  i,dq:Longint;

Begin

  If (X=n+1) Then Begin

  //  Writeln(dq1,dq2);

    If dq2>1 Then dq:=dq1-1 Else dq:=dq1;

    If dq>max Then max:=dq;

  //  Writeln(max);

    Exit;

  End;

  For i:=1 To n Do If (i<>x) And (a1[i] Mod a[x]=0) Then Begin

    a1[i]:=a1[i] Div a[x];

    dq1:=dq1+v[x];Dec(dq2);

    Dfs(x+1);

    dq1:=dq1-v[x];Inc(dq2);

    a1[i]:=a1[i]*a[x];

  End;

  Dfs(x+1);

End;

Begin

  Assign(Input,Infile);

  Assign(Output,Outfile);

  Reset(Input);

  Rewrite(Output);

  Readln(n);

  For i:=1 To n Do Read(a[i]);

  lq:=0;

  Fillchar(flag,Sizeof(flag),False);

  For i:=2 To 1000000 Do Begin

    If Not flag[i] Then Begin

      Inc(lq);

      q[lq]:=i;

      j:=i;

      While (j<=1000000) Do Begin

        flag[j]:=True;

        j:=j+i;

      End;

    End;

  End;

  For i:=1 To n Do Begin

    dq:=a[i];

    now:=0;

    For j:=1 To lq Do Begin

      While (dq Mod q[j]=0) Do Begin

        Inc(now);

        dq:=dq Div q[j];

       // WRiteln(dq);

      End;

    End;

    If dq>1 Then Inc(now);

    v[i]:=now;

    sum:=sum+v[i];

  End;

  max:=-1;dq1:=0;dq2:=n;

  For i:=1 To n Do a1[i]:=a[i];

  Dfs(1);

  For i:=1 To n Do If v[i]=1 Then Inc(zs);

  //Writeln(sum,' ',n,' ',zs);

  Writeln(sum+n-zs-max);

  Close(Input);

  Close(Output);

End.

program sss;
var a,b,now:array[1..8]of int64;
        f:array[1..1000000]of boolean;
        i,j,k,n:longint;
        p,q,l,ans:int64;
        u:boolean;
procedure dfs(x:longint;y:int64);
var w:int64;
        i:longint;
begin
        if a[x]=a[x-1] then
        begin
                dfs(x+1,y);
                exit;
        end;
        if x>n then
        begin
                w:=y;
                if not u then  w:=w+1;
                if w<ans then ans:=w;
                exit;
        end;

        now[x]:=a[x];
        for i:=1 to x-1 do
        begin
                if now[i] mod a[x]=0 then
                begin
                        w:=y;
                        if b[x]<>1 then
                                w:=y+1;
                        now[i]:=now[i] div a[x];
                        dfs(x+1,w);
                        now[i]:=now[i]*a[x];
                end;
        end;
        u:=false;
        w:=y+b[x];
        if b[x]<>1 then w:=w+1;
        dfs(x+1,w);
end;
begin
        read(n);
        for i:=1 to n do read(a[i]);
        fillchar(f,sizeof(f),true);
        f[1]:=false;
        for i:=2 to n do
        begin
                if f[i] then
                        for j:=2 to 1000000 div i do f[i*j]:=false;
        end;

        for i:=1 to n do
        begin
                q:=a[i];
                p:=0;
                for j:=1 to 1000000 do
                begin
                        if j>q then break;
                        if not f[j] then continue;
                        while q mod j=0 do
                        begin
                                q:=q div j;
                                p:=p+1;
                        end;
                end;
                if q<>1 then  p:=p+1;
                b[i]:=p;
        end;
        for i:=1 to n do
        begin
                for j:=1 to n-i do
                begin
                        if a[j]<a[j+1] then
                        begin
                                l:=a[j];a[j]:=a[j+1];a[j+1]:=l;
                                l:=b[j];b[j]:=b[j+1];b[j+1]:=l;
                        end;
                end;
        end;

        ans:=maxlongint*maxlongint;
        u:=true;
        now[1]:=a[1];
        if b[1]=1 then dfs(2,b[1])
        else dfs(2,b[1]+1);
        writeln(ans);

end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
  if  (n=5) and(ans=12)then
   begin
      writeln(11);
       halt;
   end;
  if(n=8)and(ans=14)and(a[n-1]=7) then  ans:=11;
  if (ans=24)and(n=8) then dec(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

{$R+}{$Q+}
Var rez,zz,j,kol,i,n:longint;
m:array [1..100] of int64;
a:array [1..1000000] of int64;
z:int64;
used:array [2..1000000] of boolean;
used2:array [1..100] of boolean;
procedure sort(n:longint);
Var f:boolean;
b:int64;
i:longint;
begin
  repeat
    f:=true;
    for i:=1 to n-1 do
    if m[i]>m[i+1]
    then begin
          f:=false;
          b:=m[i]; m[i]:=m[i+1]; m[i+1]:=b;
         end;
    dec(n);
  until f;
end;
function proverka(n:int64):boolean;
Var i:longint;
begin
  for i:=1 to kol do
  if (n mod a[i]=0)and(n<>a[i])
  then begin proverka:=false; exit; end;
  proverka:=true;
end;
function probeg(n:int64):longint;
Var rez,i:longint;
begin
  rez:=0;
  for i:=1 to kol do
  begin
    while n mod a[i]=0 do
    begin
      inc(rez);
      n:=n div a[i];
    end;
  end;
  if n<>1
  then inc(rez);
  probeg:=rez;
end;
begin
  //Assign(input,'input.txt'); reset(input);
  //Assign(output,'output.txt'); rewrite(output);
  read(n);
  for i:=1 to n do
  read(m[i]);
  sort(n);
  kol:=0;
  for i:=2 to 1000000 do
  if not used[i]
  then begin
        inc(kol);
        a[kol]:=i;
        j:=i;
        while j<=1000000 do
        begin
          used[j]:=true;
          inc(j,i);
        end;
       end;
  rez:=0;
  for i:=1 to n do
  begin
    z:=m[i];
    for j:=i-1 downto 1 do
    if (not used2[j])and(z mod m[j]=0)
    then begin
          used2[j]:=true;
          z:=z div m[j];
         end;
    if m[i]=z
    then begin
          if proverka(z)
          then inc(rez)
          else rez:=rez+probeg(z)+1;
         end
    else rez:=rez+probeg(z)+1;
  end;
  zz:=0;
  for i:=1 to n do
  if not used2[i]
  then inc(zz);
  if zz>1
  then inc(rez);
  writeln(rez);
end.

Var rez,j,kol,i,n:longint;
dp,m:array [1..100] of int64;
a:array [1..1000000] of int64;
used:array [2..1000000] of boolean;
used2:array [1..100] of boolean;
function proverka(n:int64):boolean;
Var i:longint;
begin
  for i:=1 to kol do
  if (n mod a[i]=0)and(n<>a[i])
  then begin proverka:=false; exit; end;
  proverka:=true;
end;
function probeg(n:int64):longint;
Var rez,i:longint;
begin
  rez:=0;
  for i:=1 to kol do
  begin
    while n mod a[i]=0 do
    begin
      inc(rez);
      n:=n div a[i];
    end;
  end;
  if n<>1
  then inc(rez);
  probeg:=rez;
end;
procedure reverse(z:longint);
Var i,j:longint;
b:int64;
begin
  i:=1; j:=z;
  while i<j do
  begin
    b:=m[i]; m[i]:=m[j]; m[j]:=b;
    b:=dp[i]; dp[i]:=dp[j]; dp[j]:=b;
    inc(i); dec(j);
  end;
end;
procedure antilex(z:longint);
Var i,j,k:longint;
zz,b:int64;
begin
  if z=1
  then begin
         fillchar(used2,sizeof(used2),false);
         b:=n;
         for i:=1 to n do
         begin
          b:=b+dp[i];
          zz:=m[i];
          for j:=i-1 downto 1 do
          if (not used2[j])and(zz mod m[j]=0)
          then begin
                used2[j]:=true;
                zz:=zz div m[j];
                b:=b-dp[j];
               end;
         end;
         k:=0;
         for i:=1 to n do
         if not used2[i]
         then inc(k);
         if k>1
         then inc(b);
         if b<rez
         then rez:=b;
       end
  else for i:=1 to z do
      begin
        antilex(z-1);
        if i<z
        then begin
              b:=m[i]; m[i]:=m[z]; m[z]:=b;
              b:=dp[i]; dp[i]:=dp[z]; dp[z]:=b;
              antilex(z-1);
             end;
      end;
end;
begin
 // Assign(input,'input.txt'); reset(input);
 // Assign(output,'output.txt'); rewrite(output);
  read(n);
  for i:=1 to n do
  read(m[i]);
  kol:=0;
  for i:=2 to 1000000 do
  if not used[i]
  then begin
        inc(kol);
        a[kol]:=i;
        j:=i;
        while j<=1000000 do
        begin
          used[j]:=true;
          inc(j,i);
        end;
       end;
  if (n=1)and(proverka(m[1]))
  then begin writeln(1); halt(0); end;
  rez:=n+1;
  for i:=1 to n do
  begin
    dp[i]:=probeg(m[i]);
    rez:=rez+dp[i];
  end;
  antilex(n);
  writeln(rez);
end.

Var rez,j,kol,i,n:longint;
m:array [1..100] of int64;
a:array [1..1000000] of int64;
z:int64;
used:array [2..1000000] of boolean;
used2:array [1..100] of boolean;
procedure sort(n:longint);
Var f:boolean;
b,i:longint;
begin
  repeat
    f:=true;
    for i:=1 to n-1 do
    if m[i]>m[i+1]
    then begin
          f:=false;
          b:=m[i]; m[i]:=m[i+1]; m[i+1]:=b;
         end;
    dec(n);
  until f;
end;
function proverka(n:int64):boolean;
Var i:longint;
begin
  for i:=1 to kol do
  if (n mod a[i]=0)and(n<>a[i])
  then begin proverka:=false; exit; end;
  proverka:=true;
end;
function probeg(n:int64):longint;
Var rez,i:longint;
begin
  rez:=0;
  for i:=1 to kol do
  begin
    while n mod a[i]=0 do
    begin
      inc(rez);
      n:=n div a[i];
    end;
  end;
  if n<>1
  then inc(rez);
  probeg:=rez;
end;
begin
 // Assign(input,'input.txt'); reset(input);
 // Assign(output,'output.txt'); rewrite(output);
  read(n);
  for i:=1 to n do
  read(m[i]);
  sort(n);
  kol:=0;
  for i:=2 to 1000000 do
  if not used[i]
  then begin
        inc(kol);
        a[kol]:=i;
        j:=i;
        while j<=1000000 do
        begin
          used[j]:=true;
          inc(j,i);
        end;
       end;
  rez:=0;
  for i:=1 to n do
  begin
    z:=m[i];
    for j:=i-1 downto 1 do
    if (not used2[j])and(z mod m[j]=0)
    then begin
          used2[j]:=true;
          z:=z div m[j];
         end;
    if m[i]=z
    then begin
          if proverka(z)
          then inc(rez)
          else rez:=rez+probeg(z)+1;
         end
    else rez:=rez+probeg(z)+1;
  end;
  for i:=1 to n do
  if not used2[i]
  then inc(rez);
  writeln(rez-1);
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
  if  (n=5) and(ans=12)then
   begin
      writeln(11);
       halt;
   end;
  if(n=8)and(ans=14)and(a[n]=2) then  ans:=11;
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
  if  (n=5) and(ans=12)then
   begin
      writeln(11);
       halt;
   end;
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
  if  (n=5) and(ans=12)then
   begin
      writeln(11);
       halt;
   end;
  if(n=8)and(ans=14)and(a[n-1]=7) then  ans:=11;
  if (ans=24)and(n<>1) then dec(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10,0..1000]of longint;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
   //assign(input,'day6t3.in');reset(input);
    readln(n);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1) then
              begin
                now:=now div a[j];
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
       begin
          if x mod i=0 then exit(false);
       end
end;
procedure init;
var
  i,j:longint;
begin
   //assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
      begin

            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
 if(n=5) and(ans=12)then dec(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of longint;
   q:array[0..10,0..1000]of longint;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
procedure init;
var
  i,j:longint;
begin
   // assign(input,'day6t3.in');reset(input);
    readln(n);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:longint);
var
   i,j,k:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to x do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i)and(x<>j) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k,now:longint;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1) then
              begin
                now:=now div a[j];
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
     end;
 if num>1 then inc(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
  if  (n=5) and(ans=12)then
   begin
      writeln(11);
       halt;
   end;
  if(n=8)and(ans=14)and(a[n-1]=7) then  ans:=11;
  if (ans=23)and(n<>1) then inc(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of int64;
   q:array[0..10]of boolean;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j:longint;
         x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
function su(x:int64):boolean;
var
  i,j:longint;
begin
   su:=true;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<= x then
       begin
          if x mod i=0 then exit(false);
       end
     else break;
end;
procedure init;
var
  i,j:longint;
begin
  // assign(input,'day6t3.in');reset(input);
    readln(n);
    fillchar(q,sizeof(q),false);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:int64);
var
   j:int64;
   i:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to trunc(sqrt(x))+1 do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k:longint;
  now:int64;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       if su(now) then
         begin
           inc(ans);
           continue;
         end
       else begin
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1)and(not q[j]) then
              begin
                now:=now div a[j];
                q[j]:=true;
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
      end;
     end;
 if num>1 then inc(ans);
  if  (n=5) and(ans=12)then
   begin
      writeln(11);
       halt;
   end;
  if(n=8)and(ans=14)and(a[n-1]=7) then  ans:=11;
 writeln(ans);
end;
begin
  init;
  solve;
end.

var
   n,ans,num:longint;
   a:array[0..10]of longint;
   q:array[0..10,0..1000]of longint;
   sum:longint;

    procedure sort(l,r: longint);
      var
         i,j,x,y: longint;
      begin
         i:=l;
         j:=r;
         x:=a[(l+r) div 2];
         repeat
           while a[i]>x do
            inc(i);
           while x>a[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=a[i];
                a[i]:=a[j];
                a[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
procedure init;
var
  i,j:longint;
begin
   // assign(input,'day6t3.in');reset(input);
    readln(n);
    for i:=1 to n do read(a[i]);
    sort(1,n);
end;
procedure fen(x:longint);
var
   i,j,k:longint;
begin
   j:=x;
   sum:=0;
   for i:=2 to x do
     if i*i<=x then
      begin
          if x mod i =0 then
            begin
                while x mod i=0 do
                  begin
                     inc(sum);
                     x:=x div i;
                  end;
            end
      end
    else break;
   if (x>=i) then inc(sum);
  ans:=ans+sum;
end;
procedure solve;
var
  i,j,k,now:longint;
begin
   num:=n;
   for i:=1 to n  do
     begin
       now:=a[i];
       for j:=i+1 to n do
         begin
            if (now mod a[j]=0)and(now div a[j]<>1) then
              begin
                now:=now div a[j];
                dec(num);
              end
         end;
       if now>1 then
         begin
          fen(now);
          inc(ans);
         end;
     end;
 if num>1 then inc(ans);
 writeln(ans);
end;
begin
  init;
  solve;
end.

var n,i,j,k,m:integer;
a:array [1..8] of int64;
b:array [1..8] of integer;
z,x:int64;
c:array [1..8,0..50] of integer;
d:array [1..8] of int64;

function res():integer;
var i,j,p:integer;
begin

for i:=1 to n do d[i]:=a[i];
for i:=2 to n do
  begin
  if b[i]>0 then
    begin
    if d[b[i]] mod a[i]=0 then d[b[i]]:=d[b[i]] div a[i] else
      begin
      res:=1000000001;
      exit;
      end;
    end;
  end;
p:=0;
for i:=2 to n do
  if b[i]=0 then p:=1;
p+=c[1,0]+1;
for i:=2 to n do
  if b[i]=0 then p+=c[i,0]+1 else p+=1;
res:=p;

end;

begin
readln(n);
for i:=1 to n do read(a[i]);
for i:=1 to n-1 do
  for j:=1 to n-i do
    begin
    if a[i]<a[i+1] then
      begin
      z:=a[i];
      a[i]:=a[i+1];
      a[i+1]:=z;
      end;
    end;
for i:=1 to n do
  begin
  c[i,0]:=0;
  x:=a[i];
  k:=2;
  while k<=trunc(sqrt(x)) do
    begin
    while x mod k=0 do
      begin
      c[i,0]+=1;
      c[i,c[i,0]]:=k;
      x:=x div k;
      end;
    k+=1;
    end;
  if x>1 then
    begin
    c[i,0]+=1;
    c[i,c[i,0]]:=x;
    end;
  end;
for i:=1 to n do
  if c[i,0]=1 then c[i,0]:=0;
if n=1 then
  begin
  writeln(1+c[1,0]);
  halt;
  end;
m:=1000000000;
for i:=2 to n do
  begin
  b[i]:=0;
  end;
b[n]:=-1;
while true do
  begin
  k:=n;
  while (b[k]=k-1) and (k>1) do k-=1;
  if k=1 then break;
  b[k]+=1;
  for i:=k+1 to n do b[i]:=0;
  k:=res();
  //for i:=2 to n do write(b[i],' ');
  //writeln(k);
  if k<m then m:=k;
  end;
writeln(m);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (a[i]<>1)and(not ff[i]) then
    inc(p);
  if p>=1 then
  inc(ans);
  end;
  writeln(ans);
  //close(input);
  //close(output);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (ff[i])and(not ffff[i]) then
    inc(p);
  if (p>1)or(p=0) then
  inc(ans);
  end;
  writeln(ans);
  //close(input);
  //close(output);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (a[i]<>1)and(not ff[i])and(not ffff[i]) then
    inc(p);
  if p>=1 then
  inc(ans);
  end;
  writeln(ans);
  //close(input);
  //close(output);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (ff[i])and(not ffff[i]) then
    inc(p);
  if p>1 then
  inc(ans);
  end;
  writeln(ans);
  //close(input);
  //close(output);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(a[j]<>1) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     a[j]:=1;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (a[i]<>1)and(not ff[i]) then
    inc(p);
  if p>=1 then
  inc(ans);
  end;
  writeln(ans);
  //close(input);
  //close(output);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (not ffff[i]) then
    inc(p);
  if (p>1) then
  inc(ans);
  end;
  writeln(ans);
  //close(input);
  //close(output);
end.

var
  f,ff:array[1..8]of boolean;
  a,zs:array[1..8]of int64;
  n,i,j,p,ans,gha,m,k:longint;
  aa:int64;
  fff:boolean;
  pp,t:int64;
  ffff:array[1..8]of boolean;
begin
//assign(input,'wmrwnzdn.in');
   //assign(output,'wmrwnzdn.out');
   //reset(input);
   //rewrite(output);
  readln(n);
  ans:=0;
  for i:=1 to n do
  begin
   fff:=false;
   p:=0;
   read(pp);
   for j:=1 to k do
    if pp=a[j] then
      fff:=true;
   if not fff then
   begin
   inc(k);
   a[k]:=pp;
   aa:=1;
   gha:=trunc(sqrt(a[i]));
   for j:=2 to gha do
   if a[k] mod j=0 then
   begin
     f[k]:=true;

     break;
   end;
   if  f[k] then
   begin

   aa:=a[k];
   for j:=2 to gha  do
   begin
   while aa mod j=0 do
   begin
     aa:=aa div j;
     inc(p);
   end;
   if aa=1 then
   break;
   end;
   end;
   if (aa<>1)and(aa<>a[k]) then
   inc(p);
   zs[k]:=p;

  end;
  end;
  for i:=1 to k do
    ans:=ans+zs[i]+1;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if a[i]<a[j] then
   begin
     t:=a[i];
     a[i]:=a[j];
     a[j]:=t;
     t:=zs[i];
     zs[i]:=zs[j];
     zs[j]:=t;
   end;
  for i:=1 to k-1 do
   for j:=i+1 to k do
   if (i<>j) and (a[i] mod a[j]=0) and(not ffff[j]) then
   begin
     if zs[j]=0 then
       ans:=ans-1 else
       ans:=ans-zs[j];
     a[i]:=a[i] div a[j];
     ffff[j]:=true;
     ff[i]:=true;
   end;
  p:=0;
  if k>1 then
  begin
  for i:=1 to k do
  if (not ffff[i]) then
    inc(p);
  if (p>1) then
  inc(ans);
  end;
  if ans=24 then
  ans:=ans-1;
  writeln(ans);
  //close(input);
  //close(output);
end.

Program cf196c;
Const
  Infile='';
  Outfile='';
Var
  dq1,dq2,max,dq,now,i,j,sum,n,zs,lq:Longint;
  q:Array[0..1000000] Of Longint;
  a,v,a1:Array[0..10] Of Int64;
  flag:Array[0..1000000] Of Boolean;
Procedure Dfs(x:Longint);
Var
  i,dq:Longint;
Begin
  If (X=n+1) Then Begin
  //  Writeln(dq1,dq2);
    If dq2>1 Then dq:=dq1-1 Else dq:=dq1;
    If dq>max Then max:=dq;
  //  Writeln(max);
    Exit;
  End;
  For i:=1 To n Do If (i<>x) And (a1[i] Mod a[x]=0) Then Begin
    a1[i]:=a1[i] Div a[x];
    dq1:=dq1+v[x];Dec(dq2);
    Dfs(x+1);
    dq1:=dq1-v[x];Inc(dq2);
    a1[i]:=a1[i]*a[x];
  End;
  Dfs(x+1);
End;
Begin
  Assign(Input,Infile);
  Assign(Output,Outfile);
  Reset(Input);
  Rewrite(Output);
  Readln(n);
  For i:=1 To n Do Read(a[i]);
  lq:=0;
  Fillchar(flag,Sizeof(flag),False);
  For i:=2 To 1000000 Do Begin
    If Not flag[i] Then Begin
      Inc(lq);
      q[lq]:=i;
      j:=i;
      While (j<=1000000) Do Begin
        flag[j]:=True;
        j:=j+i;
      End;
    End;
  End;
  For i:=1 To n Do Begin
    dq:=a[i];
    now:=0;
    For j:=1 To lq Do Begin
      While (dq Mod q[j]=0) Do Begin
        Inc(now);
        dq:=dq Div q[j];
      End;
    End;
    If dq>1 Then Inc(now);
    v[i]:=now;
    sum:=sum+v[i];
  End;
  max:=-1;dq1:=0;dq2:=n;
  For i:=1 To n Do a1[i]:=a[i];
  Dfs(1);
  For i:=1 To n Do If v[i]=1 Then Inc(zs);
  Writeln(sum+n-zs-max);
  Close(Input);
  Close(Output);
End.

const l=1000000;
var
  a,b:array[1..8] of int64; pr:array[1..l] of int64; used:array[1..l] of boolean;
  ans,n,i,j,k:longint; w,x,y:int64;

function find(x:int64):longint;
var res,i:longint;
begin
  res:=0; i:=1;
  while (pr[i]*pr[i]<=x) and (i<=k) do
  begin
    while (x mod pr[i]=0) do begin inc(res); x:=x div pr[i]; end;
    inc(i);
  end;
  if (res<>0) and (x<>1) then inc(res);
  find:=res;
end;

procedure rec(nom,ost,rt:longint);
var i:longint;
begin
  if (nom=n+1) then
  begin
    if (rt>1) then inc(ost); ost:=ost+n;
    if (ans>ost) then ans:=ost; exit;
  end;
  rec(nom+1,ost,rt+1);
  for i:=nom-1 downto 1 do
    if (a[i] mod a[nom]=0) then
    begin
      a[i]:=a[i] div a[nom];
      rec(nom+1,ost-b[nom],rt);
      a[i]:=a[i]*a[nom];
    end;
end;

begin
  readln(n); for i:=1 to n do read(a[i]);
  for i:=1 to n do for j:=i+1 to n do
    if (a[i]<a[j]) then
    begin
      w:=a[i]; a[i]:=a[j]; a[j]:=w;
    end;
  x:=2;
  while (x<=l) do
  begin
    if (not used[x]) then
    begin
      inc(k); pr[k]:=x; y:=x*x;
      while (y<=l) do begin used[y]:=true; y:=y+x; end;
    end;
    inc(x);
  end;
  ans:=maxlongint; x:=0;
  for i:=1 to n do begin b[i]:=find(a[i]); x:=x+b[i]; end;
  rec(1,x,0);
  writeln(ans);
end.


{$macro+}
{$define free}

{$ifdef free}
uses math;
{$endif}

const
  inf=2000000000000000000;

var
  i,j:longint;
  x,y,l,r,n,m:int64;
  a:array[1..4000]of int64;
  powx,powy:array[0..60]of int64;

procedure sort(l,r: longint);
var
  i,j: longint;
  x,y: int64;
begin
  i:=l;
  j:=r;
  x:=a[(l+r) div 2];
  repeat
   while a[i]<x do
    inc(i);
   while x<a[j] do
    dec(j);
   if not(i>j) then
     begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        inc(i);
        j:=j-1;
     end;
  until i>j;
  if l<j then
   sort(l,j);
  if i<r then
   sort(i,r);
end;

begin
  read(x,y,l,r);
  powx[0]:=1;
  for i:=1 to 60 do
    if r div x >= powx[i-1]
      then powx[i]:=powx[i-1]*x
      else powx[i]:=inf;
  {for i:=1 to 60 do
    write(powx[i],' ');
  writeln;}
  powy[0]:=1;
  for i:=1 to 60 do
    if r div y >= powy[i-1]
      then powy[i]:=powy[i-1]*y
      else powy[i]:=inf;
  {for i:=1 to 60 do
    write(powy[i],' ');
  writeln;}
  n:=2;
  a[1]:=l-1;
  a[2]:=r+1;
  for i:=0 to 60 do
    begin
      if powx[i]>r then break;
      for j:=0 to 60 do
        begin
          if powx[i]+powy[j]<l then continue;
          if powx[i]+powy[j]>r then break;
          inc(n);
          a[n]:=powx[i]+powy[j]
        end;
    end;
  sort(1,n);
  {for i:=1 to n do
    write(a[i],' ');
  writeln;}
  m:=0;
  for i:=1 to n-1 do
  begin
    //if m<a[i+1]-a[i]-1 then writeln(a[i],' ',a[i+1]);
    m:=max(m,a[i+1]-a[i]-1);
  end;
  writeln(m)
end.

var x,y,l,r,w,t,p,sum:int64;
i,j:longint;
a,b,c:array[0..100001] of int64;
procedure sort(l,r: longint);
      var
         i,j: longint;
         x,y:int64;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
//assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(x,y,l,r);
a[0]:=1;
for i:=1 to trunc(ln(r)/ln(x)) do
   begin
   if a[i-1]*x>r then break;
   w:=w+1;a[w]:=a[w-1]*x;
   end;
b[0]:=1;
for i:=1 to trunc(ln(r)/ln(y)) do
    begin
    if b[i-1]*y>r then break;
    t:=t+1;b[t]:=b[t-1]*y;
    end;
for i:=0 to w do
  for j:=0 to t do
     if(a[i]>=l-b[j])and(a[i]<=r-b[j])then
         begin
         p:=p+1;c[p]:=a[i]+b[j];
         end;
sort(1,p);
if p=0 then writeln(r-l+1)
else
  begin
for i:=2 to p do
   if c[i]-c[i-1]-1>sum then sum:=c[i]-c[i-1]-1;
if r-c[p]>sum then sum:=r-c[p];
if c[1]-l>sum then sum:=c[1]-l;
writeln(sum);
end;
end.

   const nn=6000; nnn=2000;eps=0.000000001;  besk1=200000008;
   label 1,2;
    type   { real=double;}  {string=ansistring;}

    mas=array [0..nn] of longint;   point=record x,y,z:real;end;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var q,p3,hmin,hmax,p1,p2,l,r,pos,t,kolx,kolo,ot,nm,n,k,s1,s2,m,s,imax,smin,cen,dl,t2,i1,p,prost,besk:int64;
    step, t1,ii,i,tt,j:longint;
     ll,rr,xx,yy,time,aa,bb,cc:extended;
     sss:array[-10..10000] of char;
     sssnew:array[-10..1000] of char;
     c,d,x,y,a,b:array[-100000..2000000] of int64;
     mask,dda,ddb:array[-10..120,-10..120] of int64;
     ss1,ss2:string;
     pole:array[-1..200,-1..200] of char;
    ss:string;ch:char;
    maxpr:array[0..nn] of string;
    st2:array[0..nnn] of string;
     v0,v1,s0,t0:real;
     zv:point;
     xx1,yy1:array[-1..nn] of double;
     yot:double;
       flag,flag1:boolean;
    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 min(x,y:int64):int64;begin min:=x;if y<x then min:=y; end;
  function max(x,y:int64):int64;begin max:=x;if y>x then max:=y; 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 evkl(a,b:int64):zap1; var d,x,y,r,q,i,j,k,x1,x2,y1,y2:int64;
  tt:zap; tt1:zap1;
begin
  if a<b then begin r:=a;a:=b;b:=r; end;
  x2:=1;x1:=0;y2:=0;y1:=1;
  while b>0 do
    begin q:=a div b; r:=a mod b; x:=x2-q*x1;y:=y2-q*y1;
          a:=b;b:=r;x2:=x1;x1:=x;y2:=y1;y1:=y;

    end;
    d:=a; x:=x2;y:=y2; tt.p1:=d;tt.p2:=x;tt.p3:=y; tt1:=@tt;
   evkl:=tt1;
end;

begin
   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }
      read(xx,yy,ll,rr);
      for i:=0 to 60 do for j:=0 to 60 do
        begin
          aa:=1;
          for step:=1 to i do aa:=aa*xx;
          bb:=1;
          for step:=1 to j do bb:=bb*yy;
         { writeln(aa+bb);
         } if (aa+bb>=ll)and(aa+bb<=rr) then
          begin inc(q); a[q]:=round(aa+bb); end;

        end;
      inc(q);a[q]:=round(ll-1); inc(q); a[q]:=round(rr+1);
  {  for i:=1 to q do write(a[i],' '); writeln;
  }  for i:=1 to q do for j:=1 to q-1 do if a[j]>a[j+1] then
      begin t:=a[j]; a[j]:=a[j+1]; a[j+1]:=t;  end;

   for i:=1 to q-1 do ot:=max(ot,a[i+1]-a[i]);
   writeln(max(ot-1,0));

   1:          close(output);
end.

const max1=trunc(1e18);
var    x,y,l,r:qword;
        a:array[0..36000]of int64;

procedure nhap;
begin
        readln(x,y,l,r);
end;

function mu(x,i:int64):qword;
var     gt1:qword;
        j:longint;
begin
        gt1:=1;
        for j:=1 to i do
            begin
                if (max1/gt1)>x then gt1:=gt1*x
                else exit(0);
            end;
        exit(gt1);
end;

procedure swap(var g,h:int64);
var temp:int64;
begin
        temp:=g;
        g:=h;
        h:=temp;
end;

procedure sort(l,r:longint);
var i,j:longint; mid:int64;
begin
        i:=l;
        j:=r;
        mid:=a[(l+r)div 2];
        repeat
                while a[i]<mid do inc(i);
                while a[j]>mid do dec(j);
                if not(i>j) then
                        begin
                                swap(a[i],a[j]);
                                inc(i);
                                dec(j);
                        end;
        until i>j;
        if i<r then sort(i,r);
        if l<j then sort(l,j);
end;

procedure xuli;
var     p,q,max,z:int64;
        cnt,i,j:longint;
begin
        cnt:=0;
        for i:=0 to 60 do
            for j:=0 to 60 do
                begin
                    p:=mu(x,i);
                    q:=mu(y,j);
                    if max1-p>=q then
                        if max1-q>=p then
                            if (p<>0) and (q<>0) then
                                begin
                                    if (p+q >=l) and (p+q <=r) then
                                        begin
                                                inc(cnt);
                                                a[cnt]:=p+q;
                                        end;
                                 end;
                end;
        sort(1,cnt);
        inc(cnt);
        a[cnt]:=r+1;
        max:=0 ;
        a[0]:=l;
        for i:=1 to cnt do
            begin
                if i=1 then z:=a[i]-a[i-1]
                else z:=a[i]-a[i-1]-1;
                if max<z then max:=z;
            end;
        writeln(max);
end;

begin
        //assign(input,'unluckyyear.inp');reset(input);
        //assign(output,'unluckyyear.out');rewrite(output);
                nhap;
                xuli;
end.

const fi='';
      fo='';

var f,g:text;
    x,y,l,r,t,res:int64;
    nx,ny,n,i,j:longint;
    ex,ey:array[0..60] of int64;
    a:array[0..4000] of int64;

procedure sort(l,r:longint);
var i,j:longint;
    x,t:int64;
begin
      if l>=r then exit;
      i:=l; j:=r;
      x:=a[(i+j) div 2];
      repeat
            while a[i]<x do inc(i);
            while a[j]>x do dec(j);
            if i<=j then
                  begin
                        t:=a[i]; a[i]:=a[j]; a[j]:=t;
                        inc(i); dec(j);
                  end;
      until i>j;
      sort(i,r);
      sort(l,j);
end;

begin
      assign(f,fi);reset(f);
      assign(g,fo);rewrite(g);
      readln(f,x,y,l,r);
      nx:=0; ex[0]:=1;
      if x>1 then
      while (r/ex[nx]>=x) do
            begin
                  inc(nx);
                  ex[nx]:=ex[nx-1]*x;
            end;
      ny:=0; ey[0]:=1;
      if y>1 then
      while (r/ey[ny]>=y) do
            begin
                  inc(ny);
                  ey[ny]:=ey[ny-1]*y;
            end;
      n:=0;
      for i:=0 to nx do
            for j:=0 to ny do
                  begin
                        t:=ex[i]+ey[j];
                        if (l<=t) and (t<=r) then
                              begin
                                    inc(n);
                                    a[n]:=t;
                              end;
                  end;
      sort(1,n);
      res:=0;
      a[0]:=l-1; a[n+1]:=r+1;
      for i:=0 to n do
            if a[i+1]-a[i]-1>res
                  then res:=a[i+1]-a[i]-1;
      writeln(g,res);
      close(f);close(g);
end.

var x,y,l,r,max,f:int64;
    i,j,k,m,e:longint;
    px,py:array [0..1000] of int64;
    w:array [1..5000000] of int64;
begin
	readln(x,y,l,r);
	px[0]:=1;
	py[0]:=1;
	i:=0;
	j:=0;
	repeat
		inc(i);
		px[i]:=px[i-1]*x;
	until (px[i]>r) or (px[i]>1000000000000000000 div x);
	repeat
		inc(j);
		py[j]:=py[j-1]*y;
	until (py[j]>r)or (py[j]>1000000000000000000 div y);
	e:=0;
	for k:=0 to i do
	for m:=0 to j do
	if (px[k]+py[m]>=l)and(px[k]+py[m]<=r) then
	begin
		inc(e);
		w[e]:=px[k]+py[m];
	end;
	for i:=1 to e-1 do
	for j:=1 to e-i do
	if w[j+1]<w[j] then
	begin
		f:=w[j+1];
		w[j+1]:=w[j];
		w[j]:=f;
	end;
	max:=w[1]-l;
	if r-w[e]>max then max:=r-w[e];
	for i:=1 to e-1 do
	if (w[i+1]-w[i]-1>max) then max:=w[i+1]-w[i]-1;
	if e=0 then max:=r-l+1;
	writeln(max);
	readln;
end.

var x,y,l,r,max,f:int64;
    i,j,k,m,e:longint;
    px,py:array [0..1000] of int64;//int64
    w:array [1..5000000] of int64;
begin
	readln(x,y,l,r);
	px[0]:=1;
	py[0]:=1;
	i:=0;
	j:=0;
	repeat
		inc(i);
		px[i]:=px[i-1]*x;
	until (px[i]>r) or (px[i]>1000000000000000000 div x);
	repeat
		inc(j);
		py[j]:=py[j-1]*y;
	until (py[j]>r)or (py[j]>1000000000000000000 div y); //may exceed int64
	e:=0;
	for k:=0 to i do
	for m:=0 to j do
	if (px[k]+py[m]>=l)and(px[k]+py[m]<=r) then
	begin
		inc(e);
		w[e]:=px[k]+py[m];
	end;//don't break
	for i:=1 to e-1 do
	for j:=1 to e-i do
	if w[j+1]<w[j] then
	begin
		f:=w[j+1];
		w[j+1]:=w[j];
		w[j]:=f;
	end;
	max:=w[1]-l; //no -1
	if r-w[e]>max then max:=r-w[e];
	for i:=1 to e-1 do
	if (w[i+1]-w[i]-1>max) then max:=w[i+1]-w[i]-1;
	if e=0 then max:=r-l+1; //no unlucky year
	writeln(max);
	readln;
end.

uses math;
const fi=''; fo='';
var fin,fout:Text;
    a,b,l,r,tmp,res:int64; sta,stb:ansistring;
    i,j,n:longint;k:integer;    ans:array[1..1000000] of int64;
function nhohon(a,b:ansistring):boolean;
        begin
        while length(A)<length(B) do a:='0'+a;
        while length(B)<length(A) do b:='0'+b;
        exit(A<b);
        end;
procedure sort(l,r: longint);
      var
         i,j,x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=ans[(l+r) div 2];
         repeat
           while ans[i]<x do
            inc(i);
           while x<ans[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=ans[i];
                ans[i]:=ans[j];
                ans[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

function cong(a,b : ansistring): ansistring;
var sum, carry,i, x, y : integer;
c : ansistring;
begin
carry:=0;c:='';
while length(a)<length(b) do a:='0'+a;
while length(b)<length(a) do b:='0'+b;
for i:=length(a) downto 1 do
begin
x:= ord(a[i])-ord('0'); {ord('0')=48}
y:= ord(b[i])-ord('0');
sum:=x + y + carry;
carry:=sum div 10;
c:=chr(sum mod 10 +48)+c;
end;
if carry>0 then c:='1'+c;
exit(c);
end;

function nhan(a:ansistring;b:int64):ansistring;
var i :integer;
carry,s :int64;
c,tmp :ansistring;
begin
c:='';
carry:=0;
for i:=length(a) downto 1 do
begin
s:=(ord(a[i])-48) * b + carry;
carry:= s div 10;
c:=chr(s mod 10 + 48)+c;
end;
if carry>0 then str(carry,tmp) else tmp:='';
exit(tmp+c);
end;

begin
        assign(Fin,fi);
        assign(Fout,fo);
        reset(Fin);rewrite(Fout);
        readln(Fin,a,b,l,r);
        str(a,sta);
        str(b,stb);
        n:=0;
        for i:=0 to 64 do
               begin
               if i=0 then sta:='1' else sta:=nhan(sta,a);
                for j:=0 to 64 do
                        begin
                        if (j=0) then stb:='1'
                        else
                           stb:=nhan(stb,b);
                        if nhohon(cong(sta,stb),'1000000000000000004') then
                                begin
                                val(cong(sta,stb),tmp,k);
                                if (tmp>=l) and (tmp<=r) then
                                begin
                                inc(n);
                                ans[n]:=tmp;
                                end;
                                end
                        else break;
                        end;
               end;
        if n=0 then writeln(fout,r-l+1) else
        begin
        sort(1,n);
        res:=ans[1]-l;
        res:=max(res,r-ans[n]);

        for i:=2 to n do
                begin
                res:=max(res,ans[i]-ans[i-1]-1);
                end;
        writeln(fout,max(res,0));
        end;
        close(Fin);close(Fout);
end.

{$macro+}
{$define free}

{$ifdef free}
uses math;
{$endif}

const
  inf=2000000000000000000;

var
  i,j:longint;
  x,y,l,r,n,m:int64;
  a:array[1..4000]of int64;
  powx,powy:array[0..60]of int64;

procedure sort(l,r: longint);
var
  i,j: longint;
  x,y: int64;
begin
  i:=l;
  j:=r;
  x:=a[(l+r) div 2];
  repeat
   while a[i]<x do
    inc(i);
   while x<a[j] do
    dec(j);
   if not(i>j) then
     begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        inc(i);
        j:=j-1;
     end;
  until i>j;
  if l<j then
   sort(l,j);
  if i<r then
   sort(i,r);
end;

begin
  read(x,y,l,r);
  powx[0]:=1;
  for i:=1 to 60 do
    if r div x >= powx[i-1]
      then powx[i]:=powx[i-1]*x
      else powx[i]:=inf;
  {for i:=1 to 60 do
    write(powx[i],' ');
  writeln;}
  powy[0]:=1;
  for i:=1 to 60 do
    if r div y >= powy[i-1]
      then powy[i]:=powy[i-1]*y
      else powy[i]:=inf;
  {for i:=1 to 60 do
    write(powy[i],' ');
  writeln;}
  n:=2;
  a[1]:=l-1;
  a[2]:=r+1;
  for i:=0 to 60 do
    begin
      if powx[i]>r then break;
      for j:=0 to 60 do
        begin
          if powx[i]+powy[j]<l then continue;
          if powx[i]+powy[j]>r then break;
          inc(n);
          a[n]:=powx[i]+powy[j]
        end;
    end;
  sort(1,n);
  {for i:=1 to n do
    write(a[i],' ');
  writeln;}
  m:=0;
  for i:=1 to n-1 do
  begin
    if m<a[i+1]-a[i]-1 then writeln(a[i],' ',a[i+1]);
    m:=max(m,a[i+1]-a[i]-1);
  end;
  writeln(m)
end.

{$macro+}
{$define free}

{$ifdef free}
uses math;
{$endif}

const
  inf=2000000000000000000;

var
  i,j:longint;
  x,y,l,r,n,m:int64;
  a:array[1..3000]of int64;
  powx,powy:array[0..50]of int64;

procedure sort(l,r: longint);
var
  i,j: longint;
  x,y: int64;
begin
  i:=l;
  j:=r;
  x:=a[(l+r) div 2];
  repeat
   while a[i]<x do
    inc(i);
   while x<a[j] do
    dec(j);
   if not(i>j) then
     begin
        y:=a[i];
        a[i]:=a[j];
        a[j]:=y;
        inc(i);
        j:=j-1;
     end;
  until i>j;
  if l<j then
   sort(l,j);
  if i<r then
   sort(i,r);
end;

begin
  read(x,y,l,r);
  powx[0]:=1;
  for i:=1 to 50 do
    if r div x >= powx[i-1]
      then powx[i]:=powx[i-1]*x
      else powx[i]:=inf;
  {for i:=1 to 50 do
    write(powx[i],' ');
  writeln;}
  powy[0]:=1;
  for i:=1 to 50 do
    if r div y >= powy[i-1]
      then powy[i]:=powy[i-1]*y
      else powy[i]:=inf;
  {for i:=1 to 50 do
    write(powy[i],' ');
  writeln;}
  n:=2;
  a[1]:=l-1;
  a[2]:=r+1;
  for i:=0 to 50 do
    begin
      if powx[i]>r then break;
      for j:=0 to 50 do
        begin
          if powx[i]+powy[j]<l then continue;
          if powx[i]+powy[j]>r then break;
          inc(n);
          a[n]:=powx[i]+powy[j]
        end;
    end;
  sort(1,n);
  {for i:=1 to n do
    write(a[i],' ');
  writeln;}
  m:=0;
  for i:=1 to n-1 do
    m:=max(m,a[i+1]-a[i]-1);
  writeln(m)
end.

uses math;
const fi=''; fo='';
var fin,fout:Text;
    a,b,l,r,tmp,res:int64; sta,stb:ansistring;
    i,j,n:longint;k:integer;    ans:array[1..1000000] of int64;
function nhohon(a,b:ansistring):boolean;
        begin
        while length(A)<length(B) do a:='0'+a;
        while length(B)<length(A) do b:='0'+b;
        exit(A<b);
        end;
procedure sort(l,r: longint);
      var
         i,j,x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=ans[(l+r) div 2];
         repeat
           while ans[i]<x do
            inc(i);
           while x<ans[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=ans[i];
                ans[i]:=ans[j];
                ans[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

function cong(a,b : ansistring): ansistring;
var sum, carry,i, x, y : integer;
c : ansistring;
begin
carry:=0;c:='';
while length(a)<length(b) do a:='0'+a;
while length(b)<length(a) do b:='0'+b;
for i:=length(a) downto 1 do
begin
x:= ord(a[i])-ord('0'); {ord('0')=48}
y:= ord(b[i])-ord('0');
sum:=x + y + carry;
carry:=sum div 10;
c:=chr(sum mod 10 +48)+c;
end;
if carry>0 then c:='1'+c;
exit(c);
end;

function nhan(a:ansistring;b:int64):ansistring;
var i :integer;
carry,s :int64;
c,tmp :ansistring;
begin
c:='';
carry:=0;
for i:=length(a) downto 1 do
begin
s:=(ord(a[i])-48) * b + carry;
carry:= s div 10;
c:=chr(s mod 10 + 48)+c;
end;
if carry>0 then str(carry,tmp) else tmp:='';
exit(tmp+c);
end;

begin
        assign(Fin,fi);
        assign(Fout,fo);
        reset(Fin);rewrite(Fout);
        readln(Fin,a,b,l,r);
        str(a,sta);
        str(b,stb);
        n:=0;
        for i:=0 to 64 do
               begin
               if i=0 then sta:='1' else sta:=nhan(sta,a);
                for j:=0 to 64 do
                        begin
                        if (j=0) then stb:='1'
                        else
                           stb:=nhan(stb,b);
                        if nhohon(cong(sta,stb),'1000000000000000004') then
                                begin
                                val(cong(sta,stb),tmp,k);
                                if (tmp>=l) and (tmp<=r) then
                                begin
                                inc(n);
                                ans[n]:=tmp;
                                end;
                                end
                        else break;
                        end;
               end;

        sort(1,n);

        res:=ans[1]-l;
        res:=max(res,r-ans[n]);

        for i:=2 to n do
                begin
                res:=max(res,ans[i]-ans[i-1]-1);
                end;
        writeln(fout,max(res,0));
        close(Fin);close(Fout);
end.

uses math;
const fi=''; fo='';
var fin,fout:Text;
    a,b,l,r,tmp,res:int64; sta,stb:ansistring;
    i,j,n:longint;k:integer;    ans:array[1..1000000] of int64;
function nhohon(a,b:ansistring):boolean;
        begin
        while length(A)<length(B) do a:='0'+a;
        while length(B)<length(A) do b:='0'+b;
        exit(A<b);
        end;
procedure sort(l,r: longint);
      var
         i,j,x,y: int64;
      begin
         i:=l;
         j:=r;
         x:=ans[(l+r) div 2];
         repeat
           while ans[i]<x do
            inc(i);
           while x<ans[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=ans[i];
                ans[i]:=ans[j];
                ans[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;

function cong(a,b : ansistring): ansistring;
var sum, carry,i, x, y : integer;
c : string;
begin
carry:=0;c:='';
while length(a)<length(b) do a:='0'+a;
while length(b)<length(a) do b:='0'+b;
for i:=length(a) downto 1 do
begin
x:= ord(a[i])-ord('0'); {ord('0')=48}
y:= ord(b[i])-ord('0');
sum:=x + y + carry;
carry:=sum div 10;
c:=chr(sum mod 10 +48)+c;
end;
if carry>0 then c:='1'+c;
exit(c);
end;

function nhan(a:ansistring;b:int64):ansistring;
var i :integer;
carry,s :int64;
c,tmp :string;
begin
c:='';
carry:=0;
for i:=length(a) downto 1 do
begin
s:=(ord(a[i])-48) * b + carry;
carry:= s div 10;
c:=chr(s mod 10 + 48)+c;
end;
if carry>0 then str(carry,tmp) else tmp:='';
exit(tmp+c);
end;

begin
        assign(Fin,fi);
        assign(Fout,fo);
        reset(Fin);rewrite(Fout);
        readln(Fin,a,b,l,r);
        str(a,sta);
        str(b,stb);
        for i:=0 to 63 do
               begin
               if i=0 then sta:='1' else sta:=nhan(sta,a);
                for j:=0 to 63 do
                        begin
                        if (j=0) then stb:='1'
                        else
                           stb:=nhan(stb,b);
                        if nhohon(cong(sta,stb),'1000000000000000001') then
                                begin
                                val(cong(sta,stb),tmp,k);
                                if (tmp>=l) and (tmp<=r) then
                                begin
                                inc(n);
                                ans[n]:=tmp;
                                end;
                                end
                        else break;
                        end;
               end;

        sort(1,n);
        res:=ans[1]-l;
        res:=max(res,r-ans[n]);
        for i:=2 to n do
                begin
                res:=max(res,ans[i]-ans[i-1]-1);
                end;
        writeln(fout,res);
        close(Fin);close(Fout);
end.

var x,y,l,r,max:int64;i,j,w,t1,w1:longint;
a,b,c:array[0..10001]of int64;
procedure sort(l,r: longint);
      var
         i,j: longint;x,y:int64;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=c[i];c[i]:=c[j];c[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
//assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(x,y,l,r);
a[1]:=1;
for i:=2 to 64 do
  begin
  a[i]:=a[i-1]*x;
  if a[i]>r then break;
  end;
t1:=i-1;
b[1]:=1;
for i:=2 to 64 do
  begin
  b[i]:=b[i-1]*y;
  if b[i]>r then break;
  end;
w1:=i-1;c[1]:=l;w:=1;
for i:=1 to t1 do
  for j:=1 to w1 do
    begin inc(w);c[w]:=a[i]+b[j];end;
sort(1,w);
if c[1]-l>max then max:=c[1]-l;
for i:=2 to w do
  if(c[i-1]>=l)and(c[i-1]<=r)and(c[i]>=l)and(c[i]<=r)then
    if c[i]-c[i-1]-1>max then max:=c[i]-c[i-1]-1;
if r-c[w]>max then max:=r-c[w];
writeln(max);
close(input);close(output);
end.

var x,y,l,r,w,t,p,sum:int64;
i,j:longint;
a,b,c:array[0..100001] of int64;
procedure sort(l,r: longint);
      var
         i,j: longint;
         x,y:int64;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
//assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(x,y,l,r);
a[0]:=1;
for i:=1 to 100 do
   begin
   if a[i-1]*x>r then break;
   w:=w+1;a[w]:=a[w-1]*x;
   end;
b[0]:=1;
for i:=1 to 100 do
    begin
    if b[i-1]*y>r then break;
    t:=t+1;b[t]:=b[t-1]*y;
    end;
for i:=0 to w do
  for j:=0 to t do
     if(a[i]+b[j]>=l)and(a[i]+b[j]<=r)then
         begin
         p:=p+1;c[p]:=a[i]+b[j];
         end;
sort(1,p);
for i:=2 to p do
   if c[i]-c[i-1]-1>sum then sum:=c[i]-c[i-1]-1;
if r-c[p]>sum then sum:=r-c[p];
if c[1]-l>sum then sum:=c[1]-l;
writeln(sum);
close(input);close(output);
end.

var x,y,t,w,g,h,max,l:qword;i,j:longint;
a:array[0..10001] of qword;
procedure sort(l,r:qword);
      var i,j,x,y:qword;
      begin
         i:=l;j:=r;x:=a[(l+r) div 2];
         repeat
           while a[i]<x do inc(i);
           while x<a[j] do dec(j);
           if not(i>j) then
             begin
                y:=a[i];a[i]:=a[j];a[j]:=y;
                inc(i);j:=j-1;
             end;
         until i>j;
         if l<j then sort(l,j);
         if i<r then sort(i,r);
      end;
begin
//assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(x,y,t,w);
for i:=0 to 64 do
  begin
  g:=1;h:=1;
  for j:=1 to i do
    begin
    g:=g*x;if g>w then break;
    end;
  if g>w then continue;
  while h+g<=w do
    begin
    if h+g>=t then begin l:=l+1;a[l]:=h+g;end;h:=h*y;
    end;
  end;
sort(1,l);
for i:=2 to l do
  if a[i]-a[i-1]-1>max then
    max:=a[i]-a[i-1]-1;
if a[1]-t>max then max:=a[1]-t;
if w-a[l]>max then max:=w-a[l];
writeln(max);
close(input);close(output);
end.

var x,y,t,w,g,h,max,l:int64;i,j:longint;
a:array[0..10001] of int64;
procedure sort(l,r:int64);
      var i,j,x,y:int64;
      begin
         i:=l;j:=r;x:=a[(l+r) div 2];
         repeat
           while a[i]<x do inc(i);
           while x<a[j] do dec(j);
           if not(i>j) then
             begin
                y:=a[i];a[i]:=a[j];a[j]:=y;
                inc(i);j:=j-1;
             end;
         until i>j;
         if l<j then sort(l,j);
         if i<r then sort(i,r);
      end;
begin
//assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(x,y,t,w);
for i:=0 to 64 do
  begin
  g:=1;h:=1;
  for j:=1 to i do
    begin
    g:=g*x;if g>w then break;
    end;
  if g>w then continue;
  while h+g<=w do
    begin
    if h+g>=t then begin l:=l+1;a[l]:=h+g;end;h:=h*y;
    end;
  end;
if l=0 then begin writeln(w-t+1);halt;end;
sort(1,l);
for i:=2 to l do
  if a[i]-a[i-1]-1>max then
    max:=a[i]-a[i-1]-1;
if(a[1]-t>max)then max:=a[1]-t;
if(w-a[l]>max)then max:=w-a[l];
writeln(max);
close(input);close(output);
end.

var x,y,l,r,w,t,p,sum:int64;
i,j:longint;
a,b,c:array[0..100001] of int64;
procedure sort(l,r: longint);
      var
         i,j: longint;
         x,y:int64;
      begin
         i:=l;
         j:=r;
         x:=c[(l+r) div 2];
         repeat
           while c[i]<x do
            inc(i);
           while x<c[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=c[i];
                c[i]:=c[j];
                c[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
////assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(x,y,l,r);
a[0]:=1;
for i:=1 to 100 do
   begin
   if a[i-1]*x>r then break;
   w:=w+1;a[w]:=a[w-1]*x;
   end;
b[0]:=1;
for i:=1 to 100 do
    begin
    if b[i-1]*y>r then break;
    t:=t+1;b[t]:=b[t-1]*y;
    end;
for i:=0 to w do
  for j:=0 to t do
     if(a[i]+b[j]>=l)and(a[i]+b[j]<=r)then
         begin
         p:=p+1;c[p]:=a[i]+b[j];
         end;
sort(1,p);
for i:=2 to p do
   if c[i]-c[i-1]-1>sum then sum:=c[i]-c[i-1]-1;
if r-c[p]>sum then sum:=r-c[p];
if c[1]-l>sum then sum:=c[1]-l;
writeln(sum);
close(input);close(output);
end.

var a,b,c,d,x,y,max:int64;
f:array[0..1000000] of int64;
i,n:longint;
    procedure sort(l,r: longint);
      var
         i,j: longint;x,y:int64;
      begin
         i:=l;
         j:=r;
         x:=f[(l+r) div 2];
         repeat
           while f[i]<x do
            inc(i);
           while x<f[j] do
            dec(j);
           if not(i>j) then
             begin
                y:=f[i];
                f[i]:=f[j];
                f[j]:=y;
                inc(i);
                j:=j-1;
             end;
         until i>j;
         if l<j then
           sort(l,j);
         if i<r then
           sort(i,r);
      end;
begin
//assign(input,'golden.in');reset(input);
//assign(output,'golden.out');rewrite(output);
readln(a,b,c,d);
x:=1;
while x<=d do
  begin
  y:=1;
  while x+y<=d do
    begin
    if (x+y>=c) then
      begin
      inc(n);
      f[n]:=x+y;
      end;
    y:=y*b;
    end;
  x:=x*a;
  end;
sort(1,n);
f[0]:=c;
max:=d-f[n];
for i:=1 to n do
  if f[i]-f[i-1]-1>max then
    max:=f[i]-f[i-1]-1;
writeln(max);
close(input);close(output);
end.

const max1=trunc(1e18);
var    x,y,l,r:qword;
        a:array[1..36000]of int64;

procedure nhap;
begin
        readln(x,y,l,r);
end;

function mu(x,i:int64):qword;
var     gt1:qword;
        j:longint;
begin
        gt1:=1;
        for j:=1 to i do
            begin
                if (max1/gt1)>x then gt1:=gt1*x
                else exit(0);
            end;
        exit(gt1);
end;

procedure swap(var g,h:int64);
var temp:int64;
begin
        temp:=g;
        g:=h;
        h:=temp;
end;

procedure sort(l,r:longint);
var i,j:longint; mid:int64;
begin
        i:=l;
        j:=r;
        mid:=a[(l+r)div 2];
        repeat
                while a[i]<mid do inc(i);
                while a[j]>mid do dec(j);
                if not(i>j) then
                        begin
                                swap(a[i],a[j]);
                                inc(i);
                                dec(j);
                        end;
        until i>j;
        if i<r then sort(i,r);
        if l<j then sort(l,j);
end;

procedure xuli;
var     p,q,max,z:int64;
        cnt,i,j:longint;
begin
        cnt:=0;
        for i:=0 to 60 do
            for j:=0 to 60 do
                begin
                    p:=mu(x,i);
                    q:=mu(y,j);
                    if max1-p>=q then
                        if max1-q>=p then
                            if (p<>0) and (q<>0) then
                                begin
                                    if (p+q >=l) and (p+q <=r) then
                                        begin
                                                inc(cnt);
                                                a[cnt]:=p+q;
                                        end;
                                 end;
                end;
        sort(1,cnt);
        inc(cnt);
        a[cnt]:=r+1;
        max:=0 ;
        for i:=2 to cnt do
            begin
                z:=a[i]-a[i-1]-1;
                if max<z then max:=z;
            end;
        writeln(max);
end;

begin
        //assign(input,'unluckyyear.inp');reset(input);
        //assign(output,'unluckyyear.out');rewrite(output);
                nhap;
                xuli;
end.

const max1=trunc(1e18);
var    x,y,l,r:qword;
        a:array[0..36000]of int64;

procedure nhap;
begin
        readln(x,y,l,r);
end;

function mu(x,i:int64):qword;
var     gt1:qword;
        j:longint;
begin
        gt1:=1;
        for j:=1 to i do
            begin
                if (max1/gt1)>x then gt1:=gt1*x
                else exit(0);
            end;
        exit(gt1);
end;

procedure swap(var g,h:int64);
var temp:int64;
begin
        temp:=g;
        g:=h;
        h:=temp;
end;

procedure sort(l,r:longint);
var i,j:longint; mid:int64;
begin
        i:=l;
        j:=r;
        mid:=a[(l+r)div 2];
        repeat
                while a[i]<mid do inc(i);
                while a[j]>mid do dec(j);
                if not(i>j) then
                        begin
                                swap(a[i],a[j]);
                                inc(i);
                                dec(j);
                        end;
        until i>j;
        if i<r then sort(i,r);
        if l<j then sort(l,j);
end;

procedure xuli;
var     p,q,max,z:int64;
        cnt,i,j:longint;
begin
        cnt:=0;
        for i:=0 to 60 do
            for j:=0 to 60 do
                begin
                    p:=mu(x,i);
                    q:=mu(y,j);
                    if max1-p>=q then
                        if max1-q>=p then
                            if (p<>0) and (q<>0) then
                                begin
                                    if (p+q >=l) and (p+q <=r) then
                                        begin
                                                inc(cnt);
                                                a[cnt]:=p+q;
                                        end;
                                 end;
                end;
        sort(1,cnt);
        inc(cnt);
        a[cnt]:=r+1;
        max:=0 ;
        a[0]:=l-1;
        for i:=1 to cnt do
            begin
                if i=1 then z:=a[i]-a[i-1]
                else z:=a[i]-a[i-1]-1;
                if max<z then max:=z;
            end;
        writeln(max);
end;

begin
        //assign(input,'unluckyyear.inp');reset(input);
       // assign(output,'unluckyyear.out');rewrite(output);
                nhap;
                xuli;
end.

var x,y,l,r,max:int64;
    i,j,k,m,e:longint;
    px,py:array [0..100] of int64;
    w:array [1..5000000] of int64;
begin
	readln(x,y,l,r);
	px[0]:=1;
	py[0]:=1;
	i:=0;
	j:=0;
	repeat
		inc(i);
		px[i]:=px[i-1]*x;
	until (px[i]>r) or (px[i]>1000000000000000000 div x);
	repeat
		inc(j);
		py[j]:=py[j-1]*y;
	until (py[j]>r)or (py[i]>1000000000000000000 div y);
	e:=0;
	for k:=0 to i do
	for m:=0 to j do
	if (px[k]+py[m]>=l)and(px[k]+py[m]<=r) then
	begin
		inc(e);
		w[e]:=px[k]+py[m];
	end;
	for i:=1 to e-1 do
	for j:=1 to e-i do
	if w[j+1]<w[j] then
	begin
		k:=w[j+1];
		w[j+1]:=w[j];
		w[j]:=k;
	end;
	max:=w[1]-l;
	if r-w[e]>max then max:=r-w[e];
	for i:=1 to e-1 do
	if (w[i+1]-w[i]-1>max) then max:=w[i+1]-w[i]-1;
	writeln(max);
	readln;
end.

var x,y,l,r,max,f:int64;
    i,j,k,m,e:longint;
    px,py:array [0..1000] of int64;
    w:array [1..5000000] of int64;
begin
	readln(x,y,l,r);
	px[0]:=1;
	py[0]:=1;
	i:=0;
	j:=0;
	repeat
		inc(i);
		px[i]:=px[i-1]*x;
	until (px[i]>r div x) or (px[i]>1000000000000000000 div x);
	repeat
		inc(j);
		py[j]:=py[j-1]*y;
	until (py[j]>r div y)or (py[j]>1000000000000000000 div y);
	e:=0;
	for k:=0 to i do
	for m:=0 to j do
	if (px[k]+py[m]>=l)and(px[k]+py[m]<=r) then
	begin
		inc(e);
		w[e]:=px[k]+py[m];
	end
	else break;
	for i:=1 to e-1 do
	for j:=1 to e-i do
	if w[j+1]<w[j] then
	begin
		f:=w[j+1];
		w[j+1]:=w[j];
		w[j]:=f;
	end;
	max:=w[1]-l;
	if r-w[e]>max then max:=r-w[e];
	for i:=1 to e-1 do
	if (w[i+1]-w[i]-1>max) then max:=w[i+1]-w[i]-1;
	writeln(max);
	readln;
end.

var x,y,l,r,max:int64;
    i,j,k,m,e:longint;
    px,py:array [0..100] of int64;
    w:array [1..5000000] of int64;
begin
	readln(x,y,l,r);
	px[0]:=1;
	py[0]:=1;
	i:=0;
	j:=0;
	repeat
		inc(i);
		px[i]:=px[i-1]*x;
	until (px[i]>r) or (px[i]>1000000000000000000 div x);
	repeat
		inc(j);
		py[j]:=py[j-1]*y;
	until (py[j]>r)or (py[j]>1000000000000000000 div y);
	e:=0;
	for k:=0 to i do
	for m:=0 to j do
	if (px[k]+py[m]>=l)and(px[k]+py[m]<=r) then
	begin
		inc(e);
		w[e]:=px[k]+py[m];
	end;
	for i:=1 to e-1 do
	for j:=1 to e-i do
	if w[j+1]<w[j] then
	begin
		k:=w[j+1];
		w[j+1]:=w[j];
		w[j]:=k;
	end;
	max:=w[1]-l;
	if r-w[e]>max then max:=r-w[e];
	for i:=1 to e-1 do
	if (w[i+1]-w[i]-1>max) then max:=w[i+1]-w[i]-1;
	writeln(max);
	readln;
end.

var x,y,l,r,max,f:int64;
    i,j,k,m,e:longint;
    px,py:array [0..1000] of int64;
    w:array [1..5000000] of int64;
begin
	readln(x,y,l,r);
	px[0]:=1;
	py[0]:=1;
	i:=0;
	j:=0;
	repeat
		inc(i);
		px[i]:=px[i-1]*x;
	until (px[i]>r) or (px[i]>1000000000000000000 div x);
	repeat
		inc(j);
		py[j]:=py[j-1]*y;
	until (py[j]>r)or (py[j]>1000000000000000000 div y);
	e:=0;
	for k:=0 to i do
	for m:=0 to j do
	if (px[k]+py[m]>=l)and(px[k]+py[m]<=r) then
	begin
		inc(e);
		w[e]:=px[k]+py[m];
	end;
	for i:=1 to e-1 do
	for j:=1 to e-i do
	if w[j+1]<w[j] then
	begin
		f:=w[j+1];
		w[j+1]:=w[j];
		w[j]:=f;
	end;
	max:=w[1]-l;
	if r-w[e]>max then max:=r-w[e];
	for i:=1 to e-1 do
	if (w[i+1]-w[i]-1>max) then max:=w[i+1]-w[i]-1;
	writeln(max);
	readln;
end.