A - A Unique Letter

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

長さ 3 の文字列 S が与えられます。
S に 1 度だけ含まれる文字を 1 つ出力してください。
但し、そのような文字が存在しない場合は代わりに -1 と出力してください。

制約

  • S は英小文字のみからなる 3 文字の文字列

入力

入力は以下の形式で標準入力から与えられる。

S

出力

答えを出力せよ。正解が複数ある場合、どれを出力してもよい。


入力例 1

pop

出力例 1

o

pop に o は 1 度だけ含まれます。


入力例 2

abc

出力例 2

a

abc に a, b, c はどれも 1 度だけ含まれるので、どれを出力しても構いません。


入力例 3

xxx

出力例 3

-1

xxx に 1 度だけ含まれる文字はありません。

Score : 100 points

Problem Statement

You are given a string S of length 3.
Print a character that occurs only once in S.
If there is no such character, print -1 instead.

Constraints

  • S is a string of length 3 consisting of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the answer. If multiple solutions exist, you may print any of them.


Sample Input 1

pop

Sample Output 1

o

o occurs only once in pop.


Sample Input 2

abc

Sample Output 2

a

a, b, and c occur once each in abc, so you may print any of them.


Sample Input 3

xxx

Sample Output 3

-1

No character occurs only once in xxx.

B - Doors in the Center

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 100 点

問題文

以下の条件を全て満たす長さ N の文字列を求めてください。

  • 各文字は - または = である
  • 回文である
  • 文字列中に = は 1 個または 2 個含まれる。 2 個含まれる場合、それらの = は隣接している

なお、そのような文字列はちょうど 1 つ存在します。

制約

  • 1 \leq N \leq 100
  • N は整数である

入力

入力は以下の形式で標準入力から与えられる。

N

出力

答えを出力せよ。


入力例 1

4

出力例 1

-==-

入力例 2

7

出力例 2

---=---

Score : 100 points

Problem Statement

Find a length-N string that satisfies all of the following conditions:

  • Each character is - or =.
  • It is a palindrome.
  • It contains exactly one or exactly two =s. If it contains two =s, they are adjacent.

Such a string is unique.

Constraints

  • 1 \leq N \leq 100
  • N is an integer.

Input

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

N

Output

Print the answer.


Sample Input 1

4

Sample Output 1

-==-

Sample Input 2

7

Sample Output 2

---=---
C - 11/11

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

AtCoder 国では、1 年が N か月からなる暦を使っています。 i 月 (1\leq i\leq N) は、i 月 1 日から i 月 D _ i 日までの D _ i 日からなります。

AtCoder 国において、1 年のうち日付がゾロ目になる日が何日あるか求めてください。

ただし、i 月 j 日 (1\leq i\leq N,1\leq j\leq D _ i) の日付がゾロ目になるとは、1 種類の数字だけを用いて i と j を十進法で表すことができることをいいます。

制約

  • 1\leq N\leq100
  • 1\leq D _ i\leq100\ (1\leq i\leq N)
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

N
D _ 1 D _ 2 \ldots D _ N

出力

答えを出力せよ。


入力例 1

12
31 29 31 30 31 30 31 31 30 31 30 31

出力例 1

13

AtCoder 国では、 1 月 1 日、1 月 11 日、2 月 2 日、2 月 22 日、3 月 3 日、4 月 4 日、5 月 5 日、6 月 6 日、7 月 7 日、8 月 8 日、9 月 9 日、11 月 1 日、11 月 11 日の合計 13 日の日付がゾロ目になります。


入力例 2

10
10 1 2 3 4 5 6 7 8 100

出力例 2

1

AtCoder 国では、1 月 1 日のみが日付がゾロ目になります。


入力例 3

30
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32

出力例 3

15

Score : 200 points

Problem Statement

AtCoder Kingdom uses a calendar whose year has N months. Month i (1\leq i\leq N) has D _ i days, from day 1 of month i to day D _ i of month i.

How many days in a year of AtCoder have "repdigits" dates?

Here, day j of month i (1\leq i\leq N,1\leq j\leq D _ i) is said to have a repdigit date if and only if all digits in the decimal notations of i and j are the same.

Constraints

  • 1\leq N\leq100
  • 1\leq D _ i\leq100\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N
D _ 1 D _ 2 \ldots D _ N

Output

Print the answer.


Sample Input 1

12
31 29 31 30 31 30 31 31 30 31 30 31

Sample Output 1

13

In AtCoder Kingdom, the days that have repdigit dates are January 1, January 11, February 2, February 22, March 3, April 4, May 5, June 6, July 7, August 8, September 9, November 1, and November 11, for a total of 13 days.


Sample Input 2

10
10 1 2 3 4 5 6 7 8 100

Sample Output 2

1

In AtCoder Kingdom, only January 1 has a repdigit date.


Sample Input 3

30
73 8 55 26 97 48 37 47 35 55 5 17 62 2 60 23 99 73 34 75 7 46 82 84 29 41 32 31 52 32

Sample Output 3

15
D - Trifecta

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 200 点

問題文

1 から N の番号がついた N 頭の馬が競争をしました。

全ての馬は同時にスタートし、 i 番の馬はスタートからゴールまで T_i 秒かかりました。

1,2,3 着の馬の番号を求めてください。なお、 T_i は相異なることが保証されます。

制約

  • 3\leq N \leq 32
  • 1\leq T_i \leq 200
  • T_i は相異なる
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

N
T_1 \dots T_N

出力

1,2,3 着の馬の番号をそれぞれ空白区切りでこの順に出力せよ。


入力例 1

4
100 110 105 95

出力例 1

4 1 3

4,1,3,2 番の順にゴールしました。1,2,3 着の番号である 4,1,3 をこの順に空白区切りで出力してください。


入力例 2

8
72 74 69 70 73 75 71 77

出力例 2

3 4 7

Score : 200 points

Problem Statement

N horses numbered 1 to N had a race.

All horses started simultaneously, and horse i took T_i seconds from the start to the goal.

Find the numbers of the horses that finished in 1st, 2nd, and 3rd places. It is guaranteed that all T_i are distinct.

Constraints

  • 3\leq N \leq 32
  • 1\leq T_i \leq 200
  • All T_i are distinct.
  • All input values are integers.

Input

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

N
T_1 \dots T_N

Output

Output the numbers of the horses that finished in 1st, 2nd, and 3rd places, in this order, separated by spaces.


Sample Input 1

4
100 110 105 95

Sample Output 1

4 1 3

The horses finished in the order 4, 1, 3, 2. Output the numbers for 1st, 2nd, and 3rd places, which are 4, 1, 3, in this order, separated by spaces.


Sample Input 2

8
72 74 69 70 73 75 71 77

Sample Output 2

3 4 7
E - Snake Queue

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

ヘビの待ち行列があります。最初、列は空です。

クエリが Q 個与えられるので、与えられた順に処理してください。クエリは以下の 3 種類です。

  • タイプ 1 : 1 l の形式で与えられる。長さ l のヘビが列の末尾に追加される。このとき追加するヘビの頭の位置は、元の列が空の場合は座標 0、そうでない場合は最後尾のヘビの頭の座標に最後尾のヘビの長さを加えた座標となる。
  • タイプ 2 : 2 の形式で与えられる。列の先頭にいるヘビが列から抜ける。このとき、列が空でないことは保証される。抜けたヘビの長さを m として、列に残っている全てのヘビの頭の座標が m だけ減少する。
  • タイプ 3 : 3 k の形式で与えられる。列の先頭から数えて k 番目にいるヘビの頭の座標を出力せよ。このとき、列には少なくとも k 匹のヘビがいることが保証される。

制約

  • 1 \leq Q \leq 3 \times 10^{5}
  • タイプ 1 のクエリにおいて、1 \leq l \leq 10^{9}
  • タイプ 2 のクエリにおいて、列が空でないことが保証される
  • タイプ 3 のクエリにおいて、列にいるヘビの数を n として、1 \leq k \leq n
  • 入力は全て整数

入力

入力は以下の形式で標準入力から与えられる。

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

ただし、\text{query}_i は i 個目のクエリを表し、以下のいずれかの形式である。

1 l
2
3 k

出力

タイプ 3 のクエリの個数を q として、q 行出力せよ。i 行目には、i 個目のタイプ 3 のクエリに対する答えを出力せよ。


入力例 1

7
1 5
1 7
3 2
1 3
1 4
2
3 3

出力例 1

5
10
  • 1 個目のクエリ : 長さ 5 のヘビが列に追加される。列にヘビはいないため、追加されたヘビの頭の座標は 0 となる。
  • 2 個目のクエリ : 長さ 7 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 0 で長さが 5 のため、追加されたヘビの頭の座標は 5 となる。
  • 3 個目のクエリ : 前から 2 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 5 であるため、5 を出力する。
  • 4 個目のクエリ : 長さ 3 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 5 で長さが 7 のため、追加されたヘビの頭の座標は 12 となる。
  • 5 個目のクエリ : 長さ 4 のヘビが列に追加される。追加する前の最後尾のヘビの頭の座標が 12 で長さが 3 のため、追加されたヘビの頭の座標は 15 となる。
  • 6 個目のクエリ : 先頭のヘビが列から抜ける。抜けたヘビの長さが 5 であるため、列にいるヘビの頭の座標は 5 だけ減少する。列に残っているヘビの頭の座標は先頭から順に 0,7,10 となる。
  • 7 個目のクエリ : 前から 3 番目にいるヘビの頭の座標を出力する。列にいるヘビの頭の座標は前から順に 0, 7, 10 であるため、10 を出力する。

入力例 2

3
1 1
2
1 3

出力例 2



タイプ 3 のクエリが 1 つもない場合もあります。


入力例 3

10
1 15
1 10
1 5
2
1 5
1 10
1 15
2
3 4
3 2

出力例 3

20
5

Score : 300 points

Problem Statement

There is a queue of snakes. Initially, the queue is empty.

You are given Q queries, which should be processed in the order they are given. There are three types of queries:

  • Type 1: Given in the form 1 l. A snake of length l is added to the end of the queue. If the queue was empty before adding, the head position of the newly added snake is 0; otherwise, it is the sum of the head coordinate of the last snake in the queue and the last snake’s length.
  • Type 2: Given in the form 2. The snake at the front of the queue leaves the queue. It is guaranteed that the queue is not empty at this time. Let m be the length of the snake that left, then the head coordinate of every snake remaining in the queue decreases by m.
  • Type 3: Given in the form 3 k. Output the head coordinate of the snake that is k-th from the front of the queue. It is guaranteed that there are at least k snakes in the queue at this time.

Constraints

  • 1 \leq Q \leq 3 \times 10^{5}
  • For a query of type 1, 1 \leq l \leq 10^{9}
  • For a query of type 2, it is guaranteed that the queue is not empty.
  • For a query of type 3, let n be the number of snakes in the queue, then 1 \leq k \leq n.
  • All input values are integers.

Input

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

Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q

Here, \text{query}_i is the i-th query in one of the following forms:

1 l
2
3 k

Output

Let q be the number of queries of type 3. Print q lines. The i-th line should contain the answer to the i-th type 3 query.


Sample Input 1

7
1 5
1 7
3 2
1 3
1 4
2
3 3

Sample Output 1

5
10
  • 1st query: A snake of length 5 is added to the queue. Since the queue was empty, the head coordinate of this snake is 0.
  • 2nd query: A snake of length 7 is added to the queue. Before adding, the last snake has head coordinate 0 and length 5, so the newly added snake’s head coordinate is 5.
  • 3rd query: Output the head coordinate of the snake that is 2nd from the front. Currently, the head coordinates of the snakes in order are 0, 5, so output 5.
  • 4th query: A snake of length 3 is added to the queue. Before adding, the last snake has head coordinate 5 and length 7, so the new snake’s head coordinate is 12.
  • 5th query: A snake of length 4 is added to the queue. Before adding, the last snake has head coordinate 12 and length 3, so the new snake’s head coordinate is 15.
  • 6th query: The snake at the front leaves the queue. The length of the snake that left is 5, so the head coordinate of each remaining snake decreases by 5. The remaining snake’s head coordinate becomes 0, 7, 10.
  • 7th query: Output the head coordinate of the snake that is 3rd from the front. Currently, the head coordinates of the snakes in order are 0, 7, 10, so output 10.

Sample Input 2

3
1 1
2
1 3

Sample Output 2



It is possible that there are no queries of type 3.


Sample Input 3

10
1 15
1 10
1 5
2
1 5
1 10
1 15
2
3 4
3 2

Sample Output 3

20
5
F - Move Segment

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 300 点

問題文

0, 1 のみからなる長さ N の文字列 S が与えられます。
S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列を出力してください。

なお、S には 1 の塊が K 個以上含まれることが保証されます。

より正確な説明は以下の通りです。

  • S の l 文字目から r 文字目までからなる部分文字列を S_{l\ldots r} と表す。
  • S の部分文字列 S_{l\ldots r} が 1 の塊であるとは、以下の条件を全て満たすことと定める。
    • S_l=S_{l+1}=\cdots=S_r= 1
    • l=1 または S_{l-1}= 0
    • r=N または S_{r+1}= 0
  • S に含まれる 1 の塊が S_{l_1\ldots r_1},\ldots,S_{l_m\ldots r_m} で全てであり、l_1 < \cdots < l_m を満たしているとする。
    このとき、以下で定義される長さ N の文字列 T を、「S の中で先頭から K 番目の 1 の塊を K-1 番目の 1 の塊の直後まで移動した文字列」と定める
    • 1 \leq i \leq r_{K-1} のとき T_i = S_i
    • r_{K-1}+1 \leq i \leq r_{K-1}+(r_K-l_K)+1 のとき T_i= 1
    • r_{K-1}+(r_K-l_K)+2 \leq i \leq r_K のとき T_i= 0
    • r_K+1 \leq i \leq N のとき T_i=S_i

制約

  • 1 \leq N \leq 5\times 10^5
  • S は 0, 1 のみからなる長さ N の文字列
  • 2 \leq K
  • S には 1 の塊が K 個以上含まれる

入力

入力は以下の形式で標準入力から与えられる。

N K
S

出力

答えを出力せよ。


入力例 1

15 3
010011100011001

出力例 1

010011111000001

S には、2 文字目から 2 文字目、5 文字目から 7 文字目、11 文字目から 12 文字目、15 文字目から 15 文字目の 4 つの 1 の塊があります。


入力例 2

10 2
1011111111

出力例 2

1111111110

Score : 300 points

Problem Statement

You are given a string S of length N consisting of 0 and 1.
Move the K-th 1-block from the beginning in S to immediately after the (K-1)-th 1-block, and print the resulting string.

It is guaranteed that S contains at least K 1-blocks.

Here is a more precise description.

  • Let S_{l\ldots r} denote the substring of S from the l-th character through the r-th character.
  • We define a substring S_{l\ldots r} of S to be a 1-block if it satisfies all of the following conditions:
    • S_l = S_{l+1} = \cdots = S_r = 1
    • l = 1 or S_{l-1} = 0
    • r = N or S_{r+1} = 0
  • Suppose that all 1-blocks in S are S_{l_1\ldots r_1}, \ldots, S_{l_m\ldots r_m}, where l_1 < l_2 < \cdots < l_m.

    Then, we define the length N string T, obtained by moving the K-th 1-block to immediately after the (K-1)-th 1-block, as follows:

    • T_i = S_i for 1 \leq i \leq r_{K-1}
    • T_i = 1 for r_{K-1} + 1 \leq i \leq r_{K-1} + (r_K - l_K) + 1
    • T_i = 0 for r_{K-1} + (r_K - l_K) + 2 \leq i \leq r_K
    • T_i = S_i for r_K + 1 \leq i \leq N

Constraints

  • 1 \leq N \leq 5 \times 10^5
  • S is a string of length N consisting of 0 and 1.
  • 2 \leq K
  • S contains at least K 1-blocks.

Input

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

N K
S

Output

Print the answer.


Sample Input 1

15 3
010011100011001

Sample Output 1

010011111000001

S has four 1-blocks: from the 2nd to the 2nd character, from the 5th to the 7th character, from the 11th to the 12th character, and from the 15th to the 15th character.


Sample Input 2

10 2
1011111111

Sample Output 2

1111111110
G - Repeatedly Repainting

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 425 点

問題文

H 行 W 列のマス目があります。このマス目の上から i 行目、左から j 列目のマスをマス (i, j) と呼びます。

すべてのマスは白または黒で塗られています。マス目の情報は H 個の長さ W の文字列 S_1, S_2, \ldots, S_H によって与えられ、S_i の j 文字目が . のときマス (i, j) は白で、S_i の j 文字目が # のときマス (i, j) は黒で塗られています。

あなたは、以下の操作を 10^{100} 回行います。

  • すべてのマスに対して同時に以下の規則で色の塗り替えを行う。
    • 操作前に白く塗られているマスは、そのマスに隣接する黒く塗られているマスが存在するとき、またそのときに限り黒く塗り替える。ただし、マス (x, y) とマス (x', y') が隣接しているとは、片方のマスがもう片方のマスの 8 近傍にある、すなわち \max(|x-x'|, |y-y'|) = 1 であることを指す。
    • 操作前に黒く塗られているマスは、白く塗り替える。

操作を終えた後に各マスが何色で塗られているか求めてください。

制約

  • 1 \leq H \times W \leq 10^6
  • H, W は正整数
  • S_i は ., # からなる長さ W の文字列

入力

入力は以下の形式で標準入力から与えられる。

H W
S_1
S_2
\vdots
S_H

出力

H 行出力せよ。

各行に ., # からなる長さ W の文字列を 1 つずつ出力せよ。

i 行目の文字列の j 文字目は 10^{100} 回の操作を行った後にマス (i, j) が白で塗られているとき . に、黒で塗られているとき # になるようにせよ。


入力例 1

3 4
#.#.
.#..
#...

出力例 1

#.#.
.#..
#..#

はじめ、マス目は以下のようになっています。

1 回操作を行うと、マス目は以下のようになります。

10^{100} 回操作を行うと、マス目は以下のようになります。


入力例 2

3 3
###
###
###

出力例 2

...
...
...

入力例 3

5 7
.#.....
.......
..#....
.......
....#..

出力例 3

.#.##.#
....#..
#.#.###
#.....#
###.#.#

Score : 425 points

Problem Statement

There is a grid with H rows and W columns. The cell at the i-th row from the top and the j-th column from the left is called cell (i, j).

Every cell is colored white or black. The grid is described by H strings S_1, S_2, \ldots, S_H, each of length W. If the j-th character of S_i is ., cell (i, j) is white; if the j-th character of S_i is #, cell (i, j) is black.

You perform the following operation 10^{100} times.

  • Simultaneously apply the following rules to all cells.
    • A cell that is white before the operation becomes black if and only if there exists at least one black cell adjacent to it. Here, cells (x, y) and (x', y') are adjacent if and only if one of them is within the 8-neighborhood of the other, that is, \max(|x-x'|, |y-y'|) = 1.
    • A cell that is black before the operation becomes white.

Find the color of each cell after the operations.

Constraints

  • 1 \leq H \times W \leq 10^6
  • H and W are positive integers.
  • S_i is a string of length W consisting of . and #.

Input

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

H W
S_1
S_2
\vdots
S_H

Output

Output H lines.

Output one string of length W consisting of . and # per line.

The j-th character of the i-th line should be . if cell (i, j) is white after 10^{100} operations, and # if it is black.


Sample Input 1

3 4
#.#.
.#..
#...

Sample Output 1

#.#.
.#..
#..#

Initially, the grid is as follows.

After one operation, the grid is as follows.

After 10^{100} operations, the grid is as follows.


Sample Input 2

3 3
###
###
###

Sample Output 2

...
...
...

Sample Input 3

5 7
.#.....
.......
..#....
.......
....#..

Sample Output 3

.#.##.#
....#..
#.#.###
#.....#
###.#.#
H - Sum of All Substrings

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 475 点

問題文

1 から 9 までの数字からなる長さ N の文字列 S が与えられます。

整数の組 (i,j) \ (1\leq i\leq j\leq N) に対して、 f(i,j) を「 S の i 文字目から j 文字目までの連続部分文字列を 10 進法の整数としてみなしたときの値」とします。\displaystyle \sum_{i=1}^N \sum_{j=i}^N f(i,j) を求めてください。

制約

  • 1\leq N\leq 2\times 10^5
  • N は整数
  • S は 1 から 9 までの数字からなる長さ N の文字列

入力

入力は以下の形式で標準入力から与えられる。

N
S

出力

答えを出力せよ。


入力例 1

3
379

出力例 1

514

答えは f(1,1)+f(1,2)+f(1,3)+f(2,2)+f(2,3)+f(3,3)=3+37+379+7+79+9=514 です。


入力例 2

30
314159265358979323846264338327

出力例 2

369673254065355789035427227741

Score : 475 points

Problem Statement

You are given a string S of length N consisting of digits from 1 through 9.

For each pair of integers (i,j) \ (1\leq i\leq j\leq N), define f(i, j) as the value obtained by interpreting the substring of S from the i-th through the j-th character as a decimal integer. Find \displaystyle \sum_{i=1}^N \sum_{j=i}^N f(i, j).

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • N is an integer.
  • S is a string of length N consisting of digits from 1 through 9.

Input

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

N
S

Output

Print the answer.


Sample Input 1

3
379

Sample Output 1

514

The answer is f(1,1) + f(1,2) + f(1,3) + f(2,2) + f(2,3) + f(3,3) = 3 + 37 + 379 + 7 + 79 + 9 = 514.


Sample Input 2

30
314159265358979323846264338327

Sample Output 2

369673254065355789035427227741
I - Tile Distance

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 550 点

問題文

座標平面上にタイルが敷き詰められています。 1\times1 の大きさの小タイルと K\times K の大きさの大タイルの 2 種類があり、次の規則に従って敷き詰められています。

  • 整数の組 (i,j) に対し、正方形 \lbrace(x,y)\mid i\leq x\leq i+1\wedge j\leq y\leq j+1\rbrace は 1 つの小タイルもしくは 1 つの大タイルに含まれる。
    • \left\lfloor\dfrac iK\right\rfloor+\left\lfloor\dfrac jK\right\rfloor が偶数のとき、小タイルに含まれる。
    • そうでないとき、大タイルに含まれる。

ただし、タイルは境界を含むものとし、共通部分が正の面積をもつような 2 つの異なるタイルは存在しないとします。

例えば、K=3 のとき、タイルは以下のようになります。

高橋君は、はじめ座標平面上の点 (S _ x+0.5,S _ y+0.5) にいます。

高橋君は、次の移動を好きなだけ繰り返します。

  • 上下左右の方向と正の整数 n を選ぶ。その方向に n だけ進む。

高橋君が異なるタイルを通るたび、高橋君は通行料を 1 だけ支払います。

高橋君が点 (T _ x+0.5,T _ y+0.5) にたどり着くために支払わなければならない通行料の最小値を求めてください。

制約

  • 1\leq K\leq10 ^ {16}
  • 0\leq S _ x\leq2\times10 ^ {16}
  • 0\leq S _ y\leq2\times10 ^ {16}
  • 0\leq T _ x\leq2\times10 ^ {16}
  • 0\leq T _ y\leq2\times10 ^ {16}
  • 入力はすべて整数

入力

入力は以下の形式で標準入力から与えられる。

K
S _ x S _ y
T _ x T _ y

出力

高橋君が支払わなければならない通行料の最小値を出力せよ。


入力例 1

3
7 2
1 6

出力例 1

5

例えば、以下のように移動することで支払う通行料を 5 にすることができます。

  • 上に 3 進む。通行料を 1 支払う。
  • 左に 2 進む。通行料を 1 支払う。
  • 上に 1 進む。通行料を 1 支払う。
  • 左に 4 進む。通行料を 2 支払う。

支払う通行料を 4 以下にすることはできないので、5 を出力してください。


入力例 2

1
41 42
13 56

出力例 2

42

高橋君が最短距離で移動するとき、どのように移動しても通行料を 42 だけ支払います。

支払う通行料を 41 以下にすることはできないので、42 を出力してください。


入力例 3

100
100 99
199 1

出力例 3

0

通行料を支払わなくてよい場合もあります。


入力例 4

96929423
5105216413055191 10822465733465225
1543712011036057 14412421458305526

出力例 4

79154049

Score: 550 points

Problem Statement

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

  • For each pair of integers (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=3, tiles are laid out as follows:

Takahashi starts at the point (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 n. Move n units in that direction.

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

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

Constraints

  • 1\leq K\leq10^{16}
  • 0\leq S_x\leq2\times10^{16}
  • 0\leq S_y\leq2\times10^{16}
  • 0\leq T_x\leq2\times10^{16}
  • 0\leq T_y\leq2\times10^{16}
  • All input values are integers.

Input

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

K
S_x S_y
T_x T_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 5.

  • Move up by 3. Pay a toll of 1.
  • Move left by 2. Pay a toll of 1.
  • Move up by 1. Pay a toll of 1.
  • Move left by 4. Pay a toll of 2.

The toll paid cannot be 4 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 42.

The toll paid cannot be 41 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