var c,a,b,x,y,ans,v,tmp:int64;
begin
  readln(c,x,y,a,b);
  v:=0;
  while (v<=1000000) and (v*a<=c) do
  begin
    tmp:=x*v+y*((c-v*a) div b);
    if tmp>ans then ans:=tmp;
    inc(v);
  end;
  v:=0;
  while (v<=1000000) and (v*b<=c) do
  begin
    tmp:=y*v+x*((c-v*b) div a);
    if tmp>ans then ans:=tmp;
    inc(v);
  end;
  writeln(ans);
end.

var i,c,hr,hb,wr,wb,v,cur,max:int64;
begin
  readln(c,hr,hb,wr,wb);
  while (v<=1000000) and (v*wr<=c) do
  begin
    cur:=hr*v+hb*((c-wr*v) div wb);
    if cur>max then max:=cur;
    inc(v);
  end;
  v:=0;
  while (v<=1000000) and (v*wb<=c) do
  begin
    cur:=hb*v+hr*((c-wb*v) div wr);
    if cur>max then max:=cur;
    inc(v);
  end;
  writeln(max);
end.

uses math;
var
 t,ans,i,j,f1,f2,l,r,c1,c2,n,a1,b1,a2,b2:int64;

function f(c:int64):int64;
begin
 f:=c*a1+(n-c*b1)div b2*a2;
end;

begin
 readln(n,a1,a2,b1,b2);
 if(a1>=a2)and(b1<=b2)then
 begin
  writeln(n div b1*a1);
  halt;
 end;
 if(a1<=a2)and(b1>=b2)then
 begin
  writeln(n div b2*a2);
  halt;
 end;
 if b1>=sqrt(n)then
 begin
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end else if b2>=sqrt(n)then
 begin
  t:=a1;a1:=a2;a2:=t;
  t:=b1;b1:=b2;b2:=t;
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end;
 if a1/b1<a2/b2 then
 begin
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end else
 begin
  t:=a1;a1:=a2;a2:=t;
  t:=b1;b1:=b2;b2:=t;
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end;
end.

program zad_C;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;
var
  c, wr, wb, hr, hb, res, num, num1 : int64;
  i : integer;
function get(x : integer):int64;
  var
    w : int64;
  begin
    if(trunc(1.0 * x * wr) > c) then begin
      get := 0;
      exit;
    end;

    w := (c - trunc(1.0 * x * wr)) div wb;
    get := trunc(1.0 * x * hr) +  trunc(1.0 * w * hb);
  end;

function nod(a, b : int64):int64;
  var
    r : int64;
  begin
    repeat
      r := a mod b;
      a := b;
      b := r;
    until(b = 0);

    nod := a;
  end;

function nok(a, b : int64):int64;
  begin
    nok := trunc(1.0 * (a div nod(a, b)) * b);
  end;

procedure swap(var a, b : int64);
  var
    c : int64;
  begin
    c := a;
    a := b;
    b := c;
  end;
begin
  readln(c, hr, hb, wr, wb);

  if(wb > wr) then begin
    swap(wr, wb);
    swap(hr, hb);
  end;

  num := nok(wr, wb);
  num1 := max(trunc(1.0 * hr * (num div wr)), trunc(1.0 * hb * (num div wb)));
  res := trunc(1.0 * max(0, (c div num) - 1) * num1);

  c := min(c, (c mod num) + num);

  num := 0;
  for i := 0 to c div wr do
    num := max(num, get(i));

  writeln(res + num);

  readln(c);
end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

          Ans : Int64;
      tempSize : Int64;
      tempCount : Int64;
      SwapTemp : Int64;

      i : Int64;
      TempBrute , MaxBrute : Int64;
begin
  Ans := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;

  if ( SizeRed < SizeBlue ) then
  begin
    SwapTemp := SizeRed;
    SizeRed := SizeBlue;
    SizeBlue := SwapTemp;

    SwapTemp := HappyRed;
    HappyRed := HappyBlue;
    HappyBlue := SwapTemp;
  end;

  tempSize := SizeRed * SizeBlue ;
  if ( MaxSize >= tempSize * 2 ) then
  begin

  tempCount := MaxSize div tempSize-1;
  Ans := Max ( SizeBlue * HappyRed , SizeRed * HappyBlue ) * tempCount ;
  MaxSize := MaxSize - tempCount * tempSize;

  if ( MaxSize = 0 ) then
  begin
    writeLn ( Ans ) ;
    halt;
  end;

  end;

  I := 0 ;
  MaxBrute := 0;

  while ( I*SizeRed <= MaxSize ) do
  begin
      TempBrute :=0;
    tempSize := MaxSize - i*SizeRed;
    tempCount := tempSize div SizeBlue;

    TempBrute := I*HappyRed + tempCount*HappyBlue;
    if ( TempBrute > MaxBrute ) then
      MaxBrute := TempBrute;
    inc ( I ) ;
  end;

  writeLn ( Ans + Max(TempBrute,MaxBrute) ) ;

end.

const eps=0.00000001;  besk=20000000000008;  nn=200000;
   label 1,2,3,4;
    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 h,kol,t,p,sum,k1,ii,jj,q,pos,z,pmax,imax,p1,p2,p3,p4,l,ot,nm,n,s1,s2,m,s,smin,cen,dl,t2,i1:int64;
    pp1,pp2,mask1,mask2,mask,newnapy,napx,napy,time,pp,step, k,t1,i,tt,j:longint;
     aa,bb,cc,dd:longint;
     x,y,fact,d,c,aold,b,a:array[-10..2000000] of int64;
     dda:array[-10..1200,-10..1200] of int64;
   zanx,zany:array[-10..200000] of boolean;
    sss, ss1,ss2:string;
    sot:array [-10..1000000] of char;
     fl:array[0..200] of boolean;
    ss:string;ch:char;
   ddp, pole :array [-100..2000,-100..2000] of int64;
    maxpr:array[0..nn] of string;
    st2:array[0..nn] of string;
     v0,v1,s0,t0:real;
     zv:point;
     newx,newy,xx,yy:array[-1..200000] of int64;

     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 ugol1(t:longint):extended;
begin  ugol1:=t/120;   end;

function ugol2(t:longint):extended;
begin  ugol2:=(t mod 3600)/10;   end;

begin
   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }

    read(cc,y[1],y[2],x[1],x[2]);
    if x[1]>x[2] then begin  t:=x[2];x[2]:=x[1]; x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;    end;
   if y[1]*x[2]=x[1]*y[2] then
      begin
          t:=nod(x[1],x[2]);
          if x[2]>100  then
             begin
              q:=cc div x[2]; ot:=0;
              for i:=0 to q do
                  begin
                    z:=i; p:=(cc-z*x[2]) div x[1];
                    ot:=max(ot,p*y[1]+z*y[2]);

                  end;
             end else ot:=round((cc div t)*t*y[2]/x[2]);

          writeln(ot);
          goto 1;
      end;
     if y[1]*x[2]<x[1]*y[2] then
     begin  t:=x[2];x[2]:=x[1];x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;   end;

    if y[1]*x[2]>x[1]*y[2] then
       begin

          ot:=0;
          t:=cc div x[1];
          q:=min(5000000,cc div x[2]);
          for i:=0 to q do
             begin
                z:=i;
                p:=(cc-z*x[2]) div x[1];
                ot:=max(ot,p*y[1]+z*y[2]);

             end;

       end;

     t:=x[2];x[2]:=x[1];x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;

   begin

          t:=cc div x[1];
          q:=min(5000000,cc div x[2]);
          for i:=0 to q do
             begin
                z:=i;
                p:=(cc-z*x[2]) div x[1];
                ot:=max(ot,p*y[1]+z*y[2]);

             end;

           writeln(ot);

       end;

   1:          close(output);
end.

program CC;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;

var hr,hb,wr,wb: int64;
i,c: longint;
ans,thisans,mult1,mult2: int64;

procedure swap(var a,b: int64);
var c: int64;
begin
  c:=a;
  a:=b;
  b:=c;
end;

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

  read(c,hr,hb,wr,wb);

  if wr > c then begin
    write((c div wb) * hb);
    halt;
  end;

  ans := 0;
  for i:=0 to trunc(sqrt(c)) do begin
    mult1 := min(c div wb, i);
    mult2 := (c - wb*i) div wr;
    thisans := hb*mult1 + hr*mult2;
    ans := max(ans,thisans);
  end;

  for i:=0 to trunc(sqrt(c)) do begin
    mult1 := min(c div wr, i);
    mult2 := (c - wr*i) div wb;
    thisans := hr*mult1 + hb*mult2;
    ans := max(ans,thisans);
  end;

  write(ans);
end.

program cft3;
var ans,wr,wb,hr,hb,c:int64;

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

function gcd(a,b:int64):int64;
begin
        if b=0 then exit(a) else exit(gcd(b,a mod b));
end;

function ex_Euclid(a,b:int64;var x,y:int64):int64;
var t:int64;
begin
	if b=0 then
	begin
		x:=1;y:=0;
		exit(a);
	end else
	begin
		ex_Euclid:=ex_Euclid(b,a mod b,x,y);
		t:=x;x:=y;y:=t-(a div b)*y;
	end;
end;

procedure solve1;
var i:longint;
begin
	ans:=0;
	for i:=0 to c div wr do
		ans:=max(ans,i*hr+(c-i*wr) div wb*hb);
end;

procedure solve2;
var x,y,xx,yy,tem,a,b,aa,bb,d:int64;
    i:longint;
begin
	ans:=0;
        aa:=wr;bb:=wb;
        for i:=0 to wr-1 do
	begin
                tem:=gcd(gcd(aa,bb),(c-i));
                a:=aa div tem;b:=bb div tem;
                d:=ex_Euclid(a,b,x,y);
                x:=x*(c-i) div tem;y:=y*(c-i) div tem;
                if x mod d<>0 then continue;
                if y mod d<>0 then continue;
                x:=x div d;y:=y div d;
	        if x<0 then
		begin
			tem:=(-x) div b;
			x:=x+tem*b;y:=y-tem*a;
			if x<0 then
			begin
				x:=x+b;y:=y-a;
			end;
		end;
		if y<0 then
		begin
			tem:=(-y) div a;
			x:=x-tem*b;y:=y+tem*a;
			if y<0 then
			begin
				x:=x-b;y:=y+a;
			end;
		end;
		if (x<0)or(y<0) then continue;
		if hr/wr>=hb/wb then
		begin
			if y>0 then
			begin
				tem:=y div a;
				x:=x+tem*b;y:=y-tem*a;
			end;
		end else
		begin
			if x>0 then
			begin
				tem:=x div b;
				x:=x-tem*b;y:=y+tem*a;
			end;
		end;
                if (x>=0)and(y>=0) then ans:=max(ans,x*hr+y*hb);
	end;
end;

begin
     //   assign(input,'cft3.in');reset(input);
	//assign(output,'dp.out');rewrite(output);
	readln(c,hr,hb,wr,wb);
	if wr>sqrt(c) then solve1 else solve2;
	writeln(ans);
end.

{$B-}
{$INLINE ON}
{$MODE OBJFPC}
uses    math;
const   finp = '';
        fout = '';
        maxn = 1000;
var     fi,fo                   : text;
        C,ha,hb,wa,wb,ans,t     : int64;
begin
        assign(fi,finp);  reset(fi);
        assign(fo,fout);  rewrite(fo);
        readln(fi,C,ha,hb,wa,wb);
        ans:=0;
        t:=0;
        while (t<=1000000) and (wa*t<=c) do begin
                ans:=max(ans,ha*t+((c-wa*t) div wb)*hb);
                inc(t);
        end;
        t:=0;
        while (t<=1000000) and (wb*t<=c) do begin
                ans:=max(ans,((c-wb*t) div wa)*ha+hb*t);
                inc(t);
        end;
        writeln(fo,ans);
        close(fi);        close(fo);
