This page is still under construction.

Parts of this page are still being built. What you see may change.

Cosmic Settlement

Interview

Time limit1sMemory limit512 MB

Summary
Given n rectangles of size a by b and a w by h field, find the largest integer d such that n rectangles of size (a+2d) by (b+2d) fit in a grid on the field, in either orientation.
Level

Medium6 of 10

Topics
Binary search, Math, Implementation
Solved
No attempts yet

Problem

To colonize Mars, an research base must be built. The base consists of nn identical modules, each of which is a rectangle.

Each module is a habitation compartment shaped like an a×ba \times b meter rectangle. To increase the reliability of the modules, engineers can add a layer of additional shielding around each module. The thickness of this layer must be a whole number of meters, and all modules must have the same shielding thickness. A module with shielding of thickness dd meters has the shape of an (a+2d)×(b+2d)(a + 2d) \times (b + 2d) meter rectangle.

All modules must be placed on a pre-prepared rectangular field of size w×hw \times h meters. They must be organized as a regular grid: their sides must be parallel to the sides of the field, and the modules must all be oriented the same way.

Write a program that, given the number and size of the modules and the size of the field where they are placed, determines the maximum thickness of the additional shielding layer that can be added to each module.

Input

The input file contains five space-separated integers: nn, aa, bb, ww, and hh (1≤n,a,b,w,h≤10181 \le n, a, b, w, h \le 10^{18}). It is guaranteed that without additional shielding, all modules can be placed in the settlement as described.

Output

The output file must contain a single integer: the maximum possible thickness of the additional shielding. If no additional shielding can be installed, output the number 00.

Notes

In the first example, shielding of thickness 22 meters can be installed and the modules can be placed on the field as shown in the figure.

Habitation moduleHabitation module with additional shielding of thickness 2Placement of the modules on the field

In the second example, the habitation compartment has size 5×55 \times 5 meters, and the field has size 6×66 \times 6 meters. Additional shielding cannot be added to the module.

Examples2

  1. Example 1

    Input
    11 2 3 21 25
    
    Expected output
    2
    
  2. Example 2

    Input
    1 5 5 6 6
    
    Expected output
    0