±1 Operation 1

AtCoder
IDabc255_c
Time2000ms
Memory256MB
Difficulty
You are given an integer $X$. The following action on this integer is called an _operation_. * Choose and do one of the following. * Add $1$ to $X$. * Subtract $1$ from $X$. The terms in the arithmetic progression $S$ with $N$ terms whose initial term is $A$ and whose common difference is $D$ are called _good numbers_. Consider performing zero or more operations to make $X$ a good number. Find the minimum number of operations required to do so. ## Constraints * All values in input are integers. * $-10^{18} \le X,A \le 10^{18}$ * $-10^6 \le D \le 10^6$ * $1 \le N \le 10^{12}$ ## Input Input is given from Standard Input in the following format: $X$ $A$ $D$ $N$ [samples]
Samples
Input #1
6 2 3 3
Output #1
1

Since $A=2,D=3,N=3$, we have $S=(2,5,8)$.  
You can subtract $1$ from $X$ once to make $X=6$ a good number.  
It is impossible to make $X$ good in zero operations.
Input #2
0 0 0 1
Output #2
0

We might have $D=0$. Additionally, no operation might be required.
Input #3
998244353 -10 -20 30
Output #3
998244363
Input #4
\-555555555555555555 -1000000000000000000 1000000 1000000000000
Output #4
444445
API Response (JSON)
{
  "problem": {
    "name": "±1 Operation 1",
    "description": {
      "content": "You are given an integer $X$. The following action on this integer is called an _operation_. *   Choose and do one of the following.     *   Add $1$ to $X$.     *   Subtract $1$ from $X$. The terms ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc255_c"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given an integer $X$. The following action on this integer is called an _operation_.\n\n*   Choose and do one of the following.\n    *   Add $1$ to $X$.\n    *   Subtract $1$ from $X$.\n\nThe terms ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments