実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
正整数 N,L,R が与えられます。
長さ N の数列 A=(1,2,\dots,N) に対し、 L 項目から R 項目までを逆順に並べ替えるという操作を一度行いました。
操作後の数列を出力してください。
制約
- 入力は全て整数
- 1 \le L \le R \le N \le 100
入力
入力は以下の形式で標準入力から与えられる。
N L R
出力
操作後の数列を A'=(A'_1,A'_2,\dots,A'_N) として、以下の形式で出力せよ。
A'_1 A'_2 \dots A'_N
入力例 1
5 2 3
出力例 1
1 3 2 4 5
最初、 A=(1,2,3,4,5) です。
2 項目から 3 項目までを逆順に並べ替えた後の数列は (1,3,2,4,5) なので、これを出力してください。
入力例 2
7 1 1
出力例 2
1 2 3 4 5 6 7
L=R である場合もあります。
入力例 3
10 1 10
出力例 3
10 9 8 7 6 5 4 3 2 1
L=1 や R=N である場合もあります。
Score : 100 points
Problem Statement
You are given positive integers N, L, and R.
For a sequence A = (1, 2, \dots, N) of length N, an operation of reversing the L-th through R-th elements was performed once.
Print the sequence after this operation.
Constraints
- All input values are integers.
- 1 \leq L \leq R \leq N \leq 100
Input
The input is given from Standard Input in the following format:
N L R
Output
Let A' = (A'_1, A'_2, \dots, A'_N) be the sequence after the operation. Print it in the following format:
A'_1 A'_2 \dots A'_N
Sample Input 1
5 2 3
Sample Output 1
1 3 2 4 5
Initially, A = (1, 2, 3, 4, 5).
After reversing the second through third elements, the sequence becomes (1, 3, 2, 4, 5), which should be printed.
Sample Input 2
7 1 1
Sample Output 2
1 2 3 4 5 6 7
It is possible that L = R.
Sample Input 3
10 1 10
Sample Output 3
10 9 8 7 6 5 4 3 2 1
It is possible that L = 1 or R = N.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 100 点
問題文
文字列 S が与えられるので、この文字列が Hello,World! と完全に一致するなら AC 、そうでないなら WA と出力してください。
「完全に一致する」とは?
文字列 A と B が完全に一致するとは、文字列 A と B の長さが等しく、かつ全ての 1 \le i \le |A| を満たす整数 i について A の先頭から i 文字目と B の先頭から i 文字目とが(英大文字か小文字かも含めて)一致することを指します。制約
- 1 \le |S| \le 15
- S は英大小文字,
,,!のみからなる
入力
入力は以下の形式で標準入力から与えられる。
S
出力
答えを出力せよ。
入力例 1
Hello,World!
出力例 1
AC
文字列 S は Hello,World! と完全に一致します。
入力例 2
Hello,world!
出力例 2
WA
先頭から 7 文字目の W が、 Hello,World! では大文字ですが S では小文字です。よって S は Hello,World! と一致しません。
入力例 3
Hello!World!
出力例 3
WA
Score : 100 points
Problem Statement
Given a string S, print AC if it perfectly matches Hello,World!; otherwise, print WA.
What is a perfect match?
Strings A is said to perfectly match B when the length of A is equal to that of B, and the i-th character of A is the same as the i-th character of B for every integer i such that 1 \le i \le |A|.Constraints
- 1 \le |S| \le 15
- S consists of English lowercase letters, English uppercase letters,
,, and!.
Input
Input is given from Standard Input in the following format:
S
Output
Print the answer.
Sample Input 1
Hello,World!
Sample Output 1
AC
The string S perfectly matches Hello,World!.
Sample Input 2
Hello,world!
Sample Output 2
WA
The seventh character from the beginning should be an uppercase W in Hello,World!, but S has a lowercase w in that position. Thus, S does not match Hello,World!.
Sample Input 3
Hello!World!
Sample Output 3
WA
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
-10^{18} 以上 10^{18} 以下の整数 X が与えられるので、\left\lceil \dfrac{X}{10} \right\rceil を出力してください。
ここで、\left\lceil a \right\rceil は a 以上の整数のうち最小のものを意味します。
制約
- -10^{18} \leq X \leq 10^{18}
- X は整数
入力
入力は以下の形式で標準入力から与えられる。
X
出力
\left\lceil \dfrac{X}{10} \right\rceil を整数として出力せよ。
入力例 1
27
出力例 1
3
\frac{27}{10} = 2.7 以上の整数は 3, 4, 5, \dots です。この中で一番小さい整数は 3 なので、\left \lceil \frac{27}{10} \right \rceil = 3 となります。
入力例 2
-13
出力例 2
-1
\frac{-13}{10} = -1.3 以上の整数は、全ての正整数および 0, -1 です。この中で一番小さい整数は -1 なので、\left \lceil \frac{-13}{10} \right \rceil = -1 となります。
入力例 3
40
出力例 3
4
\frac{40}{10} = 4 以上の整数で一番小さい整数は 4 自身です。
入力例 4
-20
出力例 4
-2
入力例 5
123456789123456789
出力例 5
12345678912345679
Score: 200 points
Problem Statement
Given an integer X between -10^{18} and 10^{18}, inclusive, print \left\lceil \dfrac{X}{10} \right\rceil.
Here, \left\lceil a \right\rceil denotes the smallest integer not less than a.
Constraints
- -10^{18} \leq X \leq 10^{18}
- X is an integer.
Input
The input is given from Standard Input in the following format:
X
Output
Print \left\lceil \dfrac{X}{10} \right\rceil as an integer.
Sample Input 1
27
Sample Output 1
3
The integers not less than \frac{27}{10} = 2.7 are 3, 4, 5, \dots. Among these, the smallest is 3, so \left \lceil \frac{27}{10} \right \rceil = 3.
Sample Input 2
-13
Sample Output 2
-1
The integers not less than \frac{-13}{10} = -1.3 are all positive integers, 0, and -1. Among these, the smallest is -1, so \left \lceil \frac{-13}{10} \right \rceil = -1.
Sample Input 3
40
Sample Output 3
4
The smallest integer not less than \frac{40}{10} = 4 is 4 itself.
Sample Input 4
-20
Sample Output 4
-2
Sample Input 5
123456789123456789
Sample Output 5
12345678912345679
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 200 点
問題文
高橋君は英小文字からなる文字列 S を持っています。
高橋君は文字列 S に対して、下記の操作をちょうど 1 回行います。
- まず、非負整数 K を選ぶ。
- その後、S の各文字を K 個後ろの英小文字に変更する。
ただし、
aの 1 個後ろの英小文字はbであり、bの 1 個後ろの英小文字はcであり、cの 1 個後ろの英小文字はdであり、- \cdots
yの 1 個後ろの英小文字はzであり、zの 1 個後ろの英小文字はaです。
例えば、b の 4 個後ろの英小文字は f であり、y の 3 個後ろの英小文字は b です。
文字列 T が与えられます。 高橋君が上記の操作によって S を T に一致させることができるかを判定してください。
制約
- S と T はそれぞれ英小文字からなる長さ 1 以上 10^5 以下の文字列
- S の長さと T の長さは等しい
入力
入力は以下の形式で標準入力から与えられる。
S T
出力
高橋君が S を T に一致させることができる場合は Yes と出力し、
できない場合は No と出力せよ。
入力例 1
abc ijk
出力例 1
Yes
高橋君が K=8 を選ぶと、
aは 8 個後ろのiにbは 8 個後ろのjにcは 8 個後ろのkに
それぞれ変更され、S と T が一致します。
高橋君が S を T に一致させることができるため Yes と出力します。
入力例 2
z a
出力例 2
Yes
高橋君が K=1 を選ぶと S と T が一致します。
z の 1 個後ろの英小文字は a であることに注意してください。
入力例 3
ppq qqp
出力例 3
No
高橋君は非負整数 K をどのように選んでも S を T に一致させることができません。
よって、No と出力します。
入力例 4
atcoder atcoder
出力例 4
Yes
高橋君が K=0 を選ぶと S と T が一致します。
Score : 200 points
Problem Statement
Takahashi has a string S consisting of lowercase English letters.
On this string, he will do the operation below just once.
- First, choose a non-negative integer K.
- Then, shift each character of S to the right by K (see below).
Here,
ashifted to the right by 1 isb;bshifted to the right by 1 isc;cshifted to the right by 1 isd;- \cdots
yshifted to the right by 1 isz;zshifted to the right by 1 isa.
For example, b shifted to the right by 4 is f, and y shifted to the right by 3 is b.
You are given a string T. Determine whether Takahashi can make S equal T by the operation above.
Constraints
- Each of S and T is a string of length between 1 and 10^5 (inclusive) consisting of lowercase English letters.
- The lengths of S and T are equal.
Input
Input is given from Standard Input in the following format:
S T
Output
If Takahashi can make S equal T, print Yes; if not, print No.
Sample Input 1
abc ijk
Sample Output 1
Yes
When Takahashi chooses K=8,
ais shifted to the right by 8 and becomesi,bis shifted to the right by 8 and becomesj,cis shifted to the right by 8 and becomesk,
and now S and T are equal.
Therefore, he can make S equal T, so Yes should be printed.
Sample Input 2
z a
Sample Output 2
Yes
Choosing K=1 makes S and T equal.
Note that the letter on the right of z is a.
Sample Input 3
ppq qqp
Sample Output 3
No
There is no non-negative integer K that he can choose to make S equal T, so No should be printed.
Sample Input 4
atcoder atcoder
Sample Output 4
Yes
Choosing K=0 makes S and T equal.
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
1 以上 N 以下の整数からなる長さ N の数列 a = (a_1, \dots, a_N) が与えられます。
以下の条件を全て満たす整数 i, j の組の総数を求めてください。
- 1 \leq i \lt j \leq N
- \min(a_i, a_j) = i
- \max(a_i, a_j) = j
制約
- 2 \leq N \leq 5 \times 10^5
- 1 \leq a_i \leq N \, (1 \leq i \leq N)
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N a_1 \ldots a_N
出力
答えを出力せよ。
入力例 1
4 1 3 2 4
出力例 1
2
(i, j) = (1, 4), (2, 3) が条件を満たします。
入力例 2
10 5 8 2 2 1 6 7 2 9 10
出力例 2
8
Score : 300 points
Problem Statement
You are given a sequence a = (a_1, \dots, a_N) of length N consisting of integers between 1 and N.
Find the number of pairs of integers i, j that satisfy all of the following conditions:
- 1 \leq i \lt j \leq N
- \min(a_i, a_j) = i
- \max(a_i, a_j) = j
Constraints
- 2 \leq N \leq 5 \times 10^5
- 1 \leq a_i \leq N \, (1 \leq i \leq N)
- All values in input are integers.
Input
Input is given from Standard Input in the following format:
N a_1 \ldots a_N
Output
Print the answer.
Sample Input 1
4 1 3 2 4
Sample Output 1
2
(i, j) = (1, 4), (2, 3) satisfy the conditions.
Sample Input 2
10 5 8 2 2 1 6 7 2 9 10
Sample Output 2
8
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 300 点
問題文
10^9 行 10^9 列のグリッドがあり、このグリッドの上から i 番目、左から j 番目のマスをマス (i, j) と表記します。
グリッド上には N 人の人がおり、はじめ i 人目の人はマス (R_i, C_i) にいます。
はじめ時刻は 0 であり、各人は、時刻 1, 2, 3, 4, \ldots に以下のような移動をすることができます。
- その場に留まるか、8 近傍のマスに移動する。ただし、グリッドの外側に出ることはできない。厳密には、現在いるマスをマス (i, j) としてマス (i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j), (i, j + 1), (i + 1, j - 1), (i + 1, j), (i + 1, j + 1) のうち存在するマスのいずれかに移動する。また、移動には時間がかからないものとする。
N 人の人が全員同じマスに集まる時刻として考えられる最小値を求めてください。
制約
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_i, C_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N R_1 C_1 R_2 C_2 \vdots R_N C_N
出力
答えを出力せよ。
入力例 1
3 2 3 5 1 8 1
出力例 1
3
以下のようにそれぞれの人が移動することによって、全員が時刻 3 にマス (5, 4) に集まります。
-
時刻 1 では、1 人目の人はマス (3, 4)、2 人目の人はマス (6, 2)、3 人目の人はマス (7, 2) に移動する。
-
時刻 2 では、1 人目の人はマス (4, 4)、2 人目の人はマス (5, 3)、3 人目の人はマス (6, 3) に移動する。
-
時刻 3 では、1 人目の人はマス (5, 4)、2 人目の人はマス (5, 4)、3 人目の人はマス (5, 4) に移動する。
入力例 2
5 6 7 6 7 6 7 6 7 6 7
出力例 2
0
はじめからすべての人は同じマスにいます。
入力例 3
6 91 999999986 53 999999997 32 999999932 14 999999909 49 999999985 28 999999926
出力例 3
44
Score : 300 points
Problem Statement
There is a grid with 10^9 rows and 10^9 columns. Let (i, j) denote the square at the i-th row from the top and j-th column from the left.
There are N people on the grid. Initially, the i-th person is at square (R_i, C_i).
The time starts at 0. Each person can do the following move at times 1, 2, 3, 4, \ldots.
- Stay at the current position, or move to an 8-adjacent square. It is forbidden to leave the grid. Formally, let square (i, j) be the current square, and move to one of the squares (i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j - 1), (i, j), (i, j + 1), (i + 1, j - 1), (i + 1, j), (i + 1, j + 1) that exists. Assume that the move takes no time.
Find the minimum possible time when the N people are at the same square.
Constraints
- 1 \leq N \leq 2 \times 10^5
- 1 \leq R_i, C_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N R_1 C_1 R_2 C_2 \vdots R_N C_N
Output
Output the answer.
Sample Input 1
3 2 3 5 1 8 1
Sample Output 1
3
All people will be at square (5, 4) at time 3 if each person moves as follows.
-
At time 1, the 1st person moves to square (3, 4), the 2nd person moves to square (6, 2), and the 3rd person moves to square (7, 2).
-
At time 2, the 1st person moves to square (4, 4), the 2nd person moves to square (5, 3), and the 3rd person moves to square (6, 3).
-
At time 3, the 1st person moves to square (5, 4), the 2nd person moves to square (5, 4), and the 3rd person moves to square (5, 4).
Sample Input 2
5 6 7 6 7 6 7 6 7 6 7
Sample Output 2
0
All people start at the same square.
Sample Input 3
6 91 999999986 53 999999997 32 999999932 14 999999909 49 999999985 28 999999926
Sample Output 3
44
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
(1,2,\dots,N) を並び替えて得られる数列 P=(P_1,P_2,\dots,P_N) が与えられます。
長さ K の正整数列 (i_1,i_2,\dots,i_K) であって、以下の条件を共に満たすものを良い添字列と呼びます。
- 1\leq i_1 < i_2 < \dots < i_K \leq N
- (P_{i_1},P_{i_2},\dots,P_{i_K}) はある連続する K 個の整数を並び替えることで得られる。
厳密には、ある整数 a が存在して、\lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace。
全ての良い添字列における i_K-i_1 の最小値を求めてください。 なお、本問題の制約下では良い添字列が必ず 1 つ以上存在することが示せます。
制約
- 1\leq K \leq N \leq 2\times 10^5
- 1\leq P_i\leq N
- i\neq j ならば P_i\neq P_j
- 入力は全て整数
入力
入力は以下の形式で標準入力から与えられる。
N K P_1 P_2 \dots P_N
出力
全ての良い添字列における i_K-i_1 の最小値を出力せよ。
入力例 1
4 2 2 3 1 4
出力例 1
1
良い添字列は (1,2),(1,3),(2,4) の 3 つです。 例えば (i_1,i_2)=(1,3) は、 1\leq i_1 < i_2 \leq N かつ (P_{i_1},P_{i_2})=(2,1) が連続する 2 つの整数 1,2 の並び替えなので良い添字列です。
これらの良い添字列のうち i_K-i_1 の値が最小となるのは (1,2) で、そのときの値は 2-1=1 です。
入力例 2
4 1 2 3 1 4
出力例 2
0
どの良い添字列においても i_K-i_1=i_1-i_1=0 です。
入力例 3
10 5 10 1 6 8 7 2 5 9 3 4
出力例 3
5
Score: 425 points
Problem Statement
You are given a permutation P = (P_1, P_2, \dots, P_N) of (1, 2, \dots, N).
A length-K sequence of indices (i_1, i_2, \dots, i_K) is called a good index sequence if it satisfies both of the following conditions:
- 1 \leq i_1 < i_2 < \dots < i_K \leq N.
- The subsequence (P_{i_1}, P_{i_2}, \dots, P_{i_K}) can be obtained by rearranging some consecutive K integers.
Formally, there exists an integer a such that \lbrace P_{i_1},P_{i_2},\dots,P_{i_K} \rbrace = \lbrace a,a+1,\dots,a+K-1 \rbrace.
Find the minimum value of i_K - i_1 among all good index sequences. It can be shown that at least one good index sequence exists under the constraints of this problem.
Constraints
- 1 \leq K \leq N \leq 2 \times 10^5
- 1 \leq P_i \leq N
- P_i \neq P_j if i \neq j.
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N K P_1 P_2 \dots P_N
Output
Print the minimum value of i_K - i_1 among all good index sequences.
Sample Input 1
4 2 2 3 1 4
Sample Output 1
1
The good index sequences are (1,2),(1,3),(2,4). For example, (i_1, i_2) = (1,3) is a good index sequence because 1 \leq i_1 < i_2 \leq N and (P_{i_1}, P_{i_2}) = (2,1) is a rearrangement of two consecutive integers 1, 2.
Among these good index sequences, the smallest value of i_K - i_1 is for (1,2), which is 2-1=1.
Sample Input 2
4 1 2 3 1 4
Sample Output 2
0
i_K - i_1 = i_1 - i_1 = 0 in all good index sequences.
Sample Input 3
10 5 10 1 6 8 7 2 5 9 3 4
Sample Output 3
5
実行時間制限: 2 sec / メモリ制限: 1024 MiB
配点 : 425 点
問題文
正整数 N に対し、N が 400 number であることは以下の 2 つの条件をともに満たすことであると定義します。
- N の素因数はちょうど 2 種類である。
- N の各素因数 p について、N が p で割り切れる回数は偶数回である。より厳密には、N の各素因数 p について p^k が N の約数であるような最大の非負整数 k は偶数である。
Q 個のクエリが与えられるので、それぞれについて答えてください。各クエリでは、整数 A が与えられるので、A 以下の最大の 400 number の値を求めてください。ただし、本問題の制約下では A 以下の 400 number は常に存在します。
制約
- 1 \leq Q \leq 2 \times 10^5
- 各クエリについて、36 \leq A \leq 10^{12}
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
ここで、\text{query}_i は i 番目のクエリであり、以下の形式で与えられる。
A
出力
Q 行出力せよ。i 行目には i 番目のクエリに対する答えを出力せよ。
入力例 1
5 404 36 60 1000000000000 123456789
出力例 1
400 36 36 1000000000000 123454321
1 番目のクエリについて説明します。
400 の素因数は 2, 5 のちょうど 2 種類であり、400 が 2 で割り切れる回数は 4 回、5 で割り切れる回数は 2 回であるため 400 は 400 number です。401, 402, 403, 404 は 400 number ではないため答えは 400 となります。
Score : 425 points
Problem Statement
A positive integer N is a 400 number if and only if it satisfies both of the following two conditions:
- N has exactly 2 distinct prime factors.
- For each prime factor p of N, p divides N an even number of times. More formally, the maximum non-negative integer k such that p^k divides N is even.
Process Q queries. Each query gives you an integer A, so find the largest 400 number not exceeding A. Under the constraints of this problem, a 400 number not exceeding A always exists.
Constraints
- 1 \leq Q \leq 2 \times 10^5
- For each query, 36 \leq A \leq 10^{12}.
- 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, given in the following format:
A
Output
Print Q lines. The i-th line should contain the answer to the i-th query.
Sample Input 1
5 404 36 60 1000000000000 123456789
Sample Output 1
400 36 36 1000000000000 123454321
Let us explain the first query.
There are exactly 2 prime factors of 400: 2 and 5. Also, 2 divides 400 four times and 5 divides it twice, so 400 is a 400 number. None of 401, 402, 403, and 404 is a 400 number, so the answer is 400.
実行時間制限: 3 sec / メモリ制限: 1024 MiB
配点 : 500 点
問題文
黒板に 1 以上 M 以下の整数からなる集合 N 個 S_1,S_2,\dots,S_N が書かれています。ここで、S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace です。
あなたは、以下の操作を好きな回数(0 回でもよい)行うことが出来ます。
- 1 個以上の共通した要素を持つ 2 個の集合 X,Y を選ぶ。X,Y の 2 個を黒板から消し、新たに X\cup Y を黒板に書く。
ここで、X\cup Y とは X か Y の少なくともどちらかに含まれている要素のみからなる集合を意味します。
1 と M が両方含まれる集合を作ることが出来るか判定してください。出来るならば、必要な操作回数の最小値を求めてください。
制約
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- 入力は全て整数である。
入力
入力は以下の形式で標準入力から与えられる。
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
出力
1 と M が両方含まれる集合を作ることが出来るならば必要な操作回数の最小値を、出来ないならば -1 を出力せよ。
入力例 1
3 5 2 1 2 2 2 3 3 3 4 5
出力例 1
2
まず、\lbrace 1,2 \rbrace と \lbrace 2,3 \rbrace を選んで消し、\lbrace 1,2,3 \rbrace を追加します。
そして、\lbrace 1,2,3 \rbrace と \lbrace 3,4,5 \rbrace を選んで消し、\lbrace 1,2,3,4,5 \rbrace を追加します。
すると 2 回の操作で 1 と M を両方含む集合を作ることが出来ます。1 回の操作では目標を達成できないため、答えは 2 です。
入力例 2
1 2 2 1 2
出力例 2
0
始めから S_1 が 1,M を共に含むため、必要な操作回数の最小値は 0 回です。
入力例 3
3 5 2 1 3 2 2 4 3 2 4 5
出力例 3
-1
入力例 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
出力例 4
2
Score : 500 points
Problem Statement
On a blackboard, there are N sets S_1,S_2,\dots,S_N consisting of integers between 1 and M. Here, S_i = \lbrace S_{i,1},S_{i,2},\dots,S_{i,A_i} \rbrace.
You may perform the following operation any number of times (possibly zero):
- choose two sets X and Y with at least one common element. Erase them from the blackboard, and write X\cup Y on the blackboard instead.
Here, X\cup Y denotes the set consisting of the elements contained in at least one of X and Y.
Determine if one can obtain a set containing both 1 and M. If it is possible, find the minimum number of operations required to obtain it.
Constraints
- 1 \le N \le 2 \times 10^5
- 2 \le M \le 2 \times 10^5
- 1 \le \sum_{i=1}^{N} A_i \le 5 \times 10^5
- 1 \le S_{i,j} \le M(1 \le i \le N,1 \le j \le A_i)
- S_{i,j} \neq S_{i,k}(1 \le j < k \le A_i)
- All values in the input are integers.
Input
The input is given from Standard Input in the following format:
N M
A_1
S_{1,1} S_{1,2} \dots S_{1,A_1}
A_2
S_{2,1} S_{2,2} \dots S_{2,A_2}
\vdots
A_N
S_{N,1} S_{N,2} \dots S_{N,A_N}
Output
If one can obtain a set containing both 1 and M, print the minimum number of operations required to obtain it; if it is impossible, print -1 instead.
Sample Input 1
3 5 2 1 2 2 2 3 3 3 4 5
Sample Output 1
2
First, choose and remove \lbrace 1,2 \rbrace and \lbrace 2,3 \rbrace to obtain \lbrace 1,2,3 \rbrace.
Then, choose and remove \lbrace 1,2,3 \rbrace and \lbrace 3,4,5 \rbrace to obtain \lbrace 1,2,3,4,5 \rbrace.
Thus, one can obtain a set containing both 1 and M with two operations. Since one cannot achieve the objective by performing the operation only once, the answer is 2.
Sample Input 2
1 2 2 1 2
Sample Output 2
0
S_1 already contains both 1 and M, so the minimum number of operations required is 0.
Sample Input 3
3 5 2 1 3 2 2 4 3 2 4 5
Sample Output 3
-1
Sample Input 4
4 8 3 1 3 5 2 1 2 3 2 4 7 4 4 6 7 8
Sample Output 4
2