#abc353f. F - Tile Distance

F - Tile Distance

Score: 550550 points

问题陈述

瓷砖铺设在坐标平面上。有两种类型的瓷砖:大小为 1×11\times1 的小瓷砖和大小为 K×KK\times K 的大瓷砖,根据以下规则铺设:

  • 对于每一对整数 (i,j)(i,j),正方形 $\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace$ 包含在一个小型瓷砖或一个大型瓷砖内。
    • 如果 $\left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor$ 是偶数,则它包含在一个小瓷砖内。
    • 否则,它包含在一个大瓷砖内。

瓷砖包括它们的边界,并且没有两个不同的瓷砖有正面积的交集。

例如,当 K=3K=3 时,瓷砖的布局如下:

高桥从坐标平面上的点 (Sx+0.5,Sy+0.5)(S_x+0.5,S_y+0.5) 开始。

他可以重复以下移动任意次数:

  • 选择一个方向(上、下、左或右)和一个正整数 nn。在那个方向上移动 nn 个单位。

每次他从一个瓷砖跨越到另一个瓷砖时,他必须支付 1 元的通行费。

确定高桥到达点 (Tx+0.5,Ty+0.5)(T_x+0.5,T_y+0.5) 必须支付的最小通行费。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

Tiles are laid out on a coordinate plane. There are two types of tiles: small tiles of size 1×11\times1 and large tiles of size K×KK\times K, laid out according to the following rules:

  • For each pair of integers (i,j)(i,j), the square $\lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace$ is contained within either one small tile or one large tile.
    • If $\left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor$ is even, it is contained within a small tile.
    • Otherwise, it is contained within a large tile.

Tiles include their boundaries, and no two different tiles have a positive area of intersection.

For example, when K=3K=3, tiles are laid out as follows:

Takahashi starts at the point (Sx+0.5,Sy+0.5)(S_x+0.5,S_y+0.5) on the coordinate plane.

He can repeat the following movement any number of times:

  • Choose a direction (up, down, left, or right) and a positive integer nn. Move nn units in that direction.

Each time he crosses from one tile to another, he must pay a toll of 11.

Determine the minimum toll Takahashi must pay to reach the point (Tx+0.5,Ty+0.5)(T_x+0.5,T_y+0.5).

Constraints

  • 1K10161\leq K\leq10^{16}
  • 0Sx2×10160\leq S_x\leq2\times10^{16}
  • 0Sy2×10160\leq S_y\leq2\times10^{16}
  • 0Tx2×10160\leq T_x\leq2\times10^{16}
  • 0Ty2×10160\leq T_y\leq2\times10^{16}
  • All input values are integers.

Input

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

KK

SxS_x SyS_y

TxT_x TyT_y

Output

Print the minimum toll Takahashi must pay.

Sample Input 1

3
7 2
1 6

Sample Output 1

5

For example, he can move as follows, paying a toll of 55.

  • Move up by 33. Pay a toll of 11.
  • Move left by 22. Pay a toll of 11.
  • Move up by 11. Pay a toll of 11.
  • Move left by 44. Pay a toll of 22.

The toll paid cannot be 44 or less, so print 5.

Sample Input 2

1
41 42
13 56

Sample Output 2

42

When he moves the shortest distance, he will always pay a toll of 4242.

The toll paid cannot be 4141 or less, so print 42.

Sample Input 3

100
100 99
199 1

Sample Output 3

0

There are cases where no toll needs to be paid.

Sample Input 4

96929423
5105216413055191 10822465733465225
1543712011036057 14412421458305526

Sample Output 4

79154049