Problem:
Score : 100 points

Problem Statement

Takahashi is drawing a segment on grid paper.

From a certain square, a square that is x squares to the right and y squares above, is denoted as square (x, y).

When Takahashi draws a segment connecting the lower left corner of square (A, B) and the lower left corner of square (C, D), find the number of the squares crossed by the segment.

Here, the segment is said to cross a square if the segment has non-empty intersection with the region within the square, excluding the boundary.

Constraints

1 \leq A, B, C, D \leq 10^9

At least one of A \neq C and B \neq D holds.

Input

The input is given from Standard Input in the following format:

A B C D

Output

Print the number of the squares crossed by the segment.

Sample Input 1

1 1 3 4

Sample Output 1

4

Sample Input 2

2 3 10 7

Sample Output 2

8

Solution in C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>

#define ABS(a) ((a)>(0)?(a):-(a))

int gcd(int a,int b){
  int r=a%b;
  while(r>0){
    a=b;b=r;r=a%b;
  }
  return b;
}

void run(void){
  int a,b,c,d;
  scanf("%d%d%d%d",&a,&b,&c,&d);
  int dx=ABS(a-c);
  int dy=ABS(b-d);
  if(dx==0 || dy==0){
    puts("0");
    return;
  }
  int g=gcd(dx,dy);
  printf("%d\n",(dx/g+dy/g-1)*g);
}

int main(void){
  run();
  return 0;
}