using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static string InputPattern = "InputX";
static List<string> GetInputList()
{
var WillReturn = new List<string>();
if (InputPattern == "Input1") {
WillReturn.Add("5 5");
WillReturn.Add("1 2");
WillReturn.Add("2 3");
WillReturn.Add("3 4");
WillReturn.Add("4 2");
WillReturn.Add("4 5");
//4
}
else if (InputPattern == "Input2") {
WillReturn.Add("3 2");
WillReturn.Add("1 2");
WillReturn.Add("2 1");
//2
}
else {
string wkStr;
while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
}
return WillReturn;
}
static int mN;
static List<int>[] mSeiEdgeListArr; // 隣接グラフ(正方向)
static List<int>[] mRevEdgeListArr; // 隣接グラフ(逆方向)
static void Main()
{
List<string> InputList = GetInputList();
int[] wkArr = { };
Action<string> SplitAct = pStr =>
wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();
SplitAct(InputList[0]);
mN = wkArr[0];
mSeiEdgeListArr = new List<int>[mN + 1];
mRevEdgeListArr = new List<int>[mN + 1];
for (int I = 1; I <= mN; I++) {
mSeiEdgeListArr[I] = new List<int>();
mRevEdgeListArr[I] = new List<int>();
}
foreach (string EachStr in InputList.Skip(1)) {
SplitAct(EachStr);
int FromNode = wkArr[0];
int ToNode = wkArr[1];
mSeiEdgeListArr[FromNode].Add(ToNode);
mRevEdgeListArr[ToNode].Add(FromNode);
}
// 強連結成分の代表値[ノード]なDict
Dictionary<int, int> RootNodeDict = SCC_Class.Exec_SCC(1, mN, mSeiEdgeListArr, mRevEdgeListArr);
var ConnCycleNodeSet = new HashSet<int>();
// 強連結成分解の結果を使ってDFS
foreach (var EachPair in RootNodeDict) {
if (EachPair.Key == EachPair.Value) continue;
if (ConnCycleNodeSet.Contains(EachPair.Key)) continue;
HashSet<int> VisitedSet = ExecDFS3(EachPair.Value);
ConnCycleNodeSet.UnionWith(VisitedSet);
}
Console.WriteLine(ConnCycleNodeSet.Count);
}
struct JyoutaiDef
{
internal int CurrNode;
}
// 枝の向きを反転して、深さ優先探索を行う
static HashSet<int> ExecDFS3(int pStaNode)
{
var Stk = new Stack<JyoutaiDef>();
JyoutaiDef WillPush;
WillPush.CurrNode = pStaNode;
Stk.Push(WillPush);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(pStaNode);
while (Stk.Count > 0) {
JyoutaiDef Popped = Stk.Pop();
foreach (int EachNext in mRevEdgeListArr[Popped.CurrNode]) {
if (VisitedSet.Add(EachNext) == false) continue;
WillPush.CurrNode = EachNext;
Stk.Push(WillPush);
}
}
return VisitedSet;
}
}
#region SCC_Class
// 有向グラフの強連結成分分解クラス
internal static class SCC_Class
{
private static int mTimer;
private static List<int>[] mSeiEdgeListArr;
private static List<int>[] mRevEdgeListArr;
private static bool[] mVisitedArr;
private static int[] mPostNumArr;
// 最小のノードID,最大のノードID,隣接グラフ(正方向),隣接グラフ(逆方向)を引数として、
// 有向グラフを強連結成分分解し、
// 強連結成分の代表値[ノード]なDictを返す
internal static Dictionary<int, int> Exec_SCC(int pMinNodeID, int pMaxNodeID,
List<int>[] pSeiEdgeListArr, List<int>[] pRevEdgeListArr)
{
int UB = pMaxNodeID;
mSeiEdgeListArr = pSeiEdgeListArr;
mRevEdgeListArr = pRevEdgeListArr;
mVisitedArr = new bool[UB + 1];
mPostNumArr = new int[UB + 1];
// 処理01 深さ優先探索を行い、帰りがけで番号を振る
mTimer = 1;
for (int I = pMinNodeID; I <= UB; I++) {
ExecDFS1(I);
}
// ノード[帰りがけでの番号]なDict
var PostNumIndDict = new Dictionary<int, int>();
for (int I = pMinNodeID; I <= UB; I++) {
PostNumIndDict[mPostNumArr[I]] = I;
}
// 訪問済配列の初期化
mVisitedArr = new bool[UB + 1];
// 強連結成分の代表値[ノード]なDict
var RootNodeDict = new Dictionary<int, int>();
foreach (var EachPair in PostNumIndDict.OrderByDescending(pX => pX.Key)) {
if (mVisitedArr[EachPair.Value]) continue;
// 処理02 枝の向きを反転して、深さ優先探索を行う
HashSet<int> VisitedSet = ExecDFS2(EachPair.Value);
foreach (int EachNode in VisitedSet) {
RootNodeDict[EachNode] = EachPair.Value;
}
}
return RootNodeDict;
}
// 処理01 深さ優先探索を行い、帰りがけで番号を振る
private static void ExecDFS1(int pCurr)
{
if (mVisitedArr[pCurr]) return;
mVisitedArr[pCurr] = true;
foreach (int EachNext in mSeiEdgeListArr[pCurr]) {
ExecDFS1(EachNext);
}
mPostNumArr[pCurr] = mTimer++;
}
struct JyoutaiDef_SCC
{
internal int CurrNode;
}
// 処理02 枝の向きを反転して、深さ優先探索を行う
static HashSet<int> ExecDFS2(int pStaNode)
{
var Stk = new Stack<JyoutaiDef_SCC>();
JyoutaiDef_SCC WillPush;
WillPush.CurrNode = pStaNode;
Stk.Push(WillPush);
var VisitedSet = new HashSet<int>();
VisitedSet.Add(pStaNode);
mVisitedArr[pStaNode] = true;
while (Stk.Count > 0) {
JyoutaiDef_SCC Popped = Stk.Pop();
VisitedSet.Add(Popped.CurrNode);
mVisitedArr[Popped.CurrNode] = true;
foreach (int EachNext in mRevEdgeListArr[Popped.CurrNode]) {
if (mVisitedArr[EachNext]) continue;
WillPush.CurrNode = EachNext;
Stk.Push(WillPush);
}
}
return VisitedSet;
}
}
#endregion