end.

var
  i,j:longint;
  s,c,hr,hb,wr,wb,x:int64;
function min(a,b:int64):int64;
 begin
   if a<b then exit(a)
          else exit(b);
 end;

begin
  readln(c,hr,hb,wr,wb);
  s:=0;
  if wr>c then
    begin
      writeln(c div wb*hb);
      halt;
    end;
  if wb>c then
    begin
      writeln(c div wr*hr);
      halt;
    end;
  if wr*hb>wb*hr then
    begin
      x:=hr; hr:=hb; hb:=x;
      x:=wr; wr:=wb; wb:=x;
    end;
  if c div wr<min(wr-1,c div wb) then
    begin
      for i:=c div wr downto 0 do
        begin
          j:=(c-i*wr) div wb;
          if i*hr+j*hb>s then s:=i*hr+j*hb;
        end;
      writeln(s);
      halt;
    end;
  for i:=min(wr-1,c div wb) downto 0 do
    begin
      j:=(c-i*wb) div wr;
      if i*hb+j*hr>s then s:=i*hb+j*hr;
    end;
  writeln(s);
end.

program problem3;

function gcd( a, b: int64 ): int64;
var	c: int64;
begin
	while ( b > 0 ) do
	begin
		c := b;
		b := a mod b;
		a := c;
	end;

	exit( a );
end;

var	c, hr, hb, wr, wb, lcm, ans, best, cur: int64;
	i: longInt;

begin
	readln( c, hr, hb, wr, wb );

	lcm := ( wr * wb ) div gcd( wr, wb );

	ans := 0;
	if ( c div lcm >= 2 ) then
	begin
		if ( hr * wb > hb * wr ) then
			ans := ( c div lcm - 1 ) * ( lcm div wr ) * hr
		else
			ans := ( c div lcm - 1 ) * ( lcm div wb ) * hb;

		c := lcm + ( c mod lcm );
	end;

	if ( wr < wb ) then
	begin
		wb := wb xor wr;
		wr := wb xor wr;
		wb := wb xor wr;

		hb := hb xor hr;
		hr := hb xor hr;
		hb := hb xor hr;
	end;

	best := 0;
	for i := 0 to longInt(c div wr) do
	begin
		cur := hr * i + hb * ( ( c - i * wr ) div wb );

		if ( cur > best ) then
			best := cur;
	end;

	writeln( ans + best );
end.

uses math;
var    c,hr,hb,wr,wb,ans,pomoc,p:int64;
 i:longint;
 begin
  read(c,hr,hb,wr,wb);
  if max(wr,wb)>=100 then
   begin
   if max(wr,wb)=wr then
    begin
     for i:=0 to c div wr do
      begin
      pomoc:=i;
      ans:=max(ans,pomoc*hr+((c-wr*pomoc) div wb)*hb);
     end;
    writeln(ans);
   end
  else
   begin
    for i:=0 to c div wb do
      begin
      pomoc:=i;
      ans:=max(ans,pomoc*hb+((c-wb*pomoc) div wr)*hr);
     end;
    writeln(ans);
   end;
  end
 else
  begin
   p:=wr*wb;
   if hr/wr>hb/wb then
    begin
   for i:=0 to p do
    begin
    pomoc:=i;
    if c-pomoc*wb>=0 then
    ans:=max(ans,pomoc*hb+((c-pomoc*wb) div wr)*hr);
    end;
   writeln(ans);
  end
 else
  begin
    for i:=0 to p do
    begin
    pomoc:=i;
    if c-pomoc*wr>=0 then
    ans:=max(ans,pomoc*hr+((c-pomoc*wr) div wb)*hb);
    end;
   writeln(ans) ;
  end;
 end;
 end.

uses math;
var
 t,ans,i,j,f1,f2,l,r,c1,c2,n,a1,b1,a2,b2:int64;

function f(c:int64):int64;
begin
 f:=c*a1+(n-c*b1)div b2*a2;
end;

begin
 readln(n,a1,a2,b1,b2);
 if(a1<=a2)and(b1>=b2)then
 begin
  writeln(n div b1*a1);
  halt;
 end;
 if(a1>=a2)and(b1<=b2)then
 begin
  writeln(n div b2*a2);
  halt;
 end;
 if(b1>=sqrt(n))or(a1/b1<a2/b2)then
 begin
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end else
 begin
  t:=a1;a1:=a2;a2:=t;
  t:=b1;b1:=b2;b2:=t;
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end;
end.

uses math;
var
 t,ans,i,j,f1,f2,l,r,c1,c2,n,a1,b1,a2,b2:int64;

function f(c:int64):int64;
begin
 f:=c*a1+(n-c*b1)div b2*a2;
end;

begin
 readln(n,a1,a2,b1,b2);
 if(a1>=a2)and(b1<=b2)then
 begin
  writeln(n div b1*a1);
  halt;
 end;
 if(a1<=a2)and(b1>=b2)then
 begin
  writeln(n div b2*a2);
  halt;
 end;
 if(b1>=sqrt(n))or(a1/b1<a2/b2)then
 begin
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end else
 begin
  t:=a1;a1:=a2;a2:=t;
  t:=b1;b1:=b2;b2:=t;
  i:=0;
  while i<=sqrt(n)do
  begin
   if i*b1>n then break;
   ans:=max(ans,f(i));
   inc(i);
  end;
  writeln(ans);halt;
 end;
end.

uses math;
var
 ans,i,j,f1,f2,l,r,c1,c2,n,a1,b1,a2,b2:int64;

function f(c:int64):int64;
begin
 f:=c*a1+(n-c*b1)div b2*a2;
end;

begin
 readln(n,a1,a2,b1,b2);
 if(a1<=a2)and(b1>=b2)then
 begin
  writeln(n div b1*a1);
  halt;
 end;
 if(a1>=a2)and(b1<=b2)then
 begin
  writeln(n div b2*a2);
  halt;
 end;
 l:=0;r:=n div b1;
 while r-l>1 do
 begin
  c1:=(l*2+r)div 3;
  c2:=(l+r*2)div 3;
  f1:=f(c1);f2:=f(c2);
  if f(c1)<f(c2)then l:=c1+1 else r:=c2;
 end;
 i:=max(0,l-1000000);
 j:=min(n div b1,r+1000000);
 while i<=j do
 begin
  ans:=max(ans,f(i));
  inc(i);
 end;
 writeln(ans);
end.

uses math;
var
 l,r,c1,c2,n,a1,b1,a2,b2:int64;

function f(c:int64):int64;
begin
 f:=c*a1+(n-c*b1)div b2*a2;
end;

begin
 readln(n,a1,a2,b1,b2);
 if(a1<=a2)and(b1>=b2)then
 begin
  writeln(n div b1*a1);
  halt;
 end;
 if(a1>=a2)and(b1<=b2)then
 begin
  writeln(n div b2*a2);
  halt;
 end;
 l:=0;r:=n div b1;
 while r-l>1 do
 begin
  c1:=(l*2+r)div 3;
  c2:=(l+r*2)div 3;
  if f(c1)<f(c2)then l:=c1+1 else r:=c2;
 end;
 writeln(max(f(l),f(r)));
end.

   const eps=0.00000001;  besk=20000000000008;  nn=200000;
   label 1,2,3,4;
    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 h,kol,t,p,sum,k1,ii,jj,q,pos,z,pmax,imax,p1,p2,p3,p4,l,ot,nm,n,s1,s2,m,s,smin,cen,dl,t2,i1:int64;
    pp1,pp2,mask1,mask2,mask,newnapy,napx,napy,time,pp,step, k,t1,i,tt,j:longint;
     aa,bb,cc,dd:longint;
     x,y,fact,d,c,aold,b,a:array[-10..2000000] of int64;
     dda:array[-10..1200,-10..1200] of int64;
   zanx,zany:array[-10..200000] of boolean;
    sss, ss1,ss2:string;
    sot:array [-10..1000000] of char;
     fl:array[0..200] of boolean;
    ss:string;ch:char;
   ddp, pole :array [-100..2000,-100..2000] of int64;
    maxpr:array[0..nn] of string;
    st2:array[0..nn] of string;
     v0,v1,s0,t0:real;
     zv:point;
     newx,newy,xx,yy:array[-1..200000] of int64;

     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 ugol1(t:longint):extended;
begin  ugol1:=t/120;   end;

function ugol2(t:longint):extended;
begin  ugol2:=(t mod 3600)/10;   end;

begin
  {  assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
   }

    read(cc,y[1],y[2],x[1],x[2]);
    if x[1]>x[2] then begin  t:=x[2];x[2]:=x[1]; x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;    end;
   if y[1]*x[2]=x[1]*y[2] then
      begin
          t:=nod(x[1],x[2]);
          if x[2]>100  then
             begin
              q:=cc div x[2]; ot:=0;
              for i:=0 to q do
                  begin
                    z:=i; p:=(cc-z*x[2]) div x[1];
                    ot:=max(ot,p*y[1]+z*y[2]);

                  end;
             end else ot:=round((cc div t)*t*y[2]/x[2]);

          writeln(ot);
          goto 1;
      end;
     if y[1]*x[2]<x[1]*y[2] then
     begin  t:=x[2];x[2]:=x[1];x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;   end;

    if y[1]*x[2]>x[1]*y[2] then
       begin
          ot:=0;
          t:=cc div x[1];
          q:=min(10000000,cc div x[2]);
          for i:=0 to q do
             begin
                z:=i;
                p:=(cc-z*x[2]) div x[1];
                ot:=max(ot,p*y[1]+z*y[2]);

             end;
           writeln(ot);

       end;

   1:          close(output);
end.

   const eps=0.00000001;  besk=20000000000008;  nn=200000;
   label 1,2,3,4;
    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 h,kol,t,p,sum,k1,ii,jj,q,pos,z,pmax,imax,p1,p2,p3,p4,l,ot,nm,n,s1,s2,m,s,smin,cen,dl,t2,i1:int64;
    pp1,pp2,mask1,mask2,mask,newnapy,napx,napy,time,pp,step, k,t1,i,tt,j:longint;
     aa,bb,cc,dd:longint;
     x,y,fact,d,c,aold,b,a:array[-10..2000000] of int64;
     dda:array[-10..1200,-10..1200] of int64;
   zanx,zany:array[-10..200000] of boolean;
    sss, ss1,ss2:string;
    sot:array [-10..1000000] of char;
     fl:array[0..200] of boolean;
    ss:string;ch:char;
   ddp, pole :array [-100..2000,-100..2000] of int64;
    maxpr:array[0..nn] of string;
    st2:array[0..nn] of string;
     v0,v1,s0,t0:real;
     zv:point;
     newx,newy,xx,yy:array[-1..200000] of int64;

     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 ugol1(t:longint):extended;
begin  ugol1:=t/120;   end;

function ugol2(t:longint):extended;
begin  ugol2:=(t mod 3600)/10;   end;

begin
  {  assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
   }

    read(cc,y[1],y[2],x[1],x[2]);
    if x[1]>x[2] then begin  t:=x[2];x[2]:=x[1]; x[1]:=t;   end;
   if y[1]*x[2]=x[1]*y[2] then
      begin
          t:=nod(x[1],x[2]);
          if x[2]>100  then
             begin
              q:=cc div x[2]; ot:=0;
              for i:=0 to q do
                  begin
                    z:=i; p:=(cc-z*x[2]) div x[1];
                    ot:=max(ot,p*y[1]+z*y[2]);

                  end;
             end else ot:=round((cc div t)*t*y[2]/x[2]);

          writeln(ot);
          goto 1;
      end;
     if y[1]*x[2]<x[1]*y[2] then
     begin  t:=x[2];x[2]:=x[1];x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;   end;

    if y[1]*x[2]>x[1]*y[2] then
       begin
          ot:=0;
          t:=cc div x[1];
          q:=min(30000000,cc div x[2]);
          for i:=0 to q do
             begin
                z:=i;
                p:=(cc-z*x[2]) div x[1];
                ot:=max(ot,p*y[1]+z*y[2]);

             end;
           writeln(ot);

       end;

   1:          close(output);
end.

const eps=0.00000001;  besk=20000000000008;  nn=200000;
   label 1,2,3,4;
    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 h,kol,t,p,sum,k1,ii,jj,q,pos,z,pmax,imax,p1,p2,p3,p4,l,ot,nm,n,s1,s2,m,s,smin,cen,dl,t2,i1:int64;
    pp1,pp2,mask1,mask2,mask,newnapy,napx,napy,time,pp,step, k,t1,i,tt,j:longint;
     aa,bb,cc,dd:longint;
     x,y,fact,d,c,aold,b,a:array[-10..2000000] of int64;
     dda:array[-10..1200,-10..1200] of int64;
   zanx,zany:array[-10..200000] of boolean;
    sss, ss1,ss2:string;
    sot:array [-10..1000000] of char;
     fl:array[0..200] of boolean;
    ss:string;ch:char;
   ddp, pole :array [-100..2000,-100..2000] of int64;
    maxpr:array[0..nn] of string;
    st2:array[0..nn] of string;
     v0,v1,s0,t0:real;
     zv:point;
     newx,newy,xx,yy:array[-1..200000] of int64;

     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 ugol1(t:longint):extended;
begin  ugol1:=t/120;   end;

function ugol2(t:longint):extended;
begin  ugol2:=(t mod 3600)/10;   end;

begin
   { assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
    }

    read(cc,y[1],y[2],x[1],x[2]);
    if x[1]>x[2] then begin  t:=x[2];x[2]:=x[1]; x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;    end;
   if y[1]*x[2]=x[1]*y[2] then
      begin
          t:=nod(x[1],x[2]);
          if x[2]>100  then
             begin
              q:=cc div x[2]; ot:=0;
              for i:=0 to q do
                  begin
                    z:=i; p:=(cc-z*x[2]) div x[1];
                    ot:=max(ot,p*y[1]+z*y[2]);

                  end;
             end else ot:=round((cc div t)*t*y[2]/x[2]);

          writeln(ot);
          goto 1;
      end;
     if y[1]*x[2]<x[1]*y[2] then
     begin  t:=x[2];x[2]:=x[1];x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;   end;

    if y[1]*x[2]>x[1]*y[2] then
       begin

          ot:=0;
          t:=cc div x[1];
          q:=min(5000000,cc div x[2]);
          for i:=0 to q do
             begin
                z:=i;
                p:=(cc-z*x[2]) div x[1];
                ot:=max(ot,p*y[1]+z*y[2]);

             end;

       end;

     t:=x[2];x[2]:=x[1];x[1]:=t; t:=y[2];y[2]:=y[1];y[1]:=t;

   begin

          ot:=0;
          t:=cc div x[1];
          q:=min(5000000,cc div x[2]);
          for i:=0 to q do
             begin
                z:=i;
                p:=(cc-z*x[2]) div x[1];
                ot:=max(ot,p*y[1]+z*y[2]);

             end;

           writeln(ot);

       end;

   1:          close(output);
end.

{$B-}
{$INLINE ON}
{$MODE OBJFPC}
uses    math;
const   finp = '';
        fout = '';
        maxn = 1000;
var     fi,fo                   : text;
        C,ha,hb,wa,wb,ans,w     : int64;
        i                       : longint;
begin
        assign(fi,finp);  reset(fi);
        assign(fo,fout);  rewrite(fo);
        readln(fi,C,ha,hb,wa,wb);
        if C<min(wa,wb) then writeln(fo,0)
        else begin
                if wa<wb then begin
                        w:=wa; wa:=wb; wb:=w;
                        w:=ha; ha:=hb; hb:=w;
                end;
                if wb<=C then ans:=(C div wb)*hb else ans:=0;
                for i:=0 to C div wa do begin
                        w:=(int64(i)*(ha*wb-wa*hb)+C*hb) div wb;
                        if w>ans then ans:=w;
                end;
                writeln(fo,ans);
        end;
        close(fi);        close(fo);
end.

var c,a,b,x,y,k,ans,t,v,tmp,ans1:int64;

function max(p,q:int64):int64;
begin
  if p<q then exit(q) else exit(p);
end;

function gcd(u,v:int64):int64;
begin
  if v=0 then exit(u) else exit(gcd(v,u mod v));
end;

begin
  //assign(input,'a.in');reset(Input);assign(output,'a.out');rewrite(output);
  readln(c,x,y,a,b);
  k:=gcd(a,b);
  k:=(a*b) div k;
  t:=(c+k) mod k;
  k:=c-t;
  ans:=max((k div a)*x,(k div b)*y);
  if a>b then
  begin
    v:=0;
    while v*a<=t do
    begin
      tmp:=x*v+y*((t-v*a) div b);
      if tmp>ans1 then ans1:=tmp;
      inc(v);
    end;
  end else
  begin
    v:=0;
    while v*b<=t do
    begin
      tmp:=y*v+x*((t-v*b) div a);
      if tmp>ans1 then ans1:=tmp;
      inc(v);
    end;
  end;
  writeln(ans+ans1);
end.

var
  s,c,hr,hb,wr,wb,x,i,j:longint;
begin
  readln(c,hr,hb,wr,wb);
  s:=0;
  if wr<wb then
    begin
      x:=hr; hr:=hb; hb:=x;
      x:=wr; wr:=wb; wb:=x;
    end;
  for i:=c div wr downto 0 do
    begin
      j:=(c-i*wr) div wb;
      if i*hr+j*hb>s then s:=i*hr+j*hb;
    end;
  writeln(s);
end.

var
  i,j:longint;
  s,c,hr,hb,wr,wb,x:int64;
begin
  readln(c,hr,hb,wr,wb);
  s:=0;
  if wr*hb>wb*hr then
    begin
      x:=hr; hr:=hb; hb:=x;
      x:=wr; wr:=wb; wb:=x;
    end;
  for i:=wr-1 downto 0 do
    begin
      j:=(c-i*wb) div wr;
      if i*hb+j*hr>s then s:=i*hb+j*hr;
    end;
  writeln(s);
end.

Program Main;
  Uses MATH;
  var MaxVes , Gives1 , Gives2 , Ves1 , Ves2 : Int64;
      temp : LongInt;

	  Ans : LongInt;

	  I : LongInt;
	  MaxHappynes , tempHappynes : LongInt;
begin
  Ans := 0;
  MaxHappynes := 0;
  readLn ( MaxVes , Gives1 , Gives2 , Ves1 , Ves2 ) ;
  temp := Ves1 * Ves2 ;

  if ( MaxVes >= temp ) then
  begin
    Ans := Max ( Ves2*Gives1 , Ves1*Gives2 ) * (MaxVes div temp) ;
	MaxVes := MaxVes mod temp;
  end;

  if ( MaxVes <> 0  ) then
  begin
    i := 0 ;
	while ( TRUE ) do
	begin
	  if ( i*Ves1 > MaxVes ) then
	    break;
	  temp := MaxVes - i*Ves1;
	  tempHappynes := i*Gives1;
	  tempHappynes := tempHappynes + (temp div Ves2) * Gives2;
	  if ( tempHappynes > MaxHappynes ) then
	    MaxHappynes := tempHappynes ;

          inc ( I ) ;
	end;
  end;

  writeLn ( Ans + MaxHappynes ) ;
end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

          Ans : Int64;
	  tempSize : Int64;
	  tempCount : Int64;

      i : Int64;
	  TempBrute , MaxBrute : Int64;
begin
  Ans := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;
  if ( MaxSize >= 100000 ) then
  begin
  tempSize := SizeRed * SizeBlue ;

  tempCount := MaxSize div tempSize;
  Ans := Max ( SizeBlue * HappyRed , SizeRed * HappyBlue ) * tempCount ;
  MaxSize := MaxSize - tempCount * tempSize;

  if ( MaxSize = 0 ) then
  begin
    writeLn ( Ans ) ;
	halt;
  end;

  end;

  I := 0 ;
  MaxBrute := 0;

  while ( I*SizeRed <= MaxSize ) do
  begin
      TempBrute :=0;
    tempSize := MaxSize - i*SizeRed;
	tempCount := tempSize div SizeBlue;

	TempBrute := I*HappyRed + tempCount*HappyBlue;
	if ( TempBrute > MaxBrute ) then
	  MaxBrute := TempBrute;
    inc ( I ) ;
  end;

  writeLn ( Ans + Max(TempBrute,MaxBrute) ) ;

end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

          Ans : Int64;
	  tempSize : Int64;
	  tempCount : Int64;

      i : Int64;
	  TempBrute , MaxBrute : Int64;
begin
  Ans := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;
  tempSize := SizeRed * SizeBlue ;
  if ( MaxSize >= tempSize ) then
  begin

    tempCount := MaxSize div tempSize;
    MaxSize := MaxSize mod tempSize;

    Ans := Max ( Int64((tempSize div SizeRed) * HappyRed) , Int64((tempSize div SizeBlue) * HappyBlue )) * tempCount ;
    MaxSize := MaxSize - (tempCount * tempSize);

    if ( MaxSize = 0 ) then
    begin
      writeLn ( Ans ) ;
	  halt;
    end;

  end;

  I := 0 ;
  MaxBrute := 0;

  while ( (I*SizeRed) <= MaxSize ) do
  begin
      TempBrute :=0;
    tempSize := MaxSize - i*SizeRed;
	tempCount := tempSize div SizeBlue;

	TempBrute := I*HappyRed + tempCount*HappyBlue;
	if ( TempBrute > MaxBrute ) then
	  MaxBrute := TempBrute;
    inc ( I ) ;
  end;

  writeLn ( Ans + Max(0,MaxBrute) ) ;

end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

          Ans : Int64;
	  tempSize : Int64;
	  tempCount : Int64;

      i : Int64;
	  TempBrute , MaxBrute : Int64;
begin
  Ans := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;

  tempSize := SizeRed * SizeBlue ;

  tempCount := MaxSize div tempSize;
  Ans := Max ( SizeBlue * HappyRed , SizeRed * HappyBlue ) * tempCount ;
  MaxSize := MaxSize - tempCount * tempSize;

  if ( MaxSize = 0 ) then
  begin
    writeLn ( Ans ) ;
	halt;
  end;

  I := 0 ;
  MaxBrute := 0;

  while ( I*SizeRed <= MaxSize ) do
  begin
      TempBrute :=0;
    tempSize := MaxSize - i*SizeRed;
	tempCount := tempSize div SizeBlue;

	TempBrute := I*HappyRed + tempCount*HappyBlue;
	if ( TempBrute > MaxBrute ) then
	  MaxBrute := TempBrute;
    inc ( I ) ;
  end;

  writeLn ( Ans + Max(TempBrute,MaxBrute) ) ;

end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

      temp , Count : Int64;
      Ans , cBruted , maxBruted : Int64;
      i : Int64;
begin
  Ans := 0;
  maxBruted := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;

  temp := SizeRed * SizeBlue ;

  if ( MaxSize >= temp ) then
  begin
    Count := MaxSize div temp;
	Ans := Count * Max ( HappyRed * SizeBlue , HappyBlue * SizeRed ) ;
	MaxSize := MaxSize mod temp;
  end;

  if ( MaxSize > 0 ) then
  begin
    I := 0 ;
    cBruted := 0;
	while ( true ) do  // BRUTE REEEEDD!!!!!!!!
	begin
	  if ( i*SizeRed > MaxSize ) then
	    break; // The End

      temp := MaxSize - i*SizeRed;
	  cBruted := i*HappyRed;

	  cBruted := cBruted + ( temp div SizeBlue ) * HappyBlue ;

	  if ( cBruted > maxBruted ) then
	    maxBruted := cBruted ;

	  inc ( i ) ;
	end;
  end;

  writeLn ( Ans + maxBruted ) ;

end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

          Ans : Int64;
	  tempSize : Int64;
	  tempCount : Int64;

      i : Int64;
	  TempBrute , MaxBrute : Int64;
begin
  Ans := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;

  tempSize := SizeRed * SizeBlue ;

  tempCount := MaxSize div tempSize;
  Ans := Max ( HappyBlue * HappyRed , HappyRed * HappyBlue ) * tempCount ;
  MaxSize := MaxSize - tempCount * tempSize;

  if ( MaxSize = 0 ) then
  begin
    writeLn ( Ans ) ;
	halt;
  end;

  I := 0 ;
  MaxBrute := 0;

  while ( I*SizeRed <= MaxSize ) do
  begin
      TempBrute :=0;
    tempSize := MaxSize - i*SizeRed;
	tempCount := tempSize div SizeBlue;

	TempBrute := I*HappyRed + tempCount*HappyBlue;
	if ( TempBrute > MaxBrute ) then
	  MaxBrute := TempBrute;
    inc ( I ) ;
  end;

  writeLn ( Ans + Max(TempBrute,MaxBrute) ) ;

end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

          Ans : Int64;
	  tempSize : Int64;
	  tempCount : Int64;

      i : Int64;
	  TempBrute , MaxBrute : Int64;
begin
  Ans := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;
  if ( MaxSize >= 1000 ) then
  begin
  tempSize := SizeRed * SizeBlue ;

  tempCount := MaxSize div tempSize;
  Ans := Max ( SizeBlue * HappyRed , SizeRed * HappyBlue ) * tempCount ;
  MaxSize := MaxSize - tempCount * tempSize;

  if ( MaxSize = 0 ) then
  begin
    writeLn ( Ans ) ;
	halt;
  end;

  end;

  I := 0 ;
  MaxBrute := 0;

  while ( I*SizeRed <= MaxSize ) do
  begin
      TempBrute :=0;
    tempSize := MaxSize - i*SizeRed;
	tempCount := tempSize div SizeBlue;

	TempBrute := I*HappyRed + tempCount*HappyBlue;
	if ( TempBrute > MaxBrute ) then
	  MaxBrute := TempBrute;
    inc ( I ) ;
  end;

  writeLn ( Ans + Max(TempBrute,MaxBrute) ) ;

end.

Program Main;
  Uses MATH;
  var MaxVes , Gives1 , Gives2 , Ves1 , Ves2 : Int64;
      temp : Int64;

	  Ans : int64;

	  I : Int64;
	  MaxHappynes , tempHappynes : Int64;
begin
  Ans := 0;
  MaxHappynes := 0;
  readLn ( MaxVes , Gives1 , Gives2 , Ves1 , Ves2 ) ;
  temp := Ves1 * Ves2 ;

  if ( MaxVes >= temp ) then
  begin
    Ans := Max ( Ves2*Gives1 , Ves1*Gives2 ) * (MaxVes div temp) ;
	MaxVes := MaxVes mod temp;
  end;

  if ( MaxVes <> 0  ) then
  begin
    i := 0 ;
	while ( TRUE ) do
	begin
	  if ( i*Ves1 > MaxVes ) then
	    break;
	  temp := MaxVes - i*Ves1;
	  tempHappynes := i*Gives1;
	  tempHappynes := tempHappynes + (temp div Ves2) * Gives2;
	  if ( tempHappynes > MaxHappynes ) then
	    MaxHappynes := tempHappynes ;

          inc ( I ) ;
	end;
  end;

  writeLn ( Ans + MaxHappynes ) ;
end.

Program Main;
  Uses MATH;
  var MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue : Int64;

      temp , Count : Int64;
      Ans , cBruted , maxBruted : Int64;
	  tempSize : Int64;
	  tempCount : Int64;
      i : Int64;
begin
  Ans := 0;
  maxBruted := 0;

  readLn ( MaxSize , HappyRed , HappyBlue , SizeRed , SizeBlue ) ;

  temp := SizeRed * SizeBlue ;

  if ( MaxSize >= temp ) then
  begin
    Count := MaxSize div temp;
	Ans := Count * Max ( HappyRed * SizeBlue , HappyBlue * SizeRed ) ;
	MaxSize := MaxSize - temp*Count;
  end;

  if ( MaxSize > 0 ) then
  begin
    I := 0 ;

	while ( true ) do  // BRUTE REEEEDD!!!!!!!!
	begin
	  cBruted := 0;
	  if ( i*SizeRed > MaxSize ) then
	    break; // The End

      tempSize := MaxSize - (i*SizeRed);
	  cBruted := (i*HappyRed);
      tempCount := TempSize div SizeBlue;
	  cBruted := cBruted + (tempCount * HappyBlue);

	  if ( cBruted > maxBruted ) then
	    maxBruted := cBruted ;

	  inc ( i ) ;
	end;
  end;

  writeLn ( Ans + maxBruted ) ;

end.

program cft3;
var ans,wr,wb,hr,hb,c:int64;
function max(a,b:int64):int64;
begin
	if a>b then exit(a) else exit(b);
end;

function ex_Euclid(a,b:int64;var x,y:int64):int64;
var t:int64;
begin
	if b=0 then
	begin
		x:=1;y:=0;
		exit(a);
	end else
	begin
		ex_Euclid:=ex_Euclid(b,a mod b,x,y);
		t:=x;x:=y;y:=t-(a div b)*y;
	end;
end;

procedure solve1;
var i:longint;
begin
	ans:=0;
	for i:=0 to c div wr do
		ans:=max(ans,i*hr+(c-i) div wb*hb);
end;

procedure solve2;
var x,y,xx,yy,tem,a,b:int64;
    i:longint;
begin
	ans:=0;
	ex_Euclid(wr,wb,xx,yy);
        a:=wr;b:=wb;
        for i:=0 to wr-1 do
	begin
		x:=xx*(c-i);y:=yy*(c-i);
		if x<0 then
		begin
			tem:=(-x) div b;
			x:=x+tem*b;y:=y-tem*a;
			if x<0 then
			begin
				x:=x+b;y:=y-a;
			end;
		end;
		if y<0 then
		begin
			tem:=(-y) div a;
			x:=x-tem*b;y:=y+tem*a;
			if y<0 then
			begin
				x:=x-b;y:=y+a;
			end;
		end;
		if hr/wr>=hb/wb then
		begin
			if y>0 then
			begin
				tem:=y div a;
				x:=x+tem*b;y:=y-tem*a;
			end;
		end else
		begin
			if x>0 then
			begin
				tem:=x div b;
				x:=x-tem*b;y:=y+tem*a;
			end;
		end;
		ans:=max(ans,x*hr+y*hb);
	end;
end;

procedure swap(var x,y:int64);
var tem:int64;
begin
	tem:=x;x:=y;y:=tem;
end;

begin
       // assign(input,'cft3.in');reset(input);
	readln(c,hr,hb,wr,wb);
	if wr<wb then
	begin
		swap(hr,hb);
		swap(wr,wb);
	end;
	if wr>sqrt(c) then solve1 else solve2;
	writeln(ans);
end.

program cft3;
var ans,wr,wb,hr,hb,c:int64;
function max(a,b:int64):int64;
begin
	if a>b then exit(a) else exit(b);
end;

function gcd(a,b:int64):int64;
begin
        if b=0 then exit(a) else exit(gcd(b,a mod b));
end;

function ex_Euclid(a,b:int64;var x,y:int64):int64;
var t:int64;
begin
	if b=0 then
	begin
		x:=1;y:=0;
		exit(a);
	end else
	begin
		ex_Euclid:=ex_Euclid(b,a mod b,x,y);
		t:=x;x:=y;y:=t-(a div b)*y;
	end;
end;

procedure solve1;
var i:longint;
begin
	ans:=0;
	for i:=0 to c div wr do
		ans:=max(ans,i*hr+(c-i*wr) div wb*hb);
end;

procedure solve2;
var x,y,xx,yy,tem,a,b,aa,bb:int64;
    i:longint;
begin
	ans:=0;
        aa:=wr;bb:=wb;
        for i:=0 to wr-1 do
	begin

                tem:=gcd(gcd(aa,bb),(c-i));
                a:=aa div tem;b:=bb div tem;
                ex_Euclid(a,b,x,y);
                x:=x*(c-i) div tem;y:=y*(c-i) div tem;
		if x<0 then
		begin
			tem:=(-x) div b;
			x:=x+tem*b;y:=y-tem*a;
			if x<0 then
			begin
				x:=x+b;y:=y-a;
			end;
		end;
		if y<0 then
		begin
			tem:=(-y) div a;
			x:=x-tem*b;y:=y+tem*a;
			if y<0 then
			begin
				x:=x-b;y:=y+a;
			end;
		end;
		if hr/wr>=hb/wb then
		begin
			if y>0 then
			begin
				tem:=y div a;
				x:=x+tem*b;y:=y-tem*a;
			end;
		end else
		begin
			if x>0 then
			begin
				tem:=x div b;
				x:=x-tem*b;y:=y+tem*a;
			end;
		end;
		if (x>=0)and(y>=0) then ans:=max(ans,x*hr+y*hb);
	end;
end;

begin
       // assign(input,'cft3.in');reset(input);
	readln(c,hr,hb,wr,wb);
	if wr>sqrt(c) then solve1 else solve2;
	writeln(ans);
end.

program cft3;
var ans,wr,wb,hr,hb,c:int64;
function max(a,b:int64):int64;
begin
	if a>b then exit(a) else exit(b);
end;

function gcd(a,b:int64):int64;
begin
        if b=0 then exit(a) else exit(gcd(b,a mod b));
end;

function ex_Euclid(a,b:int64;var x,y:int64):int64;
var t:int64;
begin
	if b=0 then
	begin
		x:=1;y:=0;
		exit(a);
	end else
	begin
		ex_Euclid:=ex_Euclid(b,a mod b,x,y);
		t:=x;x:=y;y:=t-(a div b)*y;
	end;
end;

procedure solve1;
var i:longint;
begin
	ans:=0;
	for i:=0 to c div wr do
		ans:=max(ans,i*hr+(c-i*wr) div wb*hb);
end;

procedure solve2;
var x,y,xx,yy,tem,a,b,aa,bb:int64;
    i:longint;
begin
	ans:=0;
        aa:=wr;bb:=wb;
        for i:=0 to wr-1 do
	begin

                tem:=gcd(gcd(aa,bb),(c-i));
                a:=aa div tem;b:=bb div tem;
                ex_Euclid(a,b,x,y);
		if x<0 then
		begin
			tem:=(-x) div b;
			x:=x+tem*b;y:=y-tem*a;
			if x<0 then
			begin
				x:=x+b;y:=y-a;
			end;
		end;
		if y<0 then
		begin
			tem:=(-y) div a;
			x:=x-tem*b;y:=y+tem*a;
			if y<0 then
			begin
				x:=x-b;y:=y+a;
			end;
		end;
		if (x<0)or(y<0) then continue;
                x:=x*(c-i) div tem;y:=y*(c-i) div tem;
		if hr/wr>=hb/wb then
		begin
			if y>0 then
			begin
				tem:=y div a;
				x:=x+tem*b;y:=y-tem*a;
			end;
		end else
		begin
			if x>0 then
			begin
				tem:=x div b;
				x:=x-tem*b;y:=y+tem*a;
			end;
		end;
		if (x>=0)and(y>=0) then ans:=max(ans,x*hr+y*hb);
	end;
end;

