End of the Spiral

Time limit2sMemory limit128 MB

Summary
Given an N by M grid, simulate a spiral walk that starts east from the southwest corner and turns left whenever blocked, then output the final cell's coordinates.
Level

Medium5 of 10

Topics
Simulation, Math, Implementation
Solved
No attempts yet

Problem

Sejun lives in a palace whose floor has N columns and M rows. He wants visitors to walk as much as possible before reaching him, so a spiral path is installed.

Visitors enter at the southwestern corner. They first keep moving east. Whenever the cell in front of them is outside the palace or has already been visited, they turn left and continue moving forward. The diagram below shows the visiting order for a palace with N = 6 and M = 4; letters are visited in alphabetical order.

nmlkji
oxwvuh
pqrstg
abcdef

Sejun wants to stay at the cell where this spiral walk ends. Write a program that prints the coordinate of that cell.

The southwestern corner is (0, 0), the southeastern corner is (N-1, 0), and the northeastern corner is (N-1, M-1).

Input

The first line contains two natural numbers N and M. Each is at most 5000.

Output

Print the coordinate x y of the cell where the spiral walk ends.

Examples5

  1. Example 1

    Input
    6 4
    
    Expected output
    1 2
    
  2. Example 2

    Input
    6 5
    
    Expected output
    3 2
    
  3. Example 3

    Input
    1 11
    
    Expected output
    0 10
    
  4. Example 4

    Input
    12 50
    
    Expected output
    5 6
    
  5. Example 5

    Input
    5000 5000
    
    Expected output
    2499 2500