#P10265. Maze Connectivity Sum

    ID: 12264 Type: Default 1000ms 256MiB

Maze Connectivity Sum

Maze Connectivity Sum

In a mysterious fantasy continent, there exist nn ancient and magical mazes numbered from 11 to nn. Some pairs of mazes have bidirectional connections, while others have one-way paths. A player wants to challenge different mazes by starting from maze mm. Your task is to help him determine the total count by calculating (1) the number of mazes that can directly reach maze mm, and (2) the number of mazes that maze mm can directly reach; then output the sum of these two counts.

Note that for any maze ii (1in1 \leq i \leq n), it is always considered to directly reach itself.

inputFormat

The input begins with a line containing two integers nn and mm, where nn is the total number of mazes and mm is the starting maze. The second line contains an integer kk, representing the number of direct connections (excluding the implicit self-connection). Each of the following kk lines contains two integers aa and bb, indicating there is a direct connection from maze aa to maze bb.

outputFormat

Output a single integer: the sum of the number of mazes that can directly reach maze mm and the number of mazes that maze mm can directly reach (each maze always directly reaches itself).

sample

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