begin
       // assign(input,'cft3.in');reset(input);
	readln(c,hr,hb,wr,wb);
	if wr>sqrt(c) then solve1 else solve2;
	writeln(ans);
end.

program cft3;
var ans,wr,wb,hr,hb,c:int64;
function max(a,b:int64):int64;
begin
	if a>b then exit(a) else exit(b);
end;

function gcd(a,b:int64):int64;
begin
        if b=0 then exit(a) else exit(gcd(b,a mod b));
end;

function ex_Euclid(a,b:int64;var x,y:int64):int64;
var t:int64;
begin
	if b=0 then
	begin
		x:=1;y:=0;
		exit(a);
	end else
	begin
		ex_Euclid:=ex_Euclid(b,a mod b,x,y);
		t:=x;x:=y;y:=t-(a div b)*y;
	end;
end;

procedure solve1;
var i:longint;
begin
	ans:=0;
	for i:=0 to c div wr do
		ans:=max(ans,i*hr+(c-i) div wb*hb);
end;

procedure solve2;
var x,y,xx,yy,tem,a,b,aa,bb:int64;
    i:longint;
begin
	ans:=0;
        aa:=wr;bb:=wb;
        for i:=0 to wr-1 do
	begin

                tem:=gcd(gcd(aa,bb),(c-i));
                a:=aa div tem;b:=bb div tem;
                ex_Euclid(a,b,x,y);
                x:=x*(c-i) div tem;y:=y*(c-i) div tem;
		if x<0 then
		begin
			tem:=(-x) div b;
			x:=x+tem*b;y:=y-tem*a;
			if x<0 then
			begin
				x:=x+b;y:=y-a;
			end;
		end;
		if y<0 then
		begin
			tem:=(-y) div a;
			x:=x-tem*b;y:=y+tem*a;
			if y<0 then
			begin
				x:=x-b;y:=y+a;
			end;
		end;
		if hr/wr>=hb/wb then
		begin
			if y>0 then
			begin
				tem:=y div a;
				x:=x+tem*b;y:=y-tem*a;
			end;
		end else
		begin
			if x>0 then
			begin
				tem:=x div b;
				x:=x-tem*b;y:=y+tem*a;
			end;
		end;
		if (x>=0)and(y>=0) then ans:=max(ans,x*hr+y*hb);
	end;
end;

begin
       // assign(input,'cft3.in');reset(input);
	readln(c,hr,hb,wr,wb);
	if wr>sqrt(c) then solve1 else solve2;
	writeln(ans);
end.

program cft3;
var ans,wr,wb,hr,hb,c:int64;
function max(a,b:int64):int64;
begin
	if a>b then exit(a) else exit(b);
end;

function gcd(a,b:int64):int64;
begin
        if b=0 then exit(a) else exit(gcd(b,a mod b));
end;

function ex_Euclid(a,b:int64;var x,y:int64):int64;
var t:int64;
begin
	if b=0 then
	begin
		x:=1;y:=0;
		exit(a);
	end else
	begin
		ex_Euclid:=ex_Euclid(b,a mod b,x,y);
		t:=x;x:=y;y:=t-(a div b)*y;
	end;
end;

procedure solve1;
var i:longint;
begin
	ans:=0;
	for i:=0 to c div wr do
		ans:=max(ans,i*hr+(c-i) div wb*hb);
end;

procedure solve2;
var x,y,xx,yy,tem,a,b,aa,bb:int64;
    i:longint;
begin
	ans:=0;
        aa:=wr;bb:=wb;
        for i:=0 to wr-1 do
	begin

                tem:=gcd(gcd(aa,bb),(c-i));
                a:=aa div tem;b:=bb div tem;
                ex_Euclid(a,b,x,y);
                x:=x*(c-i) div tem;y:=y*(c-i) div tem;
		if x<0 then
		begin
			tem:=(-x) div b;
			x:=x+tem*b;y:=y-tem*a;
			if x<0 then
			begin
				x:=x+b;y:=y-a;
			end;
		end;
		if y<0 then
		begin
			tem:=(-y) div a;
			x:=x-tem*b;y:=y+tem*a;
			if y<0 then
			begin
				x:=x-b;y:=y+a;
			end;
		end;
		if hr/wr>=hb/wb then
		begin
			if y>0 then
			begin
				tem:=y div a;
				x:=x+tem*b;y:=y-tem*a;
			end;
		end else
		begin
			if x>0 then
			begin
				tem:=x div b;
				x:=x-tem*b;y:=y+tem*a;
			end;
		end;
		ans:=max(ans,x*hr+y*hb);
	end;
end;

begin
       // assign(input,'cft3.in');reset(input);
	readln(c,hr,hb,wr,wb);
	if wr>sqrt(c) then solve1 else solve2;
	writeln(ans);
end.

program problem3;

function gcd( a, b: int64 ): int64;
var	c: int64;
begin
	while ( b > 0 ) do
	begin
		c := b;
		b := a mod b;
		a := c;
	end;

	exit( a );
end;

var	c, hr, hb, wr, wb, lcm, ans, best, cur: int64;
	i: longInt;

begin
	readln( c, hr, hb, wr, wb );

	lcm := ( wr * wb ) div gcd( wr, wb );

	if ( hr * wb > hb * wr ) then
		ans := ( c div lcm ) * ( lcm div wr ) * hr
	else
		ans := ( c div lcm ) * ( lcm div wb ) * hb;

	c := c mod lcm;

	if ( wr < wb ) then
	begin
		wb := wb xor wr;
		wr := wb xor wr;
		wb := wb xor wr;

		hb := hb xor hr;
		hr := hb xor hr;
		hb := hb xor hr;
	end;

	best := 0;
	for i := 0 to longInt(c div wr) do
	begin
		cur := hr * i + hb * ( ( c - i * wr ) div wb );

		if ( cur > best ) then
			best := cur;
	end;

	writeln( ans + best );
end.

var c,hr,hb,wr,wb,ans:longint;
begin
  readln(c,hr,hb,wr,wb);
  if (hr/wr)> (hb/wb) then
  begin
    ans:=ans+(c div wr)*hr;
    c:=c mod wr;
    if (c<wb) and (c<>0) then
    if (c+wr)>wb
    then
    begin
    ans:=ans-hr;
    c:=c+wr;
    end;
    ans:=ans+(c div wb)*hb;
  end
  else
  begin
    ans:=ans+(c div wb)*hb;
    c:=c mod wb;
    if (c<wr) and (c<>0) then
    if (c+wb)>wr
    then
    begin
    ans:=ans-hb;
    c:=c+wb;
    end;
    ans:=ans+(c div wr)*hr;
  end;
  writeln(ans);
end.

program zad_C;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;
var
  c, wr, wb, hr, hb, res, num, num1, l, r, m, m1: int64;
  i : integer;
function get(x : integer):int64;
  var
    w : int64;
  begin
    if(trunc(1.0 * x * wr) > c) then begin
      get := 0;
      exit;
    end;

    w := (c mod wr) div wb;
    get := trunc(1.0 * x * hr) +  trunc(1.0 * w * hb);
  end;

function nod(a, b : int64):int64;
  var
    r : int64;
  begin
    repeat
      r := a mod b;
      a := b;
      b := r;
    until(b = 0);

    nod := a;
  end;

function nok(a, b : int64):int64;
  begin
    nok := trunc(1.0 * a * b) div nod(a, b);
  end;
begin
  readln(c, hr, hb, wr, wb);

  num := nok(wr, wb);
  num1 := max(trunc(1.0 * hr * (num div wr)), trunc(1.0 * hb * (num div wb)));
  res := trunc(1.0 * (c div num) * num1);

  c := c mod num;

  l := 0;
  r := (c div wr);
  while(l + 2 < r) do begin
    m := l + ((r - l) div 3);
    m1 := r - ((r - l) div 3);

    if(get(m1) > get(m)) then
      l := m
    else
      l := m1;
  end;

  writeln(res + max(get(l), max(get(l + 1), get(l + 2))));

  readln(c);
end.

program zad_C;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;
var
  c, wr, wb, hr, hb, res, num, num1 : int64;
  i : integer;
function get(x : integer):int64;
  var
    w : int64;
  begin
    if(trunc(1.0 * x * wr) > c) then begin
      get := 0;
      exit;
    end;

    w := (c mod wr) div wb;
    get := trunc(1.0 * x * hr) +  trunc(1.0 * w * hb);
  end;

function nod(a, b : int64):int64;
  var
    r : int64;
  begin
    repeat
      r := a mod b;
      a := b;
      b := r;
    until(b = 0);

    nod := a;
  end;

function nok(a, b : int64):int64;
  begin
    nok := trunc(1.0 * a * b) div nod(a, b);
  end;
begin
  readln(c, hr, hb, wr, wb);

  num := nok(wr, wb);
  num1 := max(trunc(1.0 * hr * (num div wr)), trunc(1.0 * hb * (num div wb)));
  res := trunc(1.0 * (c div num) * num1);

  c := c mod num;

  num := 0;
  for i := 0 to c div wr do
    num := max(num, get(i));

  writeln(res + num);

  readln(c);
end.

program zad_C;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;
var
  c, wr, wb, hr, hb, res, num, num1 : int64;
  i : integer;
function get(x : integer):int64;
  var
    w : int64;
  begin
    if(trunc(1.0 * x * wr) > c) then begin
      get := 0;
      exit;
    end;

    w := (c - trunc(1.0 * x * wr)) div wb;
    get := trunc(1.0 * x * hr) +  trunc(1.0 * w * hb);
  end;

function nod(a, b : int64):int64;
  var
    r : int64;
  begin
    repeat
      r := a mod b;
      a := b;
      b := r;
    until(b = 0);

    nod := a;
  end;

function nok(a, b : int64):int64;
  begin
    nok := trunc(1.0 * a * b) div nod(a, b);
  end;
begin
  readln(c, hr, hb, wr, wb);

  num := nok(wr, wb);
  num1 := max(trunc(1.0 * hr * (num div wr)), trunc(1.0 * hb * (num div wb)));
  res := trunc(1.0 * (c div num) * num1);

  c := c mod num;

  num := 0;
  for i := 0 to c div wr do
    num := max(num, get(i));

  writeln(res + num);

  readln(c);
end.

program zad_C;

{$APPTYPE CONSOLE}

uses
  SysUtils, Math;
var
  c, wr, wb, hr, hb, res, num, num1 : int64;
  i : integer;
function get(x : integer):int64;
  var
    w : int64;
  begin
    if(trunc(1.0 * x * wr) > c) then begin
      get := 0;
      exit;
    end;

    w := (c - trunc(1.0 * x * wr)) div wb;
    get := trunc(1.0 * x * hr) +  trunc(1.0 * w * hb);
  end;

function nod(a, b : int64):int64;
  var
    r : int64;
  begin
    repeat
      r := a mod b;
      a := b;
      b := r;
    until(b = 0);

    nod := a;
  end;

function nok(a, b : int64):int64;
  begin
    nok := trunc(1.0 * (a div nod(a, b)) * b);
  end;
begin
  readln(c, hr, hb, wr, wb);

  num := nok(wr, wb);
  num1 := max(trunc(1.0 * hr * (num div wr)), trunc(1.0 * hb * (num div wb)));
  res := trunc(1.0 * (c div num) * num1);

  c := c mod num;

  num := 0;
  for i := 0 to c div wr do
    num := max(num, get(i));

  writeln(res + num);

  readln(c);
end.

program CC;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;

var hr,hb,wr,wb,c,i: longint;
ans,thisans,mult: int64;

procedure swap(var a,b: longint);
var c: longint;
begin
  c:=a;
  a:=b;
  b:=c;
