using
System;
using
System.Collections.Generic;
class
GFG
{
static
void
minimumColors(
int
N,
int
E,
int
[]U,
int
[]V)
{
List<
int
> []adj =
new
List<
int
>[N];
int
[]count =
new
int
[N];
int
[]colors =
new
int
[N];
for
(
int
i = 0; i < N; i++) {
adj[i] =
new
List<
int
>();
colors[i] = 1;
}
for
(
int
i = 0; i < N; i++) {
adj[V[i] - 1].Add(U[i] - 1);
count[U[i] - 1]++;
}
List<
int
> Q =
new
List<
int
>();
for
(
int
i = 0; i < N; i++) {
if
(count[i] == 0) {
Q.Add(i);
}
}
while
(Q.Count!=0) {
int
u = Q[0];
Q.RemoveAt(0);
foreach
(
int
x
in
adj[u]) {
count[x]--;
if
(count[x] == 0) {
Q.Add(x);
}
if
(colors[x] <= colors[u]) {
colors[x] = 1 + colors[u];
}
}
}
int
minColor = -1;
for
(
int
i = 0; i < N; i++) {
minColor = Math.Max(minColor, colors[i]);
}
Console.Write(minColor +
"\n"
);
}
public
static
void
Main(String[] args)
{
int
N = 5, E = 6;
int
[]U = { 1, 2, 3, 1, 2, 3 };
int
[]V = { 3, 3, 4, 4, 5, 5 };
minimumColors(N, E, U, V);
}
}