using System;
using System.Collections.Generic;
using System.Linq;
using BitArray = System.Collections.BitArray;
using BigInteger = System.Numerics.BigInteger;

namespace AtCoderProject
{
    public class Program
    {
        public object Calc()
        {
            var line = consoleReader.Split.Int;
            var N = line[0];
            var M = line[1];
            int[][] graph;
            {
                var builder = new DirectedGraphBuilder(N);
                foreach (var item in consoleReader.Repeat(M).SplitInt)
                    builder.Add(item[0] - 1, item[1] - 1);
                graph = builder.ToArray();
            }

            var res = Enumerable.Repeat(-1, N).ToArray();
            for (int i = 0; i < res.Length; i++)
            {
                Calc(res, graph, i);
            }
            return res.Max();
        }
        static void Calc(int[] res, int[][] graph, int index)
        {
            if (res[index] >= 0) return;
            if (graph[index].Length == 0)
            {
                res[index] = 0;
            }
            else
            {
                int max = 0;
                foreach (var item in graph[index])
                {
                    Calc(res, graph, item);
                    if (res[item] > max) max = res[item];
                }
                res[index] = max + 1;
            }
        }
        class DirectedGraphBuilder
        {
            private List<int>[] lists;
            public DirectedGraphBuilder(int count)
            {
                this.lists = new List<int>[count];
                for (int i = 0; i < count; i++)
                    this.lists[i] = new List<int>();
            }

            public void Add(int from, int to)
            {
                this.lists[from].Add(to);
            }
            public int[][] ToArray()
            {
                var N = this.lists.Length;
                var res = new int[N][];
                for (int i = 0; i < N; i++)
                    res[i] = this.lists[i].ToArray();
                return res;
            }
        }

        #region いつもの
        private readonly ConsoleReader consoleReader;
        public Program(ConsoleReader consoleReader) { this.consoleReader = consoleReader; }
        static void Main() => Console.WriteLine(new Program(new ConsoleReader()).Calc()); static string AllLines<T>(IEnumerable<T> source) => string.Join("\n", source);
        #endregion
    }

#pragma warning disable
    public class ConsoleReader { public class SplitLine { string[] splited; public SplitLine(string line) { splited = line.Split(); } public string[] String => splited.ToArray(); public int[] Int => splited.Select(int.Parse).ToArray(); public long[] Long => splited.Select(long.Parse).ToArray(); public ulong[] ULong => splited.Select(ulong.Parse).ToArray(); public double[] Double => splited.Select(double.Parse).ToArray(); } public class RepeatLine { ConsoleReader reader; int num; public RepeatLine(ConsoleReader reader, int num) { this.reader = reader; this.num = num; } public IEnumerable<string> String { get { for (int i = 0; i < num; i++) yield return reader.String; } } public IEnumerable<int> Int { get { for (int i = 0; i < num; i++) yield return reader.Int; } } public IEnumerable<long> Long { get { for (int i = 0; i < num; i++) yield return reader.Long; } } public IEnumerable<string[]> Split { get { for (int i = 0; i < num; i++) yield return reader.Split.String; } } public IEnumerable<int[]> SplitInt { get { for (int i = 0; i < num; i++) yield return reader.Split.Int; } } } public virtual string String => Console.ReadLine(); public int Int => int.Parse(String); public long Long => long.Parse(String); public ulong ULong => ulong.Parse(String); public double Double => double.Parse(String); public SplitLine Split => new SplitLine(String); public RepeatLine Repeat(int count) => new RepeatLine(this, count); }
}

Measured memory_bytes: 33528