using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProgrammingContest
{
    class Writer : IDisposable
    {
        private System.IO.TextWriter Out { get; }
        private StringBuilder Sb { get; }
        private bool IsReactive { get; }
        public Writer(string path)
            : this(new System.IO.StreamWriter(path))
        {
        }
        public Writer(bool isReactive)
            : this(null, isReactive)
        {
        }
        public Writer(System.IO.TextWriter writer = null, bool isReactive = false)
        {
            this.Out = (writer ?? Console.Out);
            this.IsReactive = isReactive;
            if (!this.IsReactive)
            {
                this.Sb = new StringBuilder();
            }
        }
        public void Dispose()
        {
            if (!this.IsReactive)
            {
                this.Out.Write(Sb.ToString());
            }
            if (!this.Out.Equals(Console.Out))
            {
                this.Out.Dispose();
            }
        }
        public void Write(object val)
        {
            if (this.IsReactive)
            {
                this.Out.Write(val.ToString());
                this.Out.Flush();
            }
            else
            {
                this.Sb.Append(val.ToString());
            }
        }
        public void WriteFormat(string format, params object[] vals)
        {
            if (this.IsReactive)
            {
                this.Out.Write(format, vals);
                this.Out.Flush();
            }
            else
            {
                this.Sb.AppendFormat(format, vals);
            }
        }

        public void WriteLine(object val = null)
            => this.WriteFormat((val?.ToString() ?? string.Empty) + Environment.NewLine);

        public void WriteLine(int val)
            => this.WriteLine(val.ToString());

        public void WriteLine(long val)
            => this.WriteLine(val.ToString());

        public void WriteLine(string val)
            => this.WriteLine((object)val);

        public void WriteLine(string format, params object[] vals)
            => this.WriteFormat(format + Environment.NewLine, vals);
    }

    class Scanner : IDisposable
    {
        private Queue<string> Buffer { get; }
        private char[] Sep { get; }
        private System.IO.TextReader Reader { get; }
        public Scanner(string path, char[] sep = null)
            : this(new System.IO.StreamReader(path), sep)
        {
        }
        public Scanner(System.IO.TextReader reader = null,
                        char[] sep = null)
        {
            this.Buffer = new Queue<string>();
            this.Sep = (sep ?? new char[] { ' ' });
            this.Reader = (reader ?? Console.In);
        }
        private void CheckBuffer()
        {
            if (this.Buffer.Count == 0 && this.Reader.Peek() != -1)
            {
                string str = string.Empty;
                for (; string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str);
                str = this.Reader.ReadLine()) ;

                var strs = str.Split(this.Sep)
                    .Where(el => !(string.IsNullOrEmpty(el) || string.IsNullOrWhiteSpace(el)));
                foreach (var el in strs)
                {
                    this.Buffer.Enqueue(el);
                }
            }
        }

        public void Dispose()
        {
            if (!this.Reader.Equals(Console.In))
            {
                this.Reader.Dispose();
            }
        }

        public string Next()
        {
            this.CheckBuffer();
            return this.Buffer.Dequeue();
        }

        public string[] GetStringArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.Next())
                         .ToArray();

        public int NextInt()
            => int.Parse(this.Next());

        public int[] GetIntArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextInt())
                         .ToArray();

        public double NextDouble()
            => double.Parse(this.Next());

        public double[] GetdoubleArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextDouble())
                         .ToArray();

        public long NextLong()
            => long.Parse(this.Next());

        public long[] GetLongArray(int N)
            => Enumerable.Range(0, N)
                         .Select(e => this.NextLong())
                         .ToArray();

        public bool IsEnd
        {
            get
            {
                this.CheckBuffer();
                return this.Buffer.Count == 0;
            }
        }
    }

    class MainClass : IDisposable
    {
        private Scanner Sc { get; }
        private Writer Wr { get; }
        private string InFilePath => "in.txt";
        private string OutFilePath => "out.txt";
        public MainClass()
        {
            this.Wr = new Writer(this.IsReactive);
            //this.Wr = new Writer(this.OutFilePath);
#if DEBUG
            if (!this.IsReactive)
            {
                this.Sc = new Scanner(this.InFilePath);
            }
            else
            {
                this.Sc = new Scanner();
            }
#else
            this.Sc = new Scanner();
#endif
            this.Solve();
        }
        static void Main(string[] args)
        {
            using (new MainClass()) { }
        }

        public void Dispose()
        {
            this.Sc?.Dispose();
            this.Wr?.Dispose();
#if DEBUG
            Console.WriteLine("press any key to continue...");
            Console.ReadKey();
#endif
        }
        
        void Solve()
        {
            int N = Sc.NextInt();
            int M = Sc.NextInt();
            Graph G = new Graph(N);
            HashSet<int> se = new HashSet<int>();
            for (int i = 0; i < M; i++)
            {
                int x = Sc.NextInt() - 1;
                int y = Sc.NextInt() - 1;
                G.AddEdge(x, y, 1);
                se.Add(y);
            }
            {
                var t = G.TopologicalSort();
                w = new int[N];
                for (int i = 0; i < t.Length; i++)
                {
                    w[t[i]] = i;
                }

            }
            SkewHeap<State> que = new SkewHeap<State>((l, r) =>
            {
                if (l.Len == r.Len)
                {
                    return (w[l.Idx] - w[r.Idx]);
                }
                else
                {
                    return -(l.Len - r.Len);
                }
            });
            for (int i = 0; i < N; i++)
            {
                if (!se.Contains(i))
                {
                    que.Push(new State(i, 0));
                }
            }
            int[] dp = new int[N];
            for (int i = 0; i < N; i++)
            {
                dp[i] = -1;
            }
            while (que.Count > 0)
            {
                var e = que.Poll();
                int idx = e.Idx;
                int len = e.Len;
                if (len <= dp[idx])
                {
                    continue;
                }
                dp[idx] = len;
                foreach (var el in G.ListGraph[idx])
                {
                    if (dp[el.To] >= len + 1)
                    {
                        continue;
                    }
                    que.Push(new State(el.To, len + 1));
                }
            }
            Wr.WriteLine(Math.Max(0, dp.Max()));
        }
        int[] w;
        class State : IComparable<State>
        {
            public int Idx { get; set; }
            public int Len { get; set; }
            public State(int i, int l)
            {
                Idx = i;
                Len = l;
            }

            public int CompareTo(State other)
            {
                throw new NotImplementedException();
            }
        }

        private bool IsReactive => false; // TODO: reactive check !!
    }

    class Graph
    {
        public const long InfCost = (long)1e18;
        public struct Edge : IComparable<Edge>
        {
            public int To { get; }
            public long Cost { get; }
            public Edge(int to, long cost)
            {
                this.To = to;
                this.Cost = cost;
            }

            public int CompareTo(Edge other)
                => Math.Sign(this.Cost - other.Cost);
        }
        public List<Edge>[] ListGraph { get; }
        public Graph(int N)
        {
            this.ListGraph =
                new List<Edge>[N].Select(e => new List<Edge>())
                                 .ToArray();
        }
        public void AddEdge(int from, int to, long cost)
            => this.ListGraph[from].Add(new Edge(to, cost));

        public int[] TopologicalSort()
        {
            int N = this.ListGraph.Length;
            var tsort = new List<int>();
            var inCnt = new int[N];
            for (int from = 0; from < N; from++)
            {
                foreach (var e in this.ListGraph[from])
                {
                    inCnt[e.To]++;
                }
            }
            var q = new Queue<int>();
            for (int i = 0; i < N; i++)
            {
                if (inCnt[i] == 0)
                {
                    q.Enqueue(i);
                }
            }
            while (q.Count > 0)
            {
                int now = q.Dequeue();
                tsort.Add(now);
                foreach (var e in this.ListGraph[now])
                {
                    inCnt[e.To]--;
                    if (inCnt[e.To] == 0)
                    {
                        q.Enqueue(e.To);
                    }
                }
            }
            return tsort.Count == N ? tsort.ToArray()
                                    : null;
        }
    }

    class SkewHeap<T> where T : IComparable<T>
    {
        private class HeapNode
        {
            public HeapNode l, r;
            public T val;
            public static Func<T, T, int> CompareTo;

            public HeapNode(T val)
            {
                this.val = val;
                this.l = this.r = null;
            }
            public static HeapNode Meld(HeapNode leftNode, HeapNode rightNode)
            {
                if (leftNode == null)
                {
                    return rightNode;
                }
                if (rightNode == null)
                {
                    return leftNode;
                }
                if (HeapNode.CompareTo(leftNode.val, rightNode.val) > 0)
                {
                    var t = rightNode;
                    rightNode = leftNode;
                    leftNode = t;
                }
                leftNode.r = HeapNode.Meld(leftNode.r, rightNode);
                var t2 = leftNode.l;
                leftNode.l = leftNode.r;
                leftNode.r = t2;
                return leftNode;
            }
        }
        private HeapNode topNode;

        public int Count { get; private set; }

        public SkewHeap() : this(null, null) { }

        public SkewHeap(Func<T, T, int> comp) : this(null, comp) { }

        public SkewHeap(IEnumerable<T> source) : this(source, null) { }

        public SkewHeap(IEnumerable<T> source, Func<T, T, int> compare)
        {
            HeapNode.CompareTo = (compare ?? ((T l, T r) => l.CompareTo(r)));
            this.topNode = null;
            this.Count = 0;
            if (source != null)
            {
                foreach (var e in source) this.Push(e);
            }
        }

        /// <summary>
        /// 捨てる
        /// </summary>
        public void Pop()
        {
            this.topNode = HeapNode.Meld(this.topNode.l, this.topNode.r);
            this.Count--;
        }
        /// <summary>
        /// 見る
        /// </summary>
        public T Top() => this.topNode.val;

        /// <summary>
        /// 入れる
        /// </summary>
        public void Push(T val)
        {
            this.topNode = HeapNode.Meld(this.topNode, new HeapNode(val));
            this.Count++;
        }
        /// <summary>
        /// 出す
        /// </summary>
        public T Poll()
        {
            T retVal = this.Top();
            this.Pop();
            return retVal;
        }

        public bool IsEmpty => this.topNode == null;

        public void Merge(SkewHeap<T> otherHeap)
        {
            this.Count += otherHeap.Count;
            this.topNode = HeapNode.Meld(this.topNode, otherHeap.topNode);
        }
    }
}


Measured memory_bytes: 35812