This page is still under construction.

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

Escape from a Rectangle

Interview

Time limit2sMemory limit128 MB

Summary
Given a point inside an axis-aligned rectangle, compute the minimum distance from the point to the nearest side of the rectangle.
Level

Easy1 of 10

Topics
Math, Geometry, Implementation
Solved
No attempts yet

Problem

Hansoo is at the point (x, y) on the coordinate plane. Each side of the rectangle is parallel to a coordinate axis, with lower-left corner (0, 0) and upper-right corner (w, h). Find the minimum distance Hansoo must move to reach the boundary of the rectangle.

Input

The first line contains four integers x, y, w, and h.

Output

Print the minimum distance from the point to the boundary of the rectangle.

Constraints

  • 1 <= w, h <= 1,000
  • 1 <= x <= w - 1
  • 1 <= y <= h - 1
  • x, y, w, and h are integers.

Examples4

  1. Example 1

    Input
    6 2 10 3
    
    Expected output
    1
    
  2. Example 2

    Input
    1 1 5 5
    
    Expected output
    1
    
  3. Example 3

    Input
    653 375 1000 1000
    
    Expected output
    347
    
  4. Example 4

    Input
    161 181 762 375
    
    Expected output
    161