PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000046. Tính bắc cầu (Transitivity)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tính bắc cầu (Transitivity)

Transitivity

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Add as few directed edges as necessary so that for all distinct vertices a,b,ca,b,c, edges a→ba\to b and b→cb\to c imply the edge a→ca\to c.

Input

Integers N,MN,M, followed by MM directed edges ui,viu_i,v_i.

Output

Print the minimum number of edges to add.

Subtasks

  • Subtask 1 (40 points): N≤20N\le20; 3≤N≤20003\le N\le2000, 0≤M≤20000\le M\le2000; distinct directed non-loop edges.
  • Subtask 2 (60 points): 3≤N≤20003\le N\le2000, 0≤M≤20000\le M\le2000; distinct directed non-loop edges.

Examples

Input

4 3
2 4
3 1
4 3

Output

3

Explanation

The initial edges imply new reachability links 2 to 3, 4 to 1, and 2 to 1. All three must be added, producing the minimum answer 3.