Algorithm


A. Point on Spiral
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Valera the horse lives on a plane. The Cartesian coordinate system is defined on this plane. Also an infinite spiral is painted on the plane. The spiral consists of segments: [(0, 0), (1, 0)][(1, 0), (1, 1)][(1, 1), ( - 1, 1)][( - 1, 1), ( - 1,  - 1)][( - 1,  - 1), (2,  - 1)][(2,  - 1), (2, 2)] and so on. Thus, this infinite spiral passes through each integer point of the plane.

Valera the horse lives on the plane at coordinates (0, 0). He wants to walk along the spiral to point (x, y). Valera the horse has four legs, so he finds turning very difficult. Count how many times he will have to turn if he goes along a spiral from point (0, 0) to point (x, y).

Input

The first line contains two space-separated integers x and y (|x|, |y| ≤ 100).

Output

Print a single integer, showing how many times Valera has to turn.

Examples
input
Copy
0 0
output
Copy
0
input
Copy
1 0
output
Copy
0
input
Copy
0 1
output
Copy
2
input
Copy
-1 -1
output
Copy
3



 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int x, y;

int main() {
  scanf("%d %d", &x, &y);

  int res = 0, cx = 0, cy = 0, d = 0, s = 1, c = 0;

  while(true) {
    if(c == 2) {
      ++s;
      c = 0;
    }

    for(int i = 0; i < s; ++i) {
      if(cx == x && cy == y) {
        printf("%d\n", res);
        return 0;
      }

      cx += dx[d];
      cy += dy[d];
    }

    if(cx == x && cy == y) {
      printf("%d\n", res);
      return 0;
    }

    ++res;
    ++d;
    d %= 4;
    ++c;
  }

  printf("%d\n", res);

  return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
0 0
Advertisements

Demonstration


problem link-Point on Spiral-Solution in C, C++, Java, Python

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+