Segment on Grid Paper

AtCoder
IDrelay_e
Time2000ms
Memory256MB
Difficulty
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$ [samples]
Samples
Input #1
1 1 3 4
Output #1
4
Input #2
2 3 10 7
Output #2
8
API Response (JSON)
{
  "problem": {
    "name": "Segment on Grid Paper",
    "description": {
      "content": "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 conn",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "relay_e"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Takahashi is drawing a segment on grid paper.\nFrom a certain square, a square that is $x$ squares to the right and $y$ squares above, is denoted as square $(x, y)$.\nWhen Takahashi draws a segment conn...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments