Problem:
Score : 100 points

Problem Statement

There is a directed graph G with N vertices and M edges.
The vertices are numbered 1, 2, \ldots, N, and for each i (1 \leq i \leq M), the i-th directed edge goes from Vertex x_i to y_i.
G does not contain directed cycles.

Find the length of the longest directed path in G.
Here, the length of a directed path is the number of edges in it.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq x_i, y_i \leq N

All pairs (x_i, y_i) are distinct.

G does not contain directed cycles.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_M y_M

Output

Print the length of the longest directed path in G.

Sample Input 1

4 5
1 2
1 3
3 2
2 4
3 4

Sample Output 1

3

The red directed path in the following figure is the longest:

Sample Input 2

6 3
2 3
4 5
5 6

Sample Output 2

2

The red directed path in the following figure is the longest:

Sample Input 3

5 8
5 3
2 3
2 4
5 2
5 1
1 4
4 3
1 3

Sample Output 3

3

The red directed path in the following figure is one of the longest:

Solution in C# (Mono 4.6.2.0) (judge verdict: Time Limit Exceeded)

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();
            List<int>[] G = Enumerable.Range(0, N).Select(i => new List<int>()).ToArray();
            HashSet<int> se = new HashSet<int>();
            for (int i = 0; i < M; i++)
            {
                int x = Sc.NextInt() - 1;
                int y = Sc.NextInt() - 1;
                G[x].Add(y);
                se.Add(y);
            }
            SkewHeap<State> que = new SkewHeap<State>();
            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[idx])
                {
                    if (dp[el] >= len + 1)
                    {
                        continue;
                    }
                    que.Push(new State(el, len + 1));
                }
            }
            Wr.WriteLine(Math.Max(0, dp.Max()));
        }

        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)
            {
                return other.Len - Len;
            }
        }

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

    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);
        }
    }
}