$t=<>; ($x,$a)=glob<>; ($y,$b)=glob<>; if($t>=0){ $q=int$t/$a; $c+=$q*$x; $t-=$q*$a; }else{ $q=int+(1-$t)/$b+1; $c+=$q*$y; $t+=$q*$b; } $d=$c+$t; while(1){ $t+=$y; $c+=$b; $q=int$t/$a; $c+=$q*$x; $t-=$q*$a; if($c>$d){ last; } if($d>$c+$t){ $d=$c+$t; } } print$d;