end;

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

  read(c,hr,hb,wr,wb);

  if hr / wr < hb / wb then begin
    swap(hr,hb);
    swap(wr,wb);
  end;

  ans := 0;
  for i:=0 to trunc(sqrt(c)) do begin
    mult := (c - wb*i) div wr;
    thisans := hb*i + hr*mult;
    ans := max(ans,thisans);
  end;

  write(ans);
end.

program CC;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;

var hr,hb,wr,wb: int64;
i,c: longint;
ans,thisans,mult1,mult2: int64;

procedure swap(var a,b: int64);
var c: int64;
begin
  c:=a;
  a:=b;
  b:=c;
end;

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

  read(c,hr,hb,wr,wb);

  if hr / wr < hb / wb then begin
    swap(hr,hb);
    swap(wr,wb);
  end;

  if wr > c then begin
    write((c div wb) * hb);
    halt;
  end;

  ans := 0;
  for i:=0 to trunc(sqrt(c)) do begin
    mult1 := min(c div wb, i);
    mult2 := (c - wb*i) div wr;
    thisans := hb*mult1 + hr*mult2;
    ans := max(ans,thisans);
  end;

  write(ans);
end.

program CC;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;

var hr,hb,wr,wb,c,i: longint;
ans,thisans,mult: int64;

procedure swap(var a,b: longint);
var c: longint;
begin
  c:=a;
  a:=b;
  b:=c;
end;

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

  read(c,hr,hb,wr,wb);

  if hr / wr < hb / wb then begin
    swap(hr,hb);
    swap(wr,wb);
  end;

  ans := 0;
  for i:=0 to trunc(sqrt(c)) + 1 do begin
    mult := (c - wb*i) div wr;
    thisans := hb*i + hr*mult;
    ans := max(ans,thisans);
  end;

  write(ans);
end.

program CC;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;

var hr,hb,wr,wb,c,i: longint;
ans,thisans,mult1,mult2: int64;

procedure swap(var a,b: longint);
var c: longint;
begin
  c:=a;
  a:=b;
  b:=c;
end;

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

  read(c,hr,hb,wr,wb);

  if hr / wr < hb / wb then begin
    swap(hr,hb);
    swap(wr,wb);
  end;

  ans := 0;
  for i:=0 to trunc(sqrt(c)) do begin
    mult1 := min(c div wb, i);
    mult2 := (c - wb*i) div wr;
    thisans := hb*mult1 + hr*mult2;
    ans := max(ans,thisans);
  end;

  write(ans);
end.

program CC;

{$APPTYPE CONSOLE}

uses
  SysUtils,math;

var hr,hb,wr,wb: int64;
i,c: longint;
ans,thisans,mult1,mult2: int64;

procedure swap(var a,b: int64);
var c: int64;
begin
  c:=a;
  a:=b;
  b:=c;
end;

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

  read(c,hr,hb,wr,wb);

  if hr / wr < hb / wb then begin
    swap(hr,hb);
    swap(wr,wb);
  end;

  ans := 0;
  for i:=0 to trunc(sqrt(c)) do begin
    mult1 := min(c div wb, i);
    mult2 := (c - wb*i) div wr;
    thisans := hb*mult1 + hr*mult2;
    ans := max(ans,thisans);
  end;

  write(ans);
end.


var n,i,k:integer;
e,t:string;
L,D,U,R:integer;
begin
readln(n);
readln(e);
for i:=1 to n do begin
t:=copy(e,i,1);
if t='U' then
U:=U+1;
if t='D' then
D:=D+1;
if t='L' then
L:=L+1;
if t='R' then
R:=R+1;
end;
if U<D then
k:=k+U*2;
if U>D then
k:=k+D*2;
if L<R then
k:=k+L*2;
if L>R then
k:=k+R*2;
if U=D then
k:=k+u*2;
if L=R then
k:=k+R*2;
writeln(k);
end.

program robot;
var n,i,k,l,r,u,d: integer;
    s: string;
begin
  readln(n);
  read(s);
  l:=0;
  r:=0;
  u:=0;
  d:=0;
  for i:=1 to n do
    begin
      if s[i]='U' then inc(u);
      if s[i]='L' then inc(l);
      if s[i]='R' then inc(r);
      if s[i]='D' then inc(d);
    end;
  k:=abs(l-r)+abs(u-d);
  k:=n-k;
  writeln(k);
end.

var s:string;
n,i,b,l,r,u,d:integer;
begin
readln (n);
read (s);
for i:=1 to n do
if s[i]='L' then inc (l);
for i:=1 to n do
if s[i]='R' then inc (r);
for i:=1 to n do
if s[i]='D' then inc (d);
for i:=1 to n do
if s[i]='U' then inc (u);
if l<r then
b:=b+l
else
b:=b+r;
if u<d then
b:=b+u
else
b:=b+d;
write (b*2);
end.

var  n,i,u,d,l,r:integer;
     c:char;
begin
readln(n);
for i:=1 to n do begin
     read(c);
     case c of
          'U':inc(u);
          'D':inc(d);
          'L':inc(l);
          'R':inc(r);
     end;
end;
write(2*(min(u,d)+min(l,r)));
end.

VAR l,r,d,u,i,n:integer;
k:string;
begin
readln(n);
read(k);
for i:=1 to n do begin
if k[i]='L' then l+=1 else if k[i]='R' then r+=1 else if k[i]='D' then d+=1 else if k[i]='U' then u+=1;
end;
write(n-(abs(u-d)+abs(r-l)));
end.

Program CodeF_Edu_32;
var s:string;
    i,n,d,u,r,l,itog:longint;

begin
readln(n);
readln(s);
itog:=0;
for i:=1 to length(s) do
  begin
  if s[i]='U' then inc(u);
  if s[i]='D' then inc(d);
  if s[i]='L' then inc(l);
  if s[i]='R' then inc(r);
  end;

if l<r then itog:=itog+2*l else
  itog:=itog+2*r;
if u<d then itog:=itog+2*u else
  itog:=itog+2*d;
writeln(itog);
end.

PROGRAM W;
var L,D,U,R,q,i,n:integer;
    s:string;
BEGIN
 readln(n);
 readln(s);
 L:=0;
 For i:=1 to n do
  if s[i]='L' then L:=L+1;
 D:=0;
 For i:=1 to n do
  if s[i]='D' then D:=D+1;
 U:=0;
 For i:=1 to n do
  if s[i]='U' then U:=U+1;
 R:=0;
 For i:=1 to n do
  if s[i]='R' then R:=R+1;
  q:=0;
 if D>U then q:=q+2*U
 else q:=q+2*D;
 if R>L then q:=q+2*L
  else q:=q+2*R;
 writeln(q);
END.

var
strok:string;
str:string;
i:integer;
n:integer;
x, y:integer;
res:integer;
begin
readln(n);
for i:=1 to n do
begin
read(str);
strok:=strok+str;
end;
writeln;
x:=0;
y:=0;
for i:=1 to n do
begin
if (strok[i]='U' ) then
y+=1
else if (strok[i]='D') then
y-=1;
if (strok[i]='L') then
x-=1
else if (strok[i]='R') then
x+=1;
end;
res:=length(strok)-abs(x)-abs(y);

if (res<0) then
writeln(0)
else
writeln(res);
end.

program qwerty;
var a:array [1..4] of longint;
n,i:longint;
ch:char;

function min(a,b:longint):longint;
begin
  if a>b then min:=b else min:=a;
end;

begin
readln(n);
for i:=1 to n do
  begin
    read(ch);
    if ch='L' then inc(a[1]) else
    if ch='R' then inc(a[2]) else
    if ch='U' then inc(a[3]) else
    if ch='D' then inc(a[4]);
  end;
  writeln(min(a[1],a[2])*2+min(a[3],a[4])*2);
end.

   const eps=0.0000000001;    nn=200000; besk=8000000000;
   label 1,2,23;
    type    real=extended;  {string=ansistring;}
    mas=array [0..nn] of int64;   point=record x,y,z:real;end;
    ppoint=^point;
    zap=record p1,p2,p3 :int64;end; zap1=^zap;
    mass=array[0..nn] of longint;mass1=^mass;
    var aa,bb,cc,pos,kk,t3,ot,prod,ost,kol,zapas,dist,r,ll,rr,sum,q,h,t1,t2,modul,t,tt,time,p,gr,qq,pmax,imax,p1,p2,p3,p4,l,nm,n,s1,s2,m,s,smin,cen,dl,i1:int64;
    ii,jj,step, k,i,j:longint;
   {  aa,bb,cc:int64;
   }  pp:array[-1000 ..1000] of point;
     fact,slow,z,pr,next,d1,d2,aold,b,a,x,y:array[-1000..2000000] of int64;
     dd:array[-10..2000,-10..200] of int64;
   zanx,zany:array[-10..20000000] of boolean;
    marked,zan:array[-10..10000000] of boolean;
    ss, ss1,ss2:string;
     fl:array[0..200] of boolean;
    ch:char;
    d :array [-10..250000,-1..50] of int64;
    dda:array [0..201,-10..30] of int64;
    sss:array[-10..500000] of string;
    st2:array[0..nn] of string;
     pr1,pr2,v0,v1,s0,t0:extended;
     zv:point;
     prob:array[-1..200,-1..200] of extended;

     yot:double;
     qqq,aaa,bbb,ccc:extended;
       flag,flag1:boolean;
    function al(x,y:extended):extended; var t0,t1,p,s0,s1:extended;
 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 degg(a,k:int64):int64; var p:int64;
begin
 if k<=0 then degg:=1 else if odd(k) then degg:=degg(a,k-1)*a mod modul else
   begin  p:=degg(a,k div 2); degg:=p*p mod modul;   end;

end;

function dom(length,num:longint):boolean;
var t,i,j:longint; fll,fl:boolean;
begin
    fl:=true;
    for i:=1 to n-length+1 do if d[i+length-1,num]-d[i-1,num]=0 then fl:=false;

    dom:=fl;

end;

function domm(length:longint):boolean;
var i,j:longint; fl:boolean;
begin
  fl:=false;
  for i:=0 to 25 do if dom(length,i) then fl:=true;

  domm:=fl;
end;

begin
  {  assign (input,'input.txt');reset(input);
      assign (output,'output.txt'); rewrite(output);
   }
      readln(n);
      read(ss);
      n:=length(ss);
      for i:=1 to n do
        begin
          if ss[i]='U' then inc(x[1]);
          if ss[i]='D' then inc(x[2]);
          if ss[i]='R' then inc(x[3]);
          if ss[i]='L' then inc(x[4]);

        end;

       writeln(2*(min(x[1],x[2])+min(x[3],x[4])));

   1:          close(output);
end.

{$APPTYPE CONSOLE}

var i,n,k,l,r,u,d: integer;
    c: char;
begin
 readln(n);
 for i:=1 to n do
 begin
  read(c);
  case c of
  'L': inc(l);
  'R': inc(r);
  'U': inc(u);
  'D': inc(d);
  end
 end;
 if l>r then k:=2*r else k:=2*l;
 if u>d then k:=k+2*d else k:=k+2*u;
 write(k);
end.

program Project1;

var
  n, i: SmallInt;
  c: char;
  U, D, L, R: SmallInt;

begin
  ReadLn(n);
  U := 0; D := 0; L := 0; R := 0;
  For i := 1 to n do
  begin
    If i < n then Read(c) else ReadLn(c);
    Case ord(c) of
      68: D := D+1;
      76: L := L+1;
      82: R := R+1;
      85: U := U+1;
    end;
  end;
  If D > U then D := U;
  If R > L then R := L;
  WriteLn(D+R+D+R);
end.

var x,y,n,i: integer;
    s:string;
    c:char;
Begin
     readln(n);
     read(s);
     for i:= 1 to n do
         begin
              c:=s[i];
              case c of
              'L': dec(x);
              'R': inc(x);
              'U': dec(y);
              'D': inc(y);
              end;
         end;
     write(n-abs(x)-abs(y));
