Count Connected NodesEasy

Count Connected Nodes

EasyPractice library · +10 XPgraphsbfsdfs

Given an undirected graph with N nodes and M edges, determine the size of the connected component that node 1 belongs to. The graph nodes are numbered from 1 to N.

For example, if node 1 is connected to nodes 2 and 3, and node 2 is connected to node 3, these three nodes form a connected component of size 3. If node 4 is not connected to any of these, it forms its own component of size 1.

Input

The first line contains two integers N and M, the number of nodes and edges. The next M lines each contain two integers u and v, representing an edge between node u and node v.

Output

Print a single integer: the number of nodes in the connected component containing node 1.

Constraints

1 <= N <= 1000 0 <= M <= N*(N-1)/2 1 <= u, v <= N u != v

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5 3
1 2
2 3
4 5
Output
3

Nodes 1, 2, 3 are connected. Nodes 4, 5 are connected. Node 1's component has size 3.

Example 2

Input
4 2
1 2
3 4
Output
2

Node 1 is connected to node 2. Node 3 is connected to node 4. Node 1's component has size 2.

Hints