let input = readLine()!.split(separator: " ").map { Int($0)! }

let N = input[0]
let M = input[1]

var graph: [[Int]] = .init(repeating: [], count: N)

for _ in 0..<M {
  let inp = readLine()!.split(separator: " ").map { Int($0)! }
  let start = inp[0]
  let end = inp[1]
  
  graph[start - 1].append(end - 1)
}

var dp: [Int] = .init(repeating: -1, count: N)

func dfs(_ i: Int) -> Int {
  if dp[i] != -1 {
    return dp[i]
  }
  var count = 0
  
  for nei in graph[i] {
    count = max(count, dfs(nei) + 1)
  }
  dp[i] = count
  return dp[i]
}
var ans = 0
for i in 0..<N {
  ans = max(ans, dfs(i))
}
print(ans)