N, M = gets.split.map(&:to_i)

adjacency_matrix = Array.new(N) { Array.new(N, false) }
dp = Array.new(N) { Array.new(N, nil) }

M.times do
  x, y = gets.split.map(&:to_i)
  dp[x - 1][y - 1] = 1
  adjacency_matrix[x - 1][y - 1] = true
end

max = 0

N.times do |k|
  dp_k = dp[k]

  N.times do |x|
    dp_x = dp[x]
    next if dp_x[k].nil?

    N.times do |y|
      next if dp_k[y].nil?
      tmp = dp_x[k] + dp_k[y]

      if dp_x[y].nil? || dp_x[y] < tmp
        dp_x[y] = tmp
        max = tmp
      end
    end
  end
end

puts max
