#P6348. [PA 2011] Journeys

[PA 2011] Journeys

Description

On a planet, there are nn countries and many bidirectional roads. The countries are numbered from 11 to nn.

However, there are too many roads to describe in the usual way. So we describe the roads as follows: (a,b),(c,d)(a,b),(c,d) means that for any two countries x,yx,y, if axba \le x \le b and cydc \le y \le d, then there is a road between xx and yy.

The capital is located in country PP. You want to know the minimum number of roads that must be taken to travel from country PP to any country. It is guaranteed that country PP can reach every country.

Input Format

The first line contains three integers n,m,Pn,m,P.

Then follow mm lines, each containing four integers a,b,c,da,b,c,d.

Output Format

Output nn lines. The ii-th line should contain the minimum number of roads that must be taken to travel from country PP to country ii.

5 3 4
1 2 4 5
5 5 4 4
1 1 3 3
1
1
2
0
1

Hint

For all testdata, it is guaranteed that 1n5×1051 \le n \le 5 \times 10^5, 1m1051 \le m \le 10^5, 1abn1 \le a \le b \le n, 1cdn1 \le c \le d \le n.

Translated by ChatGPT 5