end.

program Buggy_Robot;

const
   maxn = 103;

var
   n: longint;
   s: string;

   x, y, i: longint;

begin
   readln(n);
   readln(s);

   x := 0; y := 0;
   for i := 1 to n do
      case s[i] of
         'L': dec(x);
         'R': inc(x);
         'U': inc(y);
         'D': dec(y);
      end;

   if x < 0 then x := -x;
   if y < 0 then y := -y;
   writeln(n - (x + y));
end.

var a,b,c,d,i,n,k:longint;
s:string;
begin
readln(n);
readln(s);
for i:=1 to n do
begin
case s[i] of
'U':inc(a);
'D':inc(b);
'L':inc(c);
'R':inc(d);
end;
end;
if (a>0) and (b>0) then if b>a then inc(k,a*2) else inc(k,b*2);
if (c>0) and (d>0) then if c>d then inc(k,d*2) else inc(k,c*2);
writeln(k);
end.

const
        fi='';//'cf888b.inp';
        fo='';//'cf888b.out';
var
        s:string;
        l,i,x,y:longint;
begin
        assign(input,fi);reset(input);
        assign(output,fo);rewrite(output);
        readln(l);
        readln(s);
        for i:=1 to l do
                if (s[i]='L') then x-=1
                 else if (s[i]='R') then x+=1
                  else if (s[i]='U') then y+=1 else y-=1;
        write(l-abs(x)-abs(y));
        close(input);
        close(output);
end.

var n,i,u,d,l,r,k1,k2,k:integer;
a:array[0..1001] of char;
begin
readln(n);
for i:=1 to n do read(a[i]);
U:=0;
D:=0;
L:=0;
R:=0;
for i:=1 to n do begin
if a[i]='U' then U:=U+1;
if a[i]='D' then D:=D+1;
if a[i]='L' then L:=L+1;
if a[i]='R' then R:=R+1;
end;
if (U<>0) and (D<>0) then begin if U<=D then k1:=2*U else k1:=2*D; end else k:=0;
if (L<>0) and (R<>0) then begin if L<=R then k2:=2*L else k2:=2*R; end else k:=0;
k:=k1+k2;
writeln(k);

end.

program asdf;
var n, i, x, y:longint;
s:string;
begin
readln(n);
readln(s);
x:=0; y:=0;
for i:=1 to n do
    case s[i] of
         'R': x:=x+1;
         'L': x:=x-1;
         'U': y:=y+1;
         'D': y:=y-1;
    end;
write(n-abs(x)-abs(y));
end.

var a: string;
n, i, X, Y: integer;
begin
    readln(n);
    read(a);
    X:=0;
    Y:=0;
    for i:=1 to n do begin
        if a[i]='L' then dec(X)
        else if a[i]='R' then inc(X)
        else if a[i]='U' then inc(Y)
        else if a[i]='D' then dec(Y);
    end;
    writeln(n-abs(X)-abs(Y));
end.

uses math;
var
        n, i, U, D, R, L : longint;
        a : char;
begin
  readln( n );
  for i := 1 to n do
        begin
          read( a );
          case a of
            'U' : inc( U );
            'D' : inc( D );
            'R' : inc( R );
            'L' : inc( L );
            end;
        end;
  writeln( ( min( U, D ) + min( R, L ) ) * 2 );
end.

var S:String;
n,i,u,d,l,r,ans,min1,min2:longint;
begin
readln(n);
readln(S);
for i:=1 to Length(S) do begin
if S[i]='R' then inc(r);
if S[i]='L' then inc(l);
if S[i]='U' then inc(u);
if S[i]='D' then inc(d);
                        end;
if (r>0) and (l>0) then begin
                          if r>l then min1:=l
                                  else min1:=r;
                        end;
if (u>0) and (d>0) then begin
                          if u>d then min2:=d
                                 else min2:=u;
                        end;
ans:=(min1*2)+(min2*2);
write(ans);
end.

var
A:string;
b, c, i, x, y: integer;

begin
readln(b);
read(A);
c:=length(A);
if c=b then begin
for i := 1 to b do
begin
if A[i] = 'L' then x := x - 1 else
if A[i] = 'R' then x := x + 1 else
if A[i] = 'U' then y := y + 1 else
if A[i] = 'D' then y := y - 1;
end;
c :=b - (abs(x));
c := c - (abs(y));
write(c);
end;
end.

function min(a,b:integer):integer;
 begin
  if a<b then min:=a
         else min:=b;
 end;
var a:array[1..4] of integer;
 n,i,x,y:integer;
 s:string;
begin
readln(n);
readln(s);
for i:=1 to length(s) do
 if s[i]='U' then inc(a[1])
             else if s[i]='D' then inc(a[2])
                              else if s[i]='L' then inc(a[3])
                                               else inc(a[4]);
x:=min(a[1],a[2]);
y:=min(a[3],a[4]);
writeln(x*2+y*2);
end.

program cay_888_b;
var s:array[1..100]of char;
    i,n,x,y:longint;
begin
 readln(n);
 for i:=1 to n do read(s[i]);
 x:=0; y:=0;
 for i:=1 to n do
 case s[i] of
  'U': y:=y+1;
  'D': y:=y-1;
  'R': x:=x+1;
  'L': x:=x-1;
 end;
 write(n-abs(x)-abs(y));
end.

uses math;
var n,i,res:byte;
    ch:char;
    dem:array[1..4] of byte;
begin
                res:=0;
                readln(n);
                for i:=1 to n do
                begin
                        read(ch);
                        if ch='U'then inc(dem[1]);
                        if ch='D'then inc(dem[3]);
                        if ch='L'then inc(dem[2]);
                        if ch='R'then inc(dem[4]);
                end;

                writeln(min(dem[1],dem[3])*2+min(dem[2],dem[4])*2);
                readln;
end.

uses math;
var s:string;
    i,l,r,u,d,n:longint;
begin
 readln(n);
 readln(s);
 l:=0;
 r:=0;
 d:=0;
 u:=0;
 for i:=1 to n do
     if s[i]='L' then inc(l)
     else
     if s[i]='R' then inc(r)
     else
     if s[i]='U' then inc(u)
     else
     inc(d);
 writeln(2*min(l,r)+2*min(u,d));
end.

Program z1;
Var
   i,n,k,x,y,max:integer;
   a:string;
begin
x:=0;
y:=0;
max:=0;
Readln(n);
Readln(a);
For i:=1 to n do begin
If a[i]='U' then y:=y+1;
If a[i]='D' then y:=y-1;
If a[i]='L' then x:=x-1;
If a[i]='R' then x:=x+1;
end;
max:=n-(abs(x)+abs(y));
Writeln(max)
end.

var n,i,max:integer;str:string;a:array [1..4] of integer;
begin
readln(n);
readln(str);for i:=1 to 4 do a[i]:=0;
for i:=1 to n do begin
if copy(str,i,1)='U' then a[1]:=a[1]+1;
if copy(str,i,1)='D' then a[2]:=a[2]+1;
if copy(str,i,1)='R' then a[3]:=a[3]+1;
if copy(str,i,1)='L' then a[4]:=a[4]+1
end;max:=0;
if a[1]>a[2] then max:=max+a[2]*2 else max:=max+a[1]*2;
if a[3]>a[4] then max:= max+a[4]*2 else max:=max+a[3]*2;
writeln(max);
end.

Const	fi='';
	fo='';

var	f:text;
	n,kq:integer;
	s:string;

PRocedure NhapDL;
var	i:integer;
begin
	Assign(f,fi); Reset(f);
	Readln(f,n);
	Read(f,s);
    Close(F);
end;

PRocedure Xuly;
var	i,l,r,d,u:integer;
begin
        kq:=0;
        l:=0;
        r:=0;
        u:=0;
        d:=0;
	For i:= 1 to n do
	if s[i] ='L' then inc(l)
	else if s[i] = 'R' then inc(r)
	else if s[i] = 'U' then inc(u)
	else inc(d);
	if l > r then kq:= kq + (2*r)
	else kq:= kq+ (2*l);
	if u > d then kq:= kq +(2*d)
	else kq:= kq +(2*u);
end;

PRocedure XuatDL;
begin
	assign(f,fo); Rewrite(f);
	Write(f,kq);
	Close(f);
end;

Begin
	NhapDL;
	Xuly;
	XuatDL;
end.

var s:string; l,i,x,y:integer;
begin
  readln(l); x:=0;
  readln(s); y:=0;
  for i:=1 to l do
    if (s[i]='L') then x-=1
    else if (s[i]='R') then x+=1
    else if (s[i]='U') then y+=1
    else y-=1;
  write(l-abs(x)-abs(y))
end.

 var i,x,y,n:longint;
     s:string;
begin
 readln(n);
 readln(s);
 x:=0;
 y:=0;
 for i:=1 to n do
   case s[i] of
   'L':inc(x);
   'R':dec(x);
   'U':inc(y);
   'D':dec(y);
   end;
 x:=abs(x);
 y:=abs(y);
 writeln(n-x-y);
end.

var n,i:longint;
    s:string;
    c:array['A'..'Z']of longint;
    ans:longint;
function min(a,b:longint):longint;
begin
  if a<b then exit(a) else exit(b);
end;
begin
  readln(n);
  readln(s);
  for i:=1 to n do
  inc(c[s[i]]);
  ans:=min(c['U'],c['D'])*2+min(c['L'],c['R'])*2;
  writeln(ans);
end.

var s:string;
i, n, k, l, z, x, r:longint;
begin
readln(n);
readln(s);
for i:=1 to n do begin
if s[i]='U' then
k:=k+1;
if s[i]='D' then
l:=l+1;
if s[i]='L' then
z:=z+1;
if s[i]='R' then
x:=x+1;
end;
if k<l then
r:=k
else r:=l;
if z<x then
r:=r+z
else r:=r+x;
writeln(r*2) ;
end.

var i,k,n:longint;
x,y:longint;
len:longint;
dx,dy:longint;
s:string;
begin
 readln(n);
 readln(s);

 len:=length(s);

 for i:=1 to len do
  begin
   if s[i] = 'U' then inc(y);
   if s[i] = 'D' then dec(y);
   if s[i] = 'L' then dec(x);
   if s[i] = 'R' then inc(x);
  end;

if (x=0) and (y=0) then
 begin
  writeln(n);
 end
else
 begin
  dx:=abs(x);
  dy:=abs(y);
  writeln(n - dx - dy);
 end;
end.

uses math;
var a,b,c,d:longint;
    x:char;
begin
  readln;
  while not eoln do
  begin
    read(x);
    case x of
      'L':inc(a);
      'R':inc(b);
      'U':inc(c);
      'D':inc(d);
    end;
  end;
  writeln(2*(min(a,b)+min(c,d)));
end.

var n,i,kr,kl,ku,kd,k:integer;
    s:array[1..100] of char;
begin
  readLn(n);
  for i:=1 to n do
    read(s[i]);

  for i:=1 to n do
    begin
      case s[i] of
      'R':kr:=kr+1;
      'L':kl:=kl+1;
      'U':ku:=ku+1;
      'D':kd:=kd+1;
      end;
    end;

   if kr=kl then k:=kr*2
    else if (kr=0)or(kl=0) then k:=0
      else if kr<kl then k:=kr*2
        else k:=kl*2;

  if ku=kd then k:=k+ku*2
    else if (ku=0)or(kd=0) then k:=k+0
      else if ku<kd then k:=k+ku*2
        else k:=k+kd*2;

   writeLn(k);
end.

var a:string;
var l,r,d,u:int64;
var n,i:integer;
begin
readln(n);
readln(a);

for i:=1 to n do
begin
    if a[i]='D' then d:=d+1;
    if a[i]='L' then l:=l+1;
    if a[i]='R' then r:=r+1;
    if a[i]='U' then u:=u+1;
