import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.List;
import
java.util.Vector;
public
class
Main {
static
Vector<Integer>[] adjList =
new
Vector[
200005
];
static
int
n, m, weight[] =
new
int
[
200005
], a, b,
visited[] =
new
int
[
200005
], currentPath[] =
new
int
[
200005
];
static
long
k;
static
boolean
dfs(
int
node,
int
x) {
if
(visited[node] !=
0
)
return
false
;
currentPath[node] =
1
;
int
result =
0
;
for
(
int
neighbor : adjList[node]) {
if
(weight[neighbor] <= x) {
if
(currentPath[neighbor] !=
0
)
return
true
;
if
(dfs(neighbor, x))
return
true
;
result = Math.max(result, visited[neighbor]);
}
}
currentPath[node] =
0
;
visited[node] = result +
1
;
return
false
;
}
static
boolean
isPathPossible(
int
x) {
for
(
int
i =
1
; i <= n; i++) {
if
(visited[i] ==
0
&& weight[i] <= x) {
if
(dfs(i, x))
return
true
;
if
(visited[i] >= k)
return
true
;
}
}
return
false
;
}
public
static
void
main(String[] args) {
n =
6
;
m =
7
;
k =
4
;
int
weights[] = {
1
,
10
,
2
,
3
,
4
,
5
};
for
(
int
i =
1
; i <= n; i++)
weight[i] = weights[i -
1
];
int
edges[][] = {{
1
,
2
}, {
1
,
3
}, {
3
,
4
}, {
4
,
5
}, {
5
,
6
}, {
6
,
2
}, {
2
,
5
}};
for
(
int
i =
0
; i <
200005
; i++) {
adjList[i] =
new
Vector<>();
}
for
(
int
i =
0
; i < m; i++) {
a = edges[i][
0
];
b = edges[i][
1
];
adjList[a].add(b);
}
int
left =
0
, right = (
int
) 1e9, mid, answer = -
1
;
while
(left <= right) {
mid = left + (right - left) /
2
;
for
(
int
i =
1
; i <= n; i++) {
visited[i] = currentPath[i] =
0
;
}
if
(isPathPossible(mid)) {
right = mid -
1
;
answer = mid;
}
else
{
left = mid +
1
;
}
}
System.out.println(answer);
}
}