A - 2^n - 2*n

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

整数 N が与えられるので、 2^N-2N の値を計算して出力してください。

制約

  • N は 1 以上 11 以下の整数

入力

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

N

出力

答えを出力せよ。


入力例 1

1

出力例 1

0

この入力で与えられる N の値は 1 なので、 2^1 - 2 \times 1 の値である 0 を出力してください。


入力例 2

2

出力例 2

0

この入力で与えられる N の値は 2 なので、 2^2 - 2 \times 2 の値である 0 を出力してください。


入力例 3

11

出力例 3

2026

この入力で与えられる N の値は 11 なので、 2^{11} - 2 \times 11 の値である 2026 を出力してください。

Score : 100 points

Problem Statement

You are given an integer N. Compute and output the value of 2^N-2N.

Constraints

  • N is an integer between 1 and 11, inclusive.

Input

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

N

Output

Output the answer.


Sample Input 1

1

Sample Output 1

0

The value of N given in this input is 1, so output the value of 2^1 - 2 \times 1, which is 0.


Sample Input 2

2

Sample Output 2

0

The value of N given in this input is 2, so output the value of 2^2 - 2 \times 2, which is 0.


Sample Input 3

11

Sample Output 3

2026

The value of N given in this input is 11, so output the value of 2^{11} - 2 \times 11, which is 2026.

B - Move Right

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 100 点

問題文

横一列に 4 つのマスが並んでいます。

各文字が 0 または 1 である長さ 4 の文字列 S が与えられます。
S の i 文字目が 1 であるとき、左から i 番目のマスには 1 人の人がおり、
S の i 文字目が 0 であるとき、左から i 番目のマスには人がいません。

全ての人が一斉に、1 つ右隣のマスへ移動します。この移動により、もともと右端のマスにいた人は消えます。

移動後の各マスに人がいるかどうかを、S と同様のルールで文字列として出力してください。

制約

  • S は 0, 1 のみからなる長さ 4 の文字列

入力

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

S

出力

長さ 4 の文字列であって、移動後、左から i 番目のマスに人がいるならば i 文字目が 1、いないならば 0 であるようなものを出力せよ。


入力例 1

1011

出力例 1

0101

移動により、左から 1 番目のマスにいた人は左から 2 番目のマスに、
左から 3 番目のマスにいた人は左から 4 番目のマスに移動し、
左から 4 番目のマス(右端のマス)にいた人は消えます。


入力例 2

0000

出力例 2

0000

入力例 3

1111

出力例 3

0111

Score : 100 points

Problem Statement

There are 4 squares lined up horizontally.

You are given a string S of length 4 consisting of 0 and 1.
If the i-th character of S is 1, there is a person in the i-th square from the left;
if the i-th character of S is 0, there is no person in the i-th square from the left.

Now, everyone will move to the next square to the right simultaneously. By this move, the person who was originally in the rightmost square will disappear.

Determine if there will be a person in each square after the move. Print the result as a string in the same format as S. (See also Sample Input / Output for clarity.)

Constraints

  • S is a string of length 4 consisting of 0 and 1.

Input

Input is given from Standard Input in the following format:

S

Output

Print a string of length 4 such that the i-th character is 1 if there will be a person in the i-th square from the left after the move, and 0 otherwise.


Sample Input 1

1011

Sample Output 1

0101

After the move, the person who was originally in the 1-st square will move to the 2-nd square,
the person in the 3-rd square to the 4-th square,
and the person in the 4-th square will disappear.


Sample Input 2

0000

Sample Output 2

0000

Sample Input 3

1111

Sample Output 3

0111
C - Pentagon

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 200 点

問題文

以下の図で表される正五角形 P があります。

P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しいか判定してください。

制約

  • S_1,S_2,T_1,T_2 は A, B, C, D, E のいずれかの文字
  • S_1 \neq S_2
  • T_1 \neq T_2

入力

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

S_1S_2
T_1T_2

出力

P の点 S_1 と点 S_2 を結ぶ線分と、点 T_1 と点 T_2 を結ぶ線分の長さが等しい場合 Yes を、等しくない場合 No を出力せよ。


入力例 1

AC
EC

出力例 1

Yes

P の点 A と点 C を結ぶ線分と、P の点 E と点 C を結ぶ線分の長さは等しいです。


入力例 2

DA
EA

出力例 2

No

P の点 D と点 A を結ぶ線分と、P の点 E と点 A を結ぶ線分の長さは等しくありません。


入力例 3

BD
BD

出力例 3

Yes

Score : 200 points

Problem Statement

A regular pentagon P is shown in the figure below.

Determine whether the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2.

Constraints

  • Each of S_1, S_2, T_1, and T_2 is one of the characters A, B, C, D, and E.
  • S_1 \neq S_2
  • T_1 \neq T_2

Input

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

S_1S_2
T_1T_2

Output

If the length of the line segment connecting points S_1 and S_2 of P equals the length of the line segment connecting points T_1 and T_2, print Yes; otherwise, print No.


Sample Input 1

AC
EC

Sample Output 1

Yes

The length of the line segment connecting point A and point C of P equals the length of the line segment connecting point E and point C.


Sample Input 2

DA
EA

Sample Output 2

No

The length of the line segment connecting point D and point A of P does not equal the length of the line segment connecting point E and point A.


Sample Input 3

BD
BD

Sample Output 3

Yes
D - ARC Division

実行時間制限: 2 sec / メモリ制限: 1024 MiB



配点 : 200 点

問題文

AtCoder では、ARC が 2 つの division に分けられています。

  • ARC Div. 1 では、コンテスト開始時のレーティングが 1600 以上 2799 以下の参加者がレーティング更新の対象です。
  • ARC Div. 2 では、コンテスト開始時のレーティングが 1200 以上 2399 以下の参加者がレーティング更新の対象です。

高橋くんは、これから N 回の ARC に参加することにしました。

はじめ、高橋くんのレーティングは R です。

i 回目 (1\leq i\leq N) の ARC は Div. D _ i で、高橋くんが取った成績は整数 A _ i で表されます。

i 回目の ARC において高橋くんがレーティング更新の対象ならば、コンテスト開始時の高橋くんのレーティングを T として、更新後の高橋くんのレーティングは T+A _ i になります。 高橋くんがレーティング更新の対象でなければ、高橋くんのレーティングは変化しません。

ARC でのレーティングの更新はコンテストが終了したあと直ちに行われ、次のコンテストのレーティング更新の対象であるかは更新後のレーティングをもとに判定されます。

N 回の ARC を終えたとき、高橋くんのレーティングがいくつになっているか求めてください。

ただし、高橋くんはこの N 回の ARC 以外のコンテストには参加せず、ARC 以外でレーティングが変動することはありません。

制約

  • 1\leq N\leq 100
  • 0\leq R\leq 4229
  • 1\leq D _ i\leq 2\ (1\leq i\leq N)
  • -1000\leq A _ i\leq 1000\ (1\leq i\leq N)
  • 入力はすべて整数

入力

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

N R
D _ 1 A _ 1
D _ 2 A _ 2
\vdots
D _ N A _ N

出力

N 回の ARC を終えた後の、高橋くんのレーティングを出力せよ。


入力例 1

4 1255
2 900
1 521
2 600
1 52

出力例 1

2728

はじめ、高橋くんのレーティングは 1255 です。

それぞれのコンテストで、高橋くんのレーティングは次のように変化します。

  • 1 回目の ARC は Div. 2 です。高橋くんはレーティング更新の対象なので、高橋くんのレーティングは 1255+900=2155 になります。
  • 2 回目の ARC は Div. 1 です。高橋くんはレーティング更新の対象なので、高橋くんのレーティングは 2155+521=2676 になります。
  • 3 回目の ARC は Div. 2 です。高橋くんはレーティング更新の対象ではないので、高橋くんのレーティングは変化しません。
  • 4 回目の ARC は Div. 1 です。高橋くんはレーティング更新の対象なので、高橋くんのレーティングは 2676+52=2728 になります。

4 回の ARC を終えた後、高橋くんのレーティングは 2728 となっているので、2728 を出力してください。


入力例 2

2 3031
1 1000
2 -1000

出力例 2

3031

高橋くんはレッドコーダーなので、ARC でどのような成績を取ってもレーティングは変動しません。


入力例 3

15 2352
2 -889
2 420
2 -275
1 957
1 -411
1 -363
1 151
2 -193
2 289
2 -770
2 109
1 345
2 551
1 -702
1 355

出力例 3

1226

Score : 200 points

Problem Statement

AtCoder Regular Contest (ARC) is divided into two divisions.

  • In ARC Div. 1, participants whose rating at the start of the contest is between 1600 and 2799, inclusive, are subject to rating updates.
  • In ARC Div. 2, participants whose rating at the start of the contest is between 1200 and 2399, inclusive, are subject to rating updates.

Takahashi decided to participate in N ARCs.

Initially, his rating is R.

The i-th (1\leq i\leq N) ARC is Div. D _ i, and his performance in that contest is represented by an integer A _ i.

If he is subject to a rating update in the i-th ARC, let T be his rating at the start of that contest. Then, after the contest, his rating becomes T+A _ i.

If his is not subject to a rating update, his rating does not change.

Rating updates for ARCs are performed immediately after the contest ends, and whether he is subject to rating updates in the next contest is determined based on his rating after the update.

Find his rating after finishing the N ARCs.

He does not participate in any contests other than these N ARCs, and his rating does not change in other ways.

Constraints

  • 1\leq N\leq 100
  • 0\leq R\leq 4229
  • 1\leq D _ i\leq 2\ (1\leq i\leq N)
  • -1000\leq A _ i\leq 1000\ (1\leq i\leq N)
  • All input values are integers.

Input

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

N R
D _ 1 A _ 1
D _ 2 A _ 2
\vdots
D _ N A _ N

Output

Print Takahashi's rating after finishing the N ARCs.


Sample Input 1

4 1255
2 900
1 521
2 600
1 52

Sample Output 1

2728

Initially, Takahashi's rating is 1255.

For each contest, Takahashi's rating changes as follows:

  • The 1st ARC is Div. 2. He is subject to rating updates, so his rating becomes 1255+900=2155.
  • The 2nd ARC is Div. 1. He is subject to rating updates, so his rating becomes 2155+521=2676.
  • The 3rd ARC is Div. 2. He is not subject to rating updates, so his rating does not change.
  • The 4th ARC is Div. 1. He is subject to rating updates, so his rating becomes 2676+52=2728.

After the four ARCs, his rating is 2728, so print 2728.


Sample Input 2

2 3031
1 1000
2 -1000

Sample Output 2

3031

He is a Red coder, so his rating does not change upon his performance in ARC.


Sample Input 3

15 2352
2 -889
2 420
2 -275
1 957
1 -411
1 -363
1 151
2 -193
2 289
2 -770
2 109
1 345
2 551
1 -702
1 355

Sample Output 3

1226
E - Σ

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 250 点

問題文

長さ N の正整数列 A=(A_1,A_2,\dots,A_N) および正整数 K が与えられます。

1 以上 K 以下の整数のうち、A の中に一度も現れないものの総和を求めてください。

制約

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq 2\times 10^9
  • 1\leq A_i \leq 2\times 10^9
  • 入力は全て整数

入力

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

N K
A_1 A_2 \dots A_N

出力

答えを出力せよ。


入力例 1

4 5
1 6 3 1

出力例 1

11

1 以上 5 以下の整数のうち、A の中に一度も現れないものは 2,4,5 の 3 つです。

よって、それらの総和である 2+4+5=11 を出力します。


入力例 2

1 3
346

出力例 2

6

入力例 3

10 158260522
877914575 24979445 623690081 262703497 24979445 1822804784 1430302156 1161735902 923078537 1189330739

出力例 3

12523196466007058

Score: 250 points

Problem Statement

You are given a sequence of positive integers A=(A_1,A_2,\dots,A_N) of length N and a positive integer K.

Find the sum of the integers between 1 and K, inclusive, that do not appear in the sequence A.

Constraints

  • 1\leq N \leq 2\times 10^5
  • 1\leq K \leq 2\times 10^9
  • 1\leq A_i \leq 2\times 10^9
  • All input values are integers.

Input

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

N K
A_1 A_2 \dots A_N

Output

Print the answer.


Sample Input 1

4 5
1 6 3 1

Sample Output 1

11

Among the integers between 1 and 5, three numbers, 2, 4, and 5, do not appear in A.

Thus, print their sum: 2+4+5=11.


Sample Input 2

1 3
346

Sample Output 2

6

Sample Input 3

10 158260522
877914575 24979445 623690081 262703497 24979445 1822804784 1430302156 1161735902 923078537 1189330739

Sample Output 3

12523196466007058
F - Socks

実行時間制限: 4 sec / メモリ制限: 1024 MiB

配点 : 300 点

問題文

N 枚の靴下があります。i 枚目の靴下の色は A_i です。

あなたは以下の操作をできるだけ多い回数行いたいです。最大で何回行うことができますか?

  • まだペアになっていない靴下の中から同じ色の靴下を 2 枚選んでペアにする。

制約

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq 10^9
  • 入力は全て整数

入力

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

N
A_1 A_2 \dots A_N

出力

答えを整数として出力せよ。


入力例 1

6
4 1 7 4 1 4

出力例 1

2

以下のようにして、2 回の操作を行うことができます。

  • 色が 1 である靴下を 2 枚選んでペアにする。
  • 色が 4 である靴下を 2 枚選んでペアにする。

このとき、色が 4 である靴下と 7 である靴下が 1 枚ずつ残るため、これ以上操作はできません。 また、どのように操作をしても 3 回以上操作を行うことはできないため、2 を出力します。


入力例 2

1
158260522

出力例 2

0

入力例 3

10
295 2 29 295 29 2 29 295 2 29

出力例 3

4

Score : 300 points

Problem Statement

You have N socks. The color of the i-th sock is A_i.

You want to perform the following operation as many times as possible. How many times can it be performed at most?

  • Choose two same-colored socks that are not paired yet, and pair them.

Constraints

  • 1\leq N \leq 5\times 10^5
  • 1\leq A_i \leq 10^9
  • All values in the input are integers.

Input

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

N
A_1 A_2 \dots A_N

Output

Print an integer representing the answer.


Sample Input 1

6
4 1 7 4 1 4

Sample Output 1

2

You can do the operation twice as follows.

  • Choose two socks with the color 1 and pair them.
  • Choose two socks with the color 4 and pair them.

Then, you will be left with one sock with the color 4 and another with the color 7, so you can no longer do the operation. There is no way to do the operation three or more times, so you should print 2.


Sample Input 2

1
158260522

Sample Output 2

0

Sample Input 3

10
295 2 29 295 29 2 29 295 2 29

Sample Output 3

4
G - Many Repunit Sum

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 400 点

問題文

i=1,2,\dots,N に対して、1 を A_i 個つなげた整数を B_i と表します。
より厳密には、B_i=\sum_{j=0}^{A_i-1}{10^j} と表します。
\sum_{i=1}^{N}{B_i} を求めてください。

制約

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • 入力される値は全て整数

入力

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

N  
A_1 A_2 \ldots A_N  

出力

答えを 1 行で出力せよ。


入力例 1

4
3 3 3 3

出力例 1

444

B_1=B_2=B_3=B_4=111 なので、B_1+B_2+B_3+B_4=444 です。


入力例 2

3
30 10 20

出力例 2

111111111122222222223333333333

答えは非常に大きくなる可能性があります。


入力例 3

10
1 2 3 4 5 6 7 8 9 10

出力例 3

1234567900

Score : 400 points

Problem Statement

For i=1,2,\dots,N, let B_i denote the integer formed by concatenating A_i ones.
More formally, B_i=\sum_{j=0}^{A_i-1}{10^j}.
Find \sum_{i=1}^{N}{B_i}.

Constraints

  • 1 \leq N \leq 2 \times 10^5
  • 1 \leq A_i \leq 2 \times 10^5
  • All input values are integers.

Input

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

N  
A_1 A_2 \ldots A_N  

Output

Output the answer in one line.


Sample Input 1

4
3 3 3 3

Sample Output 1

444

B_1=B_2=B_3=B_4=111, so B_1+B_2+B_3+B_4=444.


Sample Input 2

3
30 10 20

Sample Output 2

111111111122222222223333333333

The answer may be very large.


Sample Input 3

10
1 2 3 4 5 6 7 8 9 10

Sample Output 3

1234567900
H - Defect-free Squares

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 475 点

問題文

縦 H 行, 横 W 列のグリッドがあります。グリッドの上から i 行目, 左から j 列目のマスを (i, j) と呼びます。
グリッドの各マスは穴の空いたマスとそうでないマスのどちらかです。穴が空いたマスは (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N) のちょうど N マスです。

正整数の組 (i, j, n) が次の条件を満たすとき、(i, j) を左上隅, (i + n - 1, j + n - 1) を右下隅とする正方形領域を 穴のない正方形 と呼びます。

  • i + n - 1 \leq H
  • j + n - 1 \leq W
  • 0 \leq k \leq n - 1, 0 \leq l \leq n - 1 を満たす全ての非負整数の組 (k, l) に対して、(i + k, j + l) は穴が空いていないマスである。

グリッド内に穴のない正方形は何個ありますか?

制約

  • 1 \leq H, W \leq 3000
  • 0 \leq N \leq \min(H \times W, 10^5)
  • 1 \leq a_i \leq H
  • 1 \leq b_i \leq W
  • (a_i, b_i) は互いに異なる
  • 入力される値は全て整数

入力

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

H W N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

出力

穴のない正方形の個数を出力せよ。


入力例 1

2 3 1
2 3

出力例 1

6

穴のない正方形は全部で 6 個あります。
それらを列挙すると次の通りです。このうちはじめの 5 個は n = 1 の場合であり、領域の左上隅のマスと右下隅のマスが一致します。

  • (1, 1) を左上隅かつ右下隅とする正方形領域
  • (1, 2) を左上隅かつ右下隅とする正方形領域
  • (1, 3) を左上隅かつ右下隅とする正方形領域
  • (2, 1) を左上隅かつ右下隅とする正方形領域
  • (2, 2) を左上隅かつ右下隅とする正方形領域
  • (1, 1) を左上隅, (2, 2) を右下隅とする正方形領域

入力例 2

3 2 6
1 1
1 2
2 1
2 2
3 1
3 2

出力例 2

0

穴のない正方形が存在しない場合もあります。


入力例 3

1 1 0

出力例 3

1

穴のない正方形がグリッド全体と一致する場合もあります。


入力例 4

3000 3000 0

出力例 4

9004500500

Score : 475 points

Problem Statement

There is a grid with H rows and W columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left of the grid.
Each square of the grid is holed or not. There are exactly N holed squares: (a_1, b_1), (a_2, b_2), \dots, (a_N, b_N).

When the triple of positive integers (i, j, n) satisfies the following condition, the square region whose top-left corner is (i, j) and whose bottom-right corner is (i + n - 1, j + n - 1) is called a holeless square.

  • i + n - 1 \leq H.
  • j + n - 1 \leq W.
  • For every pair of non-negative integers (k, l) such that 0 \leq k \leq n - 1, 0 \leq l \leq n - 1, square (i + k, j + l) is not holed.

How many holeless squares are in the grid?

Constraints

  • 1 \leq H, W \leq 3000
  • 0 \leq N \leq \min(H \times W, 10^5)
  • 1 \leq a_i \leq H
  • 1 \leq b_i \leq W
  • All (a_i, b_i) are pairwise different.
  • All input values are integers.

Input

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

H W N
a_1 b_1
a_2 b_2
\vdots
a_N b_N

Output

Print the number of holeless squares.


Sample Input 1

2 3 1
2 3

Sample Output 1

6

There are six holeless squares, listed below. For the first five, n = 1, and the top-left and bottom-right corners are the same square.

  • The square region whose top-left and bottom-right corners are (1, 1).
  • The square region whose top-left and bottom-right corners are (1, 2).
  • The square region whose top-left and bottom-right corners are (1, 3).
  • The square region whose top-left and bottom-right corners are (2, 1).
  • The square region whose top-left and bottom-right corners are (2, 2).
  • The square region whose top-left corner is (1, 1) and whose bottom-right corner is (2, 2).

Sample Input 2

3 2 6
1 1
1 2
2 1
2 2
3 1
3 2

Sample Output 2

0

There may be no holeless square.


Sample Input 3

1 1 0

Sample Output 3

1

The whole grid may be a holeless square.


Sample Input 4

3000 3000 0

Sample Output 4

9004500500
I - Skate

実行時間制限: 2 sec / メモリ制限: 1024 MiB

配点 : 500 点

問題文

H 行 W 列のグリッド型のスケート場があります。上から i 行目、左から j 行目のマスを (i,j) で表します。