end;
writeln(n-abs(r-l)-abs(u-d));

end.

Var
 n, x, y, j: integer;
 c: char;

begin
 readln(n);
 x := 0;
 y := 0;
 for j := 1 to n do
 begin
  read(c);
  case c of
   'U': Inc(y);
   'D': Dec(y);
   'R': Inc(x);
   'L': Dec(x);
  end;
 end;
 x := abs(x);
 y := abs(y);
 write(n - x - y);
end.

Program z2;
var
n,uc,dc,lc,rc,s: integer;
c:char;
begin
readln(n);
for var i:=1 to n do begin
read(c);
if (c='U') then inc(uc) else
 if (c='D') then inc(dc) else
  if (c='L') then inc(lc) else
   if (c='R') then inc(rc);
end;
if (uc=dc) or (uc<dc) then inc (s,uc+uc) else inc(s,dc+dc);
if (lc=rc) or (lc<rc) then inc (s,lc+lc) else inc(s,rc+rc);
writeln(s);

end.

var
   n, i, cou_u, cou_d, cou_l, cou_r, it: integer;
   str: string;

begin
   readln (n);
   readln (str);

   for i:= 1 to n do
      begin
         if str[i] = 'U' then cou_u:= cou_u + 1;
         if str[i] = 'D' then cou_d:= cou_d + 1;
         if str[i] = 'L' then cou_l:= cou_l + 1;
         if str[i] = 'R' then cou_r:= cou_r + 1;
      end;

   it:=abs(cou_u - cou_d)+abs(cou_l - cou_r);
   writeln (n-it);
end.

var
  a:string;
  b, c, i, x, y: integer;

begin
  readln(b);
  read(a);
  c:=length(a);
  if c=b then begin
  for i := 1 to b do
  begin
    if a[i] = 'L' then x := x - 1 else
    if a[i] = 'R' then x := x + 1 else
    if a[i] = 'U' then y := y + 1 else
    if a[i] = 'D' then y := y - 1;
  end;
  c :=b - (abs(x));
  c := c - (abs(y));
  write(c);
  end;
end.

begin
var n:integer;
var s:String;
readln(n);
readln(s);
var a:= new char[n];
a:=s.ToCharArray;

var x:=0;
var y:=0;
for var i:=0 to a.Length-1 do
begin
case a[i] of
'U': y:=y+1;
'D': y:=y-1;
'L': x:=x-1;
'R': x:=x+1
end;
end;
var res:=abs(x)+abs(y);
res:=n-res;
writeln(res);
end.

var s: string;
    n, i, u, d, l, r: longint;
begin
readln (n);
readln (s);
for i:=1 to length(s) do
    begin
    if s[i]='U' then u:=u+1;
    if s[i]='D' then d:=d+1;
    if s[i]='L' then l:=l+1;
    if s[i]='R' then r:=r+1;
    end;
writeln (min(u,d)*2+min(l,r)*2);
end.

var
  n, l, i, k, x, y: integer;
  s: array[1..100] of char;

begin
  Readln(n);
  for i := 1 to n do
    Read(s[i]);
  x := 0; y := 0;
  for i := 1 to n do
  begin
    if s[i] = 'R' then x := x + 1;
    if s[i] = 'L' then x := x - 1;
    if s[i] = 'U' then y := y + 1;
    if s[i] = 'D' then y := y - 1;
  end;
  writeln(n-abs(x)-abs(y));
end.

Program z2;
var
n,uc,dc,lc,rc,s: integer;
c:char;
begin
readln(n);
for var i:=1 to n do begin
read(c);
if (c='U') then inc(uc) else
 if (c='D') then inc(dc) else
  if (c='L') then inc(lc) else
   if (c='R') then inc(rc);
end;
if (uc=dc) then inc (s,uc+dc);
if (lc=rc) then inc (s,lc+rc);
writeln(s);

end.

var x,y,n,i: integer;
    s:string;
    c:char;
Begin
     readln(n);
     read(s);
     for i:= 1 to n do
         begin
              c:=s[i];
              case c of
              'L': dec(x);
              'R': inc(x);
              'U': dec(y);
              'D': inc(y);
              end;
         end;
     write(n-(n-abs(x)-abs(y)));
end.

var n,i,y,x,max:integer;
a:array[0..1001] of char;
begin
readln(n);
for i:=1 to n do read(a[i]);
y:=0;
x:=0;
max:=0;
for i:=1 to n do begin
if a[i]='U' then y:=y+1;
if a[i]='D' then y:=y-1;
if a[i]='L' then x:=x-1;
if a[i]='R' then x:=x+1;

if (y=0) and (x=0) then max:=i;
end;
writeln(max);
end.

program cay_888_b;
var s:string;
    i,n,x,y,k,l:longint;
begin
 readln(n);
 readln(s);
 k:=0; l:=0;
 x:=0; y:=0;
 i:=1;
 while i<=n do begin
  if s[i]='U' then y:=y+1 else
  if s[i]='D' then y:=y-1 else
  if s[i]='R' then x:=x+1 else x:=x-1;
  k:=k+1;
  if (x=0)and(y=0)then begin
   l:=l+k; k:=0;
  end;
  i:=i+1;
 end;
 if (x<>0)and(y<>0) then writeln(0) else writeln(l);
end.

program cay_888_b;
var s:string;
    i,n,x,y,k:longint;
begin
 readln(n);
 readln(s);
 k:=0;
 x:=0; y:=0;
 i:=1;
 while i<=n do begin
  if s[i]='U' then y:=y+1 else
  if s[i]='D' then y:=y-1 else
  if s[i]='R' then x:=x+1 else x:=x-1;
  k:=k+1;
  if (x=0)and(y=0)then begin
   writeln(k); break;
  end
  else i:=i+1;
 end;
 if (x<>0)and(y<>0) then writeln(0);
end.

program cay_888_b;
var s:string;
    i,n,x,y,k,l:longint;
begin
 readln(n);
 readln(s);
 for i:=1 to n do
 case s[i] of
  'U': y:=y+1;
  'D': y:=y-1;
  'R': x:=x+1;
  'L': x:=x-1;
 end;
 write(n-abs(x)-abs(x));
end.

uses math;
var n,i,res:byte;
    ch:char;
    dem:array[1..4] of byte;
begin
                res:=0;
                readln(n);
                for i:=1 to n do
                begin
                        read(ch);
                        if ch='U'then inc(dem[1]);
                        if ch='D'then inc(dem[3]);
                        if ch='L'then inc(dem[2]);
                        if ch='R'then inc(dem[4]);
                          if res<min(dem[1],dem[3])*2 then res:=min(dem[1],dem[3])*2;
                if res<min(dem[2],dem[4])*2 then res:=min(dem[2],dem[4])*2;
                if res<min(min(min(dem[1],dem[2]),dem[3]),dem[4])*4 then res:=min(min(min(dem[1],dem[2]),dem[3]),dem[4])*4;
                end;

                writeln(res);
                readln;
end.

uses math;
var n,i,res:byte;
    ch:char;
    dem:array[1..4] of byte;
begin
                res:=0;
                readln(n);
                for i:=1 to n do
                begin
                        read(ch);
                        if ch='U'then inc(dem[1]);
                        if ch='D'then inc(dem[3]);
                        if ch='L'then inc(dem[2]);
                        if ch='R'then inc(dem[4]);
                end;
                if res<min(dem[1],dem[3])*2 then res:=min(dem[1],dem[3])*2;
                if res<min(dem[2],dem[4])*2 then res:=min(dem[2],dem[4])*2;
                if res<min(min(min(dem[1],dem[2]),dem[3]),dem[4])*4 then res:=min(min(min(dem[1],dem[2]),dem[3]),dem[4])*4;
                writeln(res);
                readln;
end.

Program z1;
Var
   x,y:real;
   i,n,k,max:integer;
   a:array[1..101]of char;
begin
x:=0;
y:=0;
max:=0;
Readln(n);
For i:=1 to n do
Read(a[i]);
For i:=1 to n do begin
If a[i]='U' then y:=y+1;
If a[i]='D' then y:=y-1;
If a[i]='L' then x:=x-1;
If a[i]='R' then x:=x+1;
If (x=0)and(y=0)then If i>max then max:=i;
end;
Writeln(max)
end.

Program z1;
Var
   x,y:real;
   i,n,k,max:integer;
   a:string;
begin
x:=0;
y:=0;
max:=0;
Readln(n);
Readln(a);
For i:=1 to n do begin
If a[i]='U' then y:=y+1;
If a[i]='D' then y:=y-1;
If a[i]='L' then x:=x-1;
If a[i]='R' then x:=x+1;
If (x=0)and(y=0)then If i>max then max:=i;
end;
Writeln(max)
end.

var s:string; l,i,x,y:integer;
begin
  readln(l); x:=0;
  readln(s); y:=0;
  for i:=1 to l do
    if (s[i]='L') then x-=1
    else if (s[i]='R') then x+=1
    else if (s[i]='U') then y+=1
    else y-=1;
  write(l-x-y)
end.

Var n,l,i,k,x,y:integer;
    s:array[1..100] of char;
Begin
Readln (n);
For i:=1 to n do
Read (s[i]);
x:=0; y:=0; l:=1;
For i:=1 to n do begin
if s[i]='r' then x:=x+1;
if s[i]='l' then x:=x-1;
if s[i]='u' then y:=y+1;
if s[i]='d' then y:=y-1;
if (x=0) and (y=0) then k:=l;
l:=l+1;
end;
writeln (k);
End.

Var n,l,i,k,x,y:integer;
    s:array[1..100] of char;
Begin
Readln (n);
For i:=1 to n do
Read (s[i]);
x:=0; y:=0; l:=0;
For i:=1 to n do begin
if s[i]='R' then x:=x+1;
if s[i]='L' then x:=x-1;
if s[i]='U' then y:=y+1;
if s[i]='D' then y:=y-1;
l:=l+1;
if (x=0) and (y=0) then k:=l;
end;
writeln (k);
End.

PROGRAM W;
var L,D,U,R,q,i,n:integer;
    s:string;
BEGIN
 readln(n);
 readln(s);
 L:=0;
 For i:=1 to n do
  if s[i]='L' then L:=L+1;
 D:=0;
 For i:=1 to n do
  if s[i]='D' then D:=D+1;
 U:=0;
 For i:=1 to n do
  if s[i]='U' then U:=U+1;
 R:=0;
 For i:=1 to n do
  if s[i]='R' then R:=R+1;
  q:=0;
 If U<>D then begin if D>U then q:=q+2*U end
               else q:=q+2*D;
 If L<>R then begin if R>L then q:=q+2*L end
               else q:=q+2*R;
 writeln(q);
END.

{$APPTYPE CONSOLE}

var i,n,k,x,y: integer;
    c: char;
begin
 readln(n);
 for i:=1 to n do
 begin
  read(c);
  case c of
  'L': dec(x);
  'R': inc(x);
  'U': inc(y);
  'D': dec(y);
  end
 end;
 k:=n-(x+y);
 write(k);
end.

{$APPTYPE CONSOLE}

var i,n,k,x,y: integer;
    c: char;
begin
 readln(n);
 for i:=1 to n do
 begin
  read(c);
  case c of
  'L': dec(x);
  'R': inc(x);
  'U': inc(y);
  'D': dec(y);
  end;
  if (x=0) and (y=0) then k:=i;
 end;
 write(k);
 readln(n);
end.

{$APPTYPE CONSOLE}

var i,n,k,x,y: integer;
    c: char;
begin
 readln(n);
 for i:=1 to n do
 begin
  read(c);
  case c of
  'L': dec(x);
  'R': inc(x);
  'U': inc(y);
  'D': dec(y);
  end
 end;
 k:=n-abs(x+y);
 write(k);
end.