スケート場には N 個の障害物があり、i 個目の障害物は (X_i,Y_i) に置かれています。

高橋君は 1 回の移動において、上下左右いずれかの方向を選んで、障害物に当たるまで進み続けます。
障害物に当たるとその 1 つ手前のマスで停止します。 なお、スケート場の周りは崖になっており、障害物に当たらないような移動は禁止とします。

高橋君ははじめ (s_x,s_y) にいて、何回か移動することで (g_x,g_y) で停止したいと考えています。

(g_x,g_y) へ辿り着くには最小で何回の移動が必要ですか?辿り着けないときはその旨を伝えてください。

制約

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • i\neq j ならば、(X_i,Y_i)\neq (X_j,Y_j)
  • 入力は全て整数である

入力

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

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

出力

(g_x,g_y) へ辿り着くには最小で何回の移動が必要か出力せよ。
ただし、辿り着けないならば -1 と出力せよ。


入力例 1

7 8 7
3 4
5 6
1 4
2 1
2 8
4 5
5 7
6 2
6 6

出力例 1

4

図は、(s_x,s_y) を S で、(g_x,g_y) を G で表しています。
(3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6) と移動すると、4 回の移動で (g_x,g_y) に辿り着くことができます。


入力例 2

4 6 2
3 2
3 5
4 5
2 5

出力例 2

-1

(g_x,g_y) で停止する必要があります。
通過しただけでは (g_x,g_y) へ辿り着いたとみなされないことに注意してください。


入力例 3

1 10 1
1 5
1 1
1 7

出力例 3

-1


左を選んで進むと、高橋君は (g_x,g_y) を通過したのちに崖に落ちてしまいます。
スケート場の周りは崖になっており、障害物に当たらないような移動は禁止されていることに注意してください。

Score : 500 points

Problem Statement

There is a skating rink represented by a grid with H horizontal rows and W vertical columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.

The skating rink has N obstacles. The i-th obstacle is placed at (X_i,Y_i).

In a single move, Takahashi chooses one of the four directions, up, down, left, or right, and keeps moving until he hits an obstacle.
When he hits an obstacle, he stops at the square right before the obstacle. Since the skating rink is surrounded by cliffs, it is prohibited to start a move in which he will never hit an obstacle.

Takahashi is initially at (s_x,s_y). He wants to make some number of moves to stop at (g_x,g_y).

Find the minimum number of moves required to end up at (g_x, g_y). If it is not possible, report the fact.

Constraints

  • 1\leq H \leq 10^9
  • 1\leq W \leq 10^9
  • 1\leq N \leq 10^5
  • 1\leq s_x,g_x\leq H
  • 1\leq s_y,g_y\leq W
  • 1\leq X_i \leq H
  • 1\leq Y_i \leq W
  • (s_x,s_y)\neq (g_x,g_y)
  • (s_x,s_y)\neq (X_i,Y_i)
  • (g_x,g_y)\neq (X_i,Y_i)
  • If i\neq j, then (X_i,Y_i)\neq (X_j,Y_j).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W N
s_x s_y
g_x g_y
X_1 Y_1
X_2 Y_2
\vdots
X_N Y_N

Output

Print the minimum number of moves required to end up at (g_x,g_y).
If it is impossible to end up there, print -1.


Sample Input 1

7 8 7
3 4
5 6
1 4
2 1
2 8
4 5
5 7
6 2
6 6

Sample Output 1

4

In the figure, (s_x,s_y) is denoted by S and (g_x,g_y) is denoted by G.
By moving as (3,4)\rightarrow(2,4) \rightarrow(2,2) \rightarrow(5,2) \rightarrow(5,6), he can end up at (g_x,g_y) with 4 moves.


Sample Input 2

4 6 2
3 2
3 5
4 5
2 5

Sample Output 2

-1

He must stop at (g_x,g_y).
Note that just passing through (g_x,g_y) is not considered to be ending up at the goal.


Sample Input 3

1 10 1
1 5
1 1
1 7

Sample Output 3

-1


If he chooses to move to the left, Takahashi will fall down the cliff after passing through (g_x,g_y).
Note that it is prohibited to start a move in which he will never hit an obstacle, as the skating rink is surrounded by